To Index

 Documented in Volume 1 of the UNIX Programmers Manual.
 See /usr/doc/ex/ on 4.2 BSD systems for additional online documentation on this.


  :!ls
 
   will parenthetically run the ls command, and then return you to
  wherever you were in the editor.

    To put output of a command into the editor the easy way:

  :read !date
 
  This will read the output of the date command
  after the line where the curser is parked.

    To change to the line editor ex, use the single keystroke Q.
    To come back to vi from ex, use vi.

    To change case of a character, use ~

    To append a carriage return (RETURN) to each line:

  :1,$s/$/CTRL-M/
 
  Note that the CTRL-M above is entered with TWO keystrokes, CTRL-V followed
  by RETURN (yes, that's carriage return!).

    To process lines in the buffer through a filter, and replace them
  with the filtered output:

  :1,10!expand
 
  will expand tabs of lines 1-10 and put the expanded lines in place of
  lines 1-10.

    To cast out all lines EXCEPT those containing the string "XXXX":

  :g!/XXXX/d
 

    To reverse the order of lines in a file (i.e. the last line will
 become the first):

 :g/^/m0
 

    To position the cursor to a specific column (or the end of line,
  whichever is shorter) use the |. To go to column 50:

  50|
 

    To execute a text line as a command (commands may be ex commands,
  including ! constructs):

  :ya a
  :@a
 

  within visual mode of the ex editor (e.g.  vi) to reformat a paragraph,
 and even the lines to fit within 72 columns:

  !}fmt
 

    To cause the keystroke 'g' to move the cursor so you can add text to
  the last line on the screen:

  :map g LA
 
  The "g" is mnemonic for "go," and the single keystroke is a lot faster
  than the shift-two-stroke alternative.

  From harriswm@guppy:
   Within vi you can use "substitute replacement patterns" to replace a pattern
  with itself and anything you want to add to it.  So if you wanted to put
  a comma in, say, column 7 (and scoot everything from column 7 to the right),
  use the following command at the colon:

  :%s/....../&,/
 
  where:	% makes changes from line 1 to EOF
      		s substitute
      		/ delimiters for the substitution
      		. any character
      		& the pattern between the first two delimiters

  To change all the characters in a file to lower case, type:

  :1,$s/[A-Z]/\l&/g
 

    To fetch just the user file system name (/va/kemp) from a line such as the
  following (note that 'magic' must be set for this to work... use :set magic
  to set it):

  kemp:sjjkImmahjHl:1104:11:Stephen P. &,3107/sav,2258401:user:/va/kemp:/bin/csh

  Use:

   :s/.*user://
   :s/:.*$//
 
    The first command says "substitute for [any character (.) for any number of
  occurrences (*), followed by the string "user:"] the null string (//)."
    The second command says "substitute for [the string ":" followed by any
  character (.) for any number of occurrences (*), followed by the end of line
  ($) ] the null string (//)."
    Restated, this is: strip the preceding stuff off the left, and then strip
  the trailing stuff off the right, which leaves the desired string.

  # this is a useful feature for vi

   setenv EXINIT 'map #1 Gi/\"add@a|map #2 1G\!Gvispell|set ai sw=3'
 
   Your f2 key will run a file through the spell program, and put the words
  it doesn't find in the dictionary at the bottom of the file.  Then
  typing f1 will cause it to delete the last line in the file, and search
  for the string in the rest of the file.

  #! /bin/sh			#(source for vispell) .......
  tee /tmp/vis$$
  echo SpellingList
  spell /tmp/vis$$
  rm /tmp/vis$$
  #				#end of vispell) .........
 

  From sdcsvax!dcdwest!ittvax!decvax!ucbvax!ucbtopaz!gbergman
  (George M. Bergman) Tue May  1 13:15:10 1984
  Organization: Univ. of Calif., Berkeley CA USA
   ... it is ... useful to be able to keep command lines in the file and
  execute any of them ad lib.
      Given a line of the file, the way to execute it as a bottom-line
  command is to yank it to a named buffer, e.g.

 		"ayy
 
  and then give the bottom-line command that executes the contents of
  that buffer, in this case

 		:@a
 
       I have an item in my EXINIT that makes the single keystroke
  CTRL-O do this for me.  I will give it twice below, once exactly
  as it appears in my EXINIT, and once with the control-character
  shown explicitly as ^+Letter, for those of you seeing this through
  "more" etc.:

 		map  "ayy:@a
 
     explicitly:

 		map ^O "ayy:@a^M
 
       In addition to allowing you to keep commands in files for repeated
  use, it makes it possible to carefully edit a command before executing
  it, make a modification if the first version did not do what you meant
  it to, etc..  It was actually for such editing capability that I set it
  up, and the practice of storing commands for reuse developed after.
       The above mapped key can be preceded by a count.  E.g. 3^O will
  execute the next three lines as bottom-line commands.
       Let me note a couple of limitations:
       In a bank of commands so executed together, e.g. by 3^O, none
  except the last may be a "global":

  		g/pattern/command
 
  If a global appears in nonfinal position, all commands after it are
  ignored.  (This seems to be a general bug of vi; the same thing happens
  if one gives on the bottom-line of vi the command

  		:source filename
 
  and the file contains a global in nonfinal position.  Only in ex mode
  does the command :source behave properly.  Modeline also seems to
  be confused by global commands, by the way.)  On the other
  hand, I regularly execute banks of many mappings together with no
  trouble, as well as things like:

  		10,-w !nroff|col|lpr -h

  		-r !date

  		+,$-w !mail NAME
 
  Banks of several substitute commands sometimes give trouble; messages
  like "extra characters after substitute command"
       Also note that since we are in vi, not ex, the ex commands i, a,
  and c cannot be used.  (However, in place of  a  one can use, say

  		s/$/^Mline1^Mline2^Metc./
 
  with ^M's entered as ^V-carriage-return, and similar
  substitutes for the others, if the resulting command isn't too long.)
       Commands executed in this way (in fact, any commands
  given by mappings, @x macros, etc.) cannot execute a "yank" (or a
  deletion to a named buffer) after any operation that changes the
  file.  (According to Mark Horton, there is good reason for this if the
  yank is to the "nameless" buffer; the fact that it affects yanks to
  named buffers as well is a bug.)  This behavior is somewhat capricious:
  I have written one command involving such a yank that works OK for no
  reason I understand, and another that does something totally
  unrelated to what it ought to.
       Sometimes an "undo" following a command undoes both it and
  some preceding commands!
       To avoid accidentally executing a command with serious consequences
  (e.g. mailing a half-written letter, which I once did) one can write in
  such commands preceded by the escape character ", e.g.

  	"+,$-w !mail NAME
 
  and delete the " only when ready to use.
       Despite its minor drawbacks, this trick has totally transformed
  the use of the editor for me!....

  From: Larry West, UC San Diego, Institute for Cognitive Science
       UUCP:	{decvax!ucbvax,ihnp4}!sdcsvax!sdcsla!west
       ARPA:	west@NPRDC	{ NOT:  }
  diamant@wanginst.UUCP (Ira Diamant) writes:
  >	Does anyone know if there is a way to access the name of the current
  >file in vi?  I want to build a small template generator using macros, but
  >I have not been able to find this information in the documentation.

     Well, sort of.   The character "%", used in shell-escapes (":!" or
  "!" [e.g., "!!" for one line]) is expanded to the current
  filename.   Likewise, "#" is expanded to the alternate filename,
  if any.   For example, try this command:

  		:!ls -l %
 
     So you could put that command into a mapping (and probably a macro
  but not an abbreviation) by doing something like:

  		:map V :!ls -l %^V^M
 
 	NOTE that the above are the ACTUAL control characters:
			^M =	CTRL-M
			^D =	CTRL-D
     This will not work on older versions of VI for various reasons.

To Index