🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.editors
Article #2951

Re: Centering lines in vi

#2951
From: James C. Hu
Date: Thu, 28 Jan 1993 22:43
76 lines
2554 bytes
pietro@nova.bellcore.com (Pietro Manzoni) writes:

>Hi,
>is there anybody who knows whether there is a :map command to center a
>single line in VI?

There is probably already a package that does this, but I'd put this in
as an example on how to build useful simple filters for vi.  At the end
of this post is a simple program that centers text.  Compile it and
install it in ~/bin/center, or whatever.

Then, make the following map:
:map == !!center^M
(the ^M represents the result of hitting CTRL-V followed by CTRL-M)

Presto, you have a pseudo center "operator".  Now you can do a == to
center a single line, or a 5== to center the next 5 lines.

Enjoy.

/* File:    center.c
 * Creator: James C. Hu (sirius@matt.ksu.ksu.edu)
 *
 * Description:
 *     Centers lines of input.
 * 
 *     Caveats are that lines cannot be longer than the specified
 *     centering line length, if they are, then they may be truncated,
 *     and that the default centering line length is 72.
 *
 * Copyright:
 *     This program is placed into the public doman.
 *
 * Date Started: Thu Jan 28 15:33:44 CST 1993
 *
 * Change Log:
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static int length = 72;
static char *buf;
static char format[10];         /* should be enough */

int main(int argc, char *argv[])
{
  int i,n,buflen;
  char *p;

  switch(argc) {
  case 2:
    length = atoi(argv[1]);
    if (length == 0) length = 72;
  case 1:
    break;
  default:
    fprintf(stderr, "usage: center [width]");
    exit(1);
  }

  buf = malloc((length + 2) * sizeof(char));
  while (fgets(buf, length+2, stdin) != NULL) {
    if ((p = strrchr(buf, '\n')) != NULL) *p = '\0';
    while (isspace(*buf)) buf++;
    buflen = strlen(buf);
    sprintf(format, "%%%ds\n", length/2 + (buflen+1)/2);
    printf(format, buf);
  }
  return 0;
}

-- 
James C. Hu (jxh@math.ksu.edu), 1804 Denholm Dr., Manhattan, KS 66502
I speak for me, the whole me, and nothing but for me.  So help me me.
Technical Details
Message-ID: <1k9nhsINN9ia@hilbert.math.ksu.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!ukma!bogus.sura.net!howland.reston.ans.net!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!math.ksu.edu!deadend
References: <1993Jan28.155257.4953@walter.bellcore.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!ukma!bogus.sura.net!howland.reston.ans.net!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!math.ksu.edu!deadend
From: jxh@math.ksu.edu (James C. Hu)
Newsgroups: comp.editors
Subject: Re: Centering lines in vi
Date: 28 Jan 1993 16:43:08 -0600
Organization: Dept. of Mathematics, Kansas State University
Lines: 76
Distribution: inet
Message-ID: <1k9nhsINN9ia@hilbert.math.ksu.edu>
References: <1993Jan28.155257.4953@walter.bellcore.com>
NNTP-Posting-Host: hilbert.math.ksu.edu