vim Hints and Tips


Contents

Introduction

vi is available on just about every UNIX and UNIX-like system you will ever encounter, it can be used without a graphical environment, and it includes features specifically included to be useful in writing source code, but it is a somewhat primitive editor. Newer and more capable variants exist, and in fact vi on the classroom/lab machines starts one of them, namely vim (“vi improved”). (In the rest of the document, I'll use vi to mean the original program and its variants including vim, and vim for the improved version.) There is even a graphical version, gvim, though it does require a graphical environment so is less useful when working remotely. Start it by giving its name (vim or vi) followed by the name of the file to edit (existing or new).

ADVICE: I highly recommend that if you plan to use this editor you take the time to learn a little about it beyond the bare minimum! If you don't, you are likely to find vim quite tedious. But a little time spent up front learning some shortcuts will save much time and tedium in the long run. The rest of this document presents an assortment of tips that should help. There are several ways to learn more:

A note about notation: In the rest of this document,

Basics

The first thing to know is that vi, unlike many editors, has two distinct “modes”, and what happens when you press keys depends on which mode you are in. The modes are:

(Why this distinction? My guess is that the original designer of vi wanted something optimized for editing text, as opposed to simply entering it.) Several commands put you into insert mode (i, a, etc.). vim displays INSERT at the bottom of the screen if you are in insert mode. To get out of insert mode, press ESC.

If you try to type commands while in insert mode, the commands will just be entered in your file. Press ESC, delete the commands from your file, and try the commands again.

If you try to enter text while not in insert mode, the text will be interpreted as commands, often with surprising results. If this happens, first get out of insert mode if necessary and then use “undo” (u) repeatedly to clean up.

Some basic commands:

Slightly beyond basics

The commands in the previous section are enough to get you started, but if they're all you know, you will likely find using vim very tedious. Before looking at more commands it's useful to know some basic principles:

Navigation

You can move around just using the arrow keys, but it can be tedious (and may not even work in plain vi). There are several faster ways:

Editing

You can edit text just by going into insert mode and using the arrow keys and backspace and retyping, but it can be tedious. Commands you may find useful:

Cut/copy/paste

Editing is also a lot less tedious if you know how to cut, copy, and paste.

Search and replace

Searching is described earlier, under “navigation”. There are a couple of ways to do search and replace:

Indentation

If you're editing program source code with a filename ending in a suffix vim knows about (e.g., .scala or .c), vim will try to automatically indent according to its rules for the language. (There are ways to customize this, but they're a little beyond the scope of this document.) Some things to know:

Possibly-confusing vim behaviors

Working with multiple files

vi can work with multiple files; among other things this makes it easy to copy and paste from one file to another:

Customizations

There are a number of things you can to do change how vim behaves or what it displays. Here are some of them. If there's one you like and want to always use, put it in a file .vimrc in your home directory.




2021-09-08