How to Configure vi and vim to Meet Your Needs

Learn how to configure vi and vim to meet your individual needs, including setting key bindings, customizing the appearance, and enabling additional features.

Configuring vi and vim

  • Configuring on the fly
    • :set or :set all
  • Configuring permanently
    • ~/.vimrc or ~/.exrc (do not include the colon [:] in these files)
  • A few common configurations items
    • :set number
    • :set autoindent
    • :set textwidth=65 (vim only)
    • :set wrapmargin=15
    • :set ignorecase
  • Run :help option-list for a complete list

Dozens of configurations items exist for vi and vim. To examine your current configuration, run:

  • :set lists a small number of important configuration items
  • :set all lists the vast panoply of configuration items

To change a configuration item, use the :set command. Some common items to set include:

  • :set showmatch causes the cursor to momentarily jump to the matching left curly brace or left parenthesis when a right curly brace or a right parenthesis is typed. :se sm is an abbreviation of this.
    :set noshowmatch (:se nosm) turns off this behavior.
  • :set autoindent (:se ai) causes new lines to inherit the indentation level of the previous line. This is very useful for programmers. :set noautoindent (:se noai) turns this off.
  • :set textwidth=65 causes text to wrap (by inserting a hard return) when the text exceeds 65 characters (of course, any number can be given). :set textwidth=0 turns this off. This option only works in vim.
  • :set wrapmargin=15 (:se wm=0) causes text to wrap when it reaches 15 characters from the right margin. :set wrapmargin=0 turns this off.
  • :set number (se nu) causes line numbers to be displayed in the left margin (visual only; line numbers are not actually stored in the document). :set nonumber (se nonu) turns this off.
  • :set ignorecase (se ic) causes searches to be case-insensitive (by default, they are case sensitive). :set noignorecase (se noic) turns this off.
  • To save these settings so that they are run at every invocation of the editors, place the commands above (minus the colon) in ~/.vimrc. The older ~/.exrc will be read by both vi and vim if ~/.vimrc does not exist.
  • vi/vim built-in help
    • :help
    • :help topic
    • Use :q to exit help
  • vimtutor command

To learn more about vim, you can type :help to enter Ex mode and get a quick list of help items. :q will exit the help screen.

vimtutor is a great way to explore the functions of vim and get practice, too.

Scenario

In this sequence, you will create a ~/.vimrc file to practice document creation with vim. This file is also used to set your preferred settings for vim

Instructions

  1. Start by creating a new file:
    vim ~/.vimrc
  2. Remember that before you can type anything, you will need to enter insert mode. To do this, press:
    i
    You can now type as you would in an ordinary text editor. The next exercise will explore some of what you can do in command mode.
  3. Type the following as the first line in your document:
    :set nu
    This will cause line numbering to be turned on by default.
  4. Press Enter to start a new line and type the following:
    :set wrapmargin=10

    This will cause lines to automatically “wrap” when they come within 10 characters of the edge of vim’s screen, instead of being treated like one long line, which is vim’s default behavior.
  5. If there are any other settings you have decided that you prefer, add them now.
  6. Exit insert mode by pressing Esc, then enter ex mode and instruct vim to write (save) the file and quit:
    :wq
  7. Open ~/passwd again. Observe that line numbering, along with the other settings you added to ~/.vimrc are now automatically enabled. When you are finished exploring, quit vim.

Leave a Comment