Centering lines in vi

Jerry Peek jdpeek at rodan.acs.syr.edu
Wed Sep 13 14:51:53 AEST 1989


In article <2283 at umbc3.UMBC.EDU> rostamia at umbc3.UMBC.EDU (Rouben Rostamian) writes:
> Is there a command that returns the current column position of the cursor 
> in vi?

Here's something that gets close.  I just dreamed this up, so I haven't tested
it real thoroughly.  It's a kludge, but it seems to work.

First part:  Define this vi macro named "v":
	:map v i^A^[:.w !colpos^[0f^Ax
where the ^A is a real control-A character (I listed it here as a ^ followed
by an A) and ^[ is a real ESC character (type it as control-V ESC).  The ^A
isn't important; you can use any character that you won't appear in your
file.  The macro inserts a ^A character in the line, then writes the line to
the standard input of a shell script called "colpos" (COLumn POSition).
After that, the macro finds the ^A again and removes it from the line.

Second part: the 'colpos' script (written for 4.3 BSD):
	#! /bin/sh
	sed 's/.*//' | wc -c
	# time to read answer:
	# sleep 1
(sorry, no "shar file").  Again, replace the ^A with a real control-A
character.  The script reads the line from vi (from the script's standard
input), strips off the ^A and the rest of the line, and uses "wc -c" to count
how many characters are left on the line.  It prints that number to its
standard output, which 'vi' displays.  I commented out the last line; see
below.

This kludge has some problems:
	- sometimes, 'vi' tells me 'press return to continue' and pauses
	  after running the macro.  Other times, it doesn't pause.
	- sometimes, the ^A is removed from the line.  Other times, it isn't.
I don't have time to work on this more.  If I wait to clean it up, I'll never
get to it!  I posted this in case someone wants to clean it up.  Hope it
helps.

--Jerry Peek; Syracuse University Academic Computing Services; Syracuse, NY
  jdpeek at rodan.acs.syr.edu///JDPEEK at SUVM.BITNET///GEnie: J.PEEK1
  +1 315 443-3995



More information about the Comp.unix.questions mailing list