vi Odds and Ends

vi Odds and Ends

You will find as you work with vi that you will often use the same vi commands over and again. Here too, vi can help. Because the named buffers are simply sequences of characters, you can store commands in them for later use. For example, when editing a file in vi, I needed to mark new paragraphs in some way as my word processor normally sees all end-of-line characters as new paragraphs. Therefore, I created a command that entered a “para-marker” for me.

First, I created the command. To do this, I opened up a new line in my current document and typed in the following text:


Had I typed this from command mode, it would have inserted the text “Para” at the beginning of the line. I next loaded it into a named buffer with

, which deletes the line and loads it into buffer p. To execute it, I entered @p. The @ is what tells vi to execute the contents of the buffer.

Keep in mind that many commands, abbreviations, etc., are transitive. For example, when I want to add a new paragraph, I don’t write Para as the only characters on a line. Instead, I use something less common: {P}. I am certain that I will never have {P} at the beginning of a line; however, there are contexts where I might have Para at the beginning of a line. Instead, I have an abbreviation, Para, that I translated to {P}.

Now, I can type in Para at the beginning of a line in input mode and it will be translated to {P}. When I execute the command I have in buffer p, it inserts Para, which is then translated to {P}.

So why don’t I just have {P} in buffer p? Because the curly braces are one set of movement keys that I did not mention yet. The { moves you back to the beginning of the paragraph and } moves you forward. Because paragraphs are defined by vi as being separated by a blank line or delimited by nroff macros, I never use them (nroff is an old UNIX text processing language). Because vi sees the brackets as something special in command mode, I need to use this transitivity.

If you are a C programmer, you can take advantage of a couple of nifty tricks of vi. The first is the ability to show you matching pairs of parentheses ( () ), square brackets ([]), and curly braces ({}). In ex-mode (:), type set showmatch. Afterward, every time you enter the closing parenthesis, bracket, or brace, you are bounced back to its match. This is useful in checking whether or not you have the right number of each.

We can also jump back and forth between these pairs by using %. No matter where we are within a curly braces pair ({}), pressing % once moves us to the first (opening) brace. Press % again and we are moved to its match (the closing brace). We can also place the cursor on the closing brace and press % to move us to the opening brace.

If you are a programmer, you may like to indent blocks of code to make things more readable. Sometimes, changes within the code may make you want to shift blocks to the left or right to keep the spacing the same. To do this, use << (two less-than signs) to move the text one “shift-width” to the left, and >> (two greater-than signs) to move the text one “shift-width” to the right. A “shift-width” is defined in ex-mode with set shiftwidth=n, where n is some number. When you shift a line, it moves left or right n characters.

To shift multiple lines, input a number before you shift. For example, if you input 23>>, you shift the next 23 lines one shiftwidth to the right.

There are a lot of settings that can be used with vi to make life easier. These are done in ex-mode, using the set command. For example, use :set autoindent to have vi automatically indent. To get a listing of options which have been changed from their default, simply input :set and you get something like in the following image:

Image – vi set command. (interactive)

Inputting :set all will show you the value of all options. Watch out! There are a lot and typically spread across multiple screens. See the vi man-page for more details of the set command and options.

Some useful set commands include:

  • wrapmargin=n
automatically “word wraps” when you get to within n spaces of the end of the line
  • showmode
tells you whether you are in insert mode
  • number
displays line numbers at the left-hand edge of the screen
  • autowrite
Saves any changes that have been made to the current file when you issue the :n, :rew, or :! command
  • ignorecase
Ignores the case of text while searching
  • list
Prints end-of-line characters such as $ and tab characters such as ^I, which are normally invisible
  • tabstop=n
Sets the number of spaces between each tab stop on the screen to n
  • shiftwidth
Sets the number of spaces << and >> shifts each line