Greg Hinkel's UNIX Tip of the Week for June 23, 1996

Some advanced vi "commands."  See the "Previous tip of the week" for
more basic vi commands.

Here's a few ideas that may help you navigate in "vi" more quickly.

In Command Mode

     	Command 	Action
     	----------------------
	  .		(That's a period).  Repeats the previous command, i.e.,
			if you insert a new line of text, entering "." will 
			insert the same line at the current cursor position.
			Likewise if you delete a word (using dw or dW) and then
			enter "." the next word will be deleted.  (You don't
			enter the double quotes - I put them there so it's 
			easier to read.)
	  yw		yank (copy) the current word (into the unnamed buffer).
	  yy		yank (copy) the current line (into the unnamed buffer).
	  p  		put the text (in the unnamed buffer) after the cursor.
	  P		put the text (in the unnamed buffer) before the cursor.
			examples:  you want to copy a word from its current
				   position and insert it 3 words later 
				   yw3wP
				   yw - yank current word
				   3w - skip ahead 3 words
				   P  - Put word before cursor.

				   You want to copy 3 lines from the current
				   position and put them at the end of the file.
				   3yyGp
			           3yy - yank 3 lines
			 	   G - Go to end of file
				   p - place after cursor.
	  
	  s		replace a string with another string.
			syntax.  s/oldstring/newstring/flags
				 s - substitute command
				 / - field delimiter (doesn't have to be /)
				 oldstring - string to be replaced
				 newstring - replacement string
				 flags - (optional) 
					 c - prompt for confirmation
					 g - replace all occurrences in line
					     (by default only the first 
					      occurrence in a line is replaced)
			examples:  replace the string "wabbit" with "rabbit"
				   s/wabbit/rabbit
				   
				   replace all occurrences of "bugs bunny" with
				   "Bugs Bunny" in lines 10 through 120
				   :10,120s/bugs bunny/Bugs Bunny/g

	  g		search for a string and then substitute a new string
			for an old string (where "old string" may or may not be
			the "string" we searched for).
			syntax.  :g/string/s/oldstring/newstring/flags
			example:  Search for the string Hinkel and replace the
				  initials G. C. with Greg.
				  :g/Hinkel/s/G. C./Greg/g

	  J		Join the current line with the next line.  Can be
			preceded by an integer "x" to join the next "x" lines.
	  u		undo previous command
	  U		Undo all the changes to the current line (Do not move
			the cursor from the line before entering U).
	  ~		Change the case of the current character.
	  :!command	Execute one shell command then return to editing
	  :r!command	Execute a shell command and insert its output below the
			current line.
			Example:  To put a list of files into the file you are 
				  editing.
				  :r!ls
				  
				  Here's a great one.  Format the following
				  paragraph so that lines are no longer than
				  79 characters and words are not broken
				  across lines.
				  !}fmt -79

        Marking
          Text		Action
        ----------------------
	
	  mn		mark a position for use by a subsequent command.  "n"
			is one of the letters a-z.  You can mark up to 26 
			locations.  	
			Example:  Mark the current position with marker a.
				  ma

	  `n		(Back quote n).  go to character marked by "n"
	  'n		(Single quote n).  go to beginning of line marked by n.

	  		Examples  Delete from current character through the 
				  character marked by a.
				  d`a

				  Delete from current line through line marked
				  by b.
				  d'b

				  Copy from current line to line marked by z,
				  and insert it before line number 10.
				  y'z10GP
				  y'z - yank lines to line marked by z
				  10G - go to line number 10
				  P   - put text before cursor.
				

	Up till now everything we've done is with the "unnamed buffer" which
	is the default. It's "unnamed" because we haven't specified a buffer
	in which to use.  However, there are 26 named buffers, whose names
	are a-z.  These buffers can be used to save text for later use by
	another command.  To use these buffers you use "n (where n is a letter
	from a-z).  
	Examples:  Yank (copy) 4 lines into buffer 'a'
		   "a4yy

		   Delete the current line and put it into buffer 'b'
		   "bdd

		   Put the contents of the 'a' buffer after the cursor.
		   "ap

		   Delete lines from the current position to the line marked 
		   "a" and put them into the 'a' buffer.
		   "ad'a
		
	Any time you place something into a buffer it overwrites what is 
	currently in that buffer.  However, if you want to append to a buffer
	rather than overwriting it, you use an upper case letter (A-Z).  So
	to append the current line to buffer 'b' and then place the entire
	contents of buffer 'b' at the end of the paragraph do this.
	"Byy}"bp
	"Byy - copy current line to buffer b (appending to it).
	}    - go to end of paragraph
	"bp  - put buffer b after cursor.
		
	
	Settable options	Action
	------------------------------
	:set wrapmargin=n	Set right margin to n.  Uses automatic word
				wrap
	:set number		Show line numbers.
	:set nonumber		Don't show line numbers.
	:set ignorecase		Ignore upper/lower case in searches
	:set noignorecase	Distinguish between upper and lower case
	:set autoindent		The next line will begin at the same
				indentation point as the previous line.
	:set noautoindent	Turn off automatic indentation
	:set wrapscan		Wrap to top of file duing searches.
	:set nowrapscan		Don't wrap to top of file duing searches.
	:set tabstop=n		Set tabs at every n character positions.
	:set showmatch		Show matching close bracket/parenthesis for 
				open bracket/parenthesis or vice versa. Use %
	:set shiftwidth=n	Shift lines over by n spaces.
	:set autowrite		save files automatically if they have been
				changed when opening another file with :n
	:set all		Show current settings.

	
	Boy this is getting long winded.  There still more features, but I
	think I'll end here.  Get a book like "Learning the vi Editor" from
	O'Reilly & Associates to learn about "key mapping" and abbreviations.
	Actually, I'll show you my 2 most used "key mappings."  The first
	one puts "C" style comments around the current line, and the second
	one removes them.  So, when I enter ^x (control-x), the current line
	gets /* at the beginning, and */ at the end.  When I enter ^y 
	(control-y), the /* at the beginning, and the */ at the end are 
	removed.

	" Put C comments around current line.
	map ^X ^i/* ^[A */^[^
	"
	" Remove C comments from current line.
	map ^Y :s#/\* \([^*]*\) \*\/#\1^[


	These commands are in my ~/.exrc file so that I always have them.  If 
	you don't already know, ~/.exrc is the startup file for vi.
	Oh, what the heck, here's some more things in my ~/.exrc file

	" Put single quotes around the current word.
	map *s Ea'^[Bi'^[
	 
	" Put double quotes around the current word.
	map *d Ea"^[Bi"^[
	 
	" Copy the current line and comment it (shell comment), return to 
	" original line.
	map *# "nY"nPI#^[j
	 
	" Spell check putting the suspect words at the bottom of the file
	map *p :w! z_t.1^MGo^[:r!spell z_t.1^M:!rm z_t.1^M^M
	" Search for misspelled words (only after using *p)
	map *f GI/^["ndd@n
	" Search for next misspelled word (only after using *p and *f)
	map \f @n
	 
	"  Abbreviations
	ab csmd Computer Science and Mathematics Division
	ab gh Greg Hinkel
	ab p (615) 574-3147
	ab em hinkelg AT ornl.gov
	ab U UNIX


Previous Tip of the Week · Index

Greg's Home Page


Greg Hinkel / (hinkelgc AT ornl.gov)
Last Modified: Friday, 02-Sep-2005 12:54:12 EDT
Visitors: 8180 since March 18, 1996