Learning the vi Editor/Vim/Tips and Tricks

From Wikibooks, open books for an open world
< Learning the vi Editor‎ | Vim
Jump to: navigation, search

Learning the vi Editor/TOC

The Vim Tipbook is a collection of tips, hints and HowTos for using the Vim text editor. It is an outgrowth of the Vim tips database in a more flexible format, and also includes some helpful posts from the Vim mailing lists.

For information on the general use of Vim, please see the Learning the vi Editor/Vim.

About this Book[edit]

Tips for Editing[edit]

  • Where possible, extensive personal configurations should be avoided. Keep suggestions within the scope of a single tip. You may wish to link to several other tips that might be used alongside, much in the same way a food cookbook would suggest dishes that go well together.
  • Always provide enough information so that a tip can be used from Vim's default compatible settings.

Conventions Used[edit]

A single character, such as 'a' or '1'.
<ESC>, <Ctrl-[>
Indicates that the Escape (Esc) key on your keyboard should be pressed, which is identical to Control and '['.
Indicates that the Return (Enter) key should be pressed.
Indicates that the Tabulator key should be pressed
<Ctrl-x>, <C-x>
Indicates that the Control key and the 'x' key should be pressed simultaneously. 'x' can be almost any other key on your keyboard.
<Shift-x>, <S-x>, <X>
Indicates that the Shift key and the 'x' key should be pressed simultaneously
<Meta-x>, <M-x>
Indicates that the Meta or Alt key and the 'x' key should be pressed simultaneously.
:quit, :q
An Ex command. started with <:>, followed by the command and ends with <CR>. For many Ex commands there is a long form (:quit) and a short form (:q).
:set nocompatible 
represents a setting.
strlen () 
represents a function.
/pattern/, ?pattern?
A Search pattern. Search pattern in vi are regular expressions.
:ranges/search/replace/options, :global /pattern/ delete
A Search pattern combined with an Ex command.

All commands in vi are case sensitive.

Where a series of commands are required to be entered, these might be listed in a preformatted block:

 {{Vi/Ex|set}} {{Vi/set|number}}
 {{Vi/Ex|set}} {{Vi/set|textwidth=70}}
 {{Vi/Ex|set}} {{Vi/set|wrap}}

Which will produce the following formatted text:

set number
set textwidth=70
set wrap

Vim Help[edit]

If you are new to vi, try the vimtutor command. It's an excellent guide for the beginner.

Vim has an extensive help system. EVERYTHING is covered. This system is so extensive, however, that finding the needed information is sometimes akin to finding one's own little needle in a huge stack of hay. But even for that, there are Vim tools:

  • (assuming 'nocompatible' is already set)
set wildmenu
  • Help tag completion: if you think 'foo' is part of something which has a hyperlink in the help system, use
help foo<Tab>
where <Tab> means "hit the Tab key", and if there is only one possible completion Vim fills it in for you; if there is more than one the bottom status line is replaced by a menu which can be navigated by hitting the <Left> and <Right> arrow keys; accept a selection by hitting <Enter>, abort by hitting <Esc>.
  • The :helpgrep function: if you think that some regular expression describe text you want to search for in the text of all the help files, use
helpgrep <pattern>
where <pattern> is a Vim regular expression, like what you can use after / or ? . It may take some time for Vim to look up all its help files, and it may or may not display interim information which may require you to hit Enter to clear the |more-prompt| (q.v.) When the blinking cursor reappears in your editfile, it means Vim has compiled the list of all help locations where your regexp matches. See them by means of the following
 cfirst or :cr
 cnext or :cn
 cprevious or :cprev or :cN
 clast or :cla

Inserting Text From a File or Register[edit]

If your text is in a file on its own, you can use :r with a line number (the number of the line after which to insert, or 0 for "before first line", or . for "after cursor line", or $ for "after last line"; default is after cursor line) in the "range" position, i.e. just before the r. The file name comes as an argument at the end.

Example (after line 5):

  5r ~/template.txt

If your text is in a register, you can use :put with a line number (again) in the range position and the register name (including ", which must be escaped as \", for the default register; or + for the system clipboard) after the :put.

Example (before cursor line):

  .-1put \"

You can also insert a string directly using :put and direct assignment:

  :put ='This is text to insert.'


  :help :read
  :help :put

Full Screen Mode[edit]

To achieve a full screen editing window on any version of gvim you can do:

  :set go-=m go-=T go-=l go-=L go-=r go-=R go-=b go-=F
  :set lines=999 columns=999
  • 'guioptions': We remove the flags one-by-one to avoid problems if they appear in the option in a different order, or if some of them do not appear at all. By choosing which ones to remove (or not) you can customize your own flavour of "full-screen Vim".
  m   when present, menu bar is present
  T   when present, toolbar is present on versions which support it (W32, GTK1, GTK2, Motif, Photon, kvim)
  l   when present, left scrollbar is always present
  L   when present, left scrollbar is present if there is a vertical split
  r   when present, right scrollbar is always present
  R   when present, right scrollbar is present if there is a vertical split
  b   when present, bottom scrollbar is present
  F   when present, gvim (Motif) will display a footer
  • 'lines', 'columns': setting them to a large value will maximize the window.

For more, see:

  :help 'guioptions'
  :help 'lines'
  :help 'columns'

Learning the vi Editor/TOC