An Introduction to Vim for Sysadmins

4389

Why, you ask, should anyone care about Vim? It’s complex, it’s so old it’s a fossil, and you like Kate/Leafpad/Geany/Gedit/Nano/Jed/Lime/Emacs/what-have-you, and the Linux world is cram-full of great text editors, so why bother with Vim?

The main reason it is included in nearly all Linux distributions. Sure, it is rather a desert island scenario to worry about being trapped on an unfamiliar system and you only have Vim to rescue yourself. It does happen. You will especially appreciate it when you have to work over a slow SSH session with high latency because Vim is chock-a-block with powerful single-key commands.

The other reason is it is a customizable powerhouse that rewards a bit of study and tweaking with a nice productivity boost. For example, if you have to tag XML or HTML documents Vim can enter both opening and closing tags with a single keystroke. Or enter blocks of text, such as copyright notices or URLs, with a single keystroke. Vim is so flexible you can make it do just about anything with a minimum number of keystrokes.

Distro Quirks

The ancestral vi is long gone, replaced eons ago by Vim — vi IMproved. Vim includes extensive documentation, unless your distro installs only vim-tiny, which strips out the documentation and other fripperies, which is another reason to know the basics without having to look them up.

Most distros symlink vi to Vim, so you should be able to start it with either vi or vim. However, on some distros, notably Ubuntu, vi starts Vim in vi-compatible mode, and it will behave like the ancestral vi. It will even tell you on the home screen:

Running in Vi compatible mode                                        
type :set nocp for Vim defaults

The biggest hassle with vi-compatible mode is you can’t use the arrow, home, end, page up, or page down keys for navigating your document without entering command mode. We’ll get to Vim’s modes in a moment; the short story is Vim is more comfortable to use than vi. There are two ways to return to normal Vim mode. One is to do what the home screen tells you: press the Escape key and type :set nocp, then press Enter.

To change this permanently create ~/.vimrc and enter this line:

set nocp

A third way is to start Vim with vim instead of vi. On Ubuntu use vim.tiny.

Remembering Vim Commands

Vim’s commands are mnemonic, so it’s not that farfetched that you will remember them. d = delete, y = yank or cut, p = paste, w = write, q = quit. Your hands never leave the keyboard, so you are fast and efficient.

Starting Vim

The biggest hurdle for new Vim users is its dual-mode system. It has a command mode for entering commands, and an input mode for typing your text. Vim starts up in command mode. Let’s start with basic usage.

  • vi opens a new empty document
  • vi [newfilename] opens and names a new document
  • vi [filename] opens an existing document
  • Press the i or Insert key to enter input mode
  • Press the Escape key to leave insert mode and enter command mode

When you are in input mode you can type and edit your text just like any other text editor. Navigate through your document with the arrow keys. Home goes to the beginning of the paragraph, and End goes to the end. Ctrl+Home goes to the beginning of your document and Ctrl+End goes to the end.

Compact Keyboards

If you find yourself stuck with a keyboard that does not have Home, End, or arrow keys, enter command mode:

  • j moves the cursor down one line
  • i moves the cursor up one line
  • G goes to the end of the document
  • gg goes to the beginning of the document
  • h moves the cursor to the left, one character at a time
  • l moves the cursor to the right, one character at a time

Saving Changes and Exiting

Now we come to the fun part: saving your changes and closing Vim.

Save your changes as you go by pressing the Escape key to enter command mode, and then type :w Enter. You will see a confirmation that says something like “newfilename” 7 lines, 40 characters written”.

:sav [filename] names a new document, or saves the file under a new name

To quit and save your changes, enter command mode and type :x Enter or :wq Enter.

:q Enter quits if you have already saved your changes.

If you try to quit with unsaved changes, Vim won’t let you. Make it obey with :q! Enter. (Or save your changes.)

Common Editing Functions

Some common command mode editing functions:

  • u toggles undo/redo.
  • r replaces the character under the cursor
  • x deletes a single character
  • dw deletes a single word, starting from the cursor
  • D deletes to the end of the line, starting from the cursor
  • dd cuts one line
  • Ndd cuts N lines; e.g. 3dd deletes three lines starting with the current line
  • yy copies one line
  • p pastes whatever has been cut or copied
  • :set number displays line numbers
  • :set nonumber turns off line numbering

Advanced Vim

This should be adequate for simple editing tasks like configuration files. To learn advanced Vim functions type :help Enter to see all the built-in documentation. I recommend starting with the tutor, which takes 30-60 minutes to complete. You can launch the tutor outside of Vim by entering the vimtutor command in your shell.

Learn more about Linux through the free “Introduction to Linux” course from The Linux Foundation and edX.