Introduction to Vim

Search and replace

Editing a very long configuration file and you can not find the line you have to change? Why not simply search for it?

Searching

Searching in Vim is simple. When you are in the Normal mode, just press / and type the word or phrase you want to search for. As soon as you press <ENTER> Vim will start searching below the current line. You can now jump with n and N (<SHIFT> + n) forward respective backwards between the matches. If you want to search above the current line, use ? instead of /.

Replacing

If you not only want to search for text, but also replace the matches, you can use the :substitute command or its shorthand :s. Readers familiar with Perl should feel at home now, because the syntax on this command is very much alike Perl Compatible Regular Expressions. You have to use a character to mark what Vim should search for and with what to replace the match. Traditionally a / is used, by almost every non word character will work. Between the first two separators you specify what should be replaced, between the second and third separators the replacement is defined. If you want to remove the search pattern, just leave the replacement empty. The command :s/nano/Vim/ will replace the first occurrence of nano with Vim. To remove the first occurrence of Emacs from the current list, you can use for instance :s!Emacs!!.

There are options which change the behaviour of the substitute command. These options are specified after the last separator. A g will not stop at the first occurrence, the replacement happens global in the range, which is by default the current line. Another useful options is c, which makes Vim ask for confirmation before replacing anything.

Regular expressions

Of course you can use regular expressions to search. If you do not know what regular expressions are, you should probably skip this section and start reading about them somewhere on the internet. Spoiler: Regexp are awesome!.

There is not much to say here, the regular expressions work as they should. You have to escape modifiers in the regular expression (e.g. + or *). So \d+ will search for a digit followed by a +, and \d\+ searches for a series of digits.

Ranges

Replacing one line at a time goes to slow? Vim allows you to specify a range in which it will operate. The range is given before the actual command. Use % as range, if you want to operate on the whole file (line by line). You can specify a range in the following syntax: START,END. As START and END you can use line numbers, . for the current line, as well as ^ and , which represent respectively the start and end of the file. An example? :%s!/var/log/messages!/var/log/maillog!g will change the path from /var/log/messages to /var/log/maillog anywhere in the file.

These are far from all ways to specify ranges, an easy on for the start is to use the Visual mode. Enter the Visual mode, select the area you want to operate on and then just start entering your command with : as you are used to.

As you might have noticed, you can combine ranges, movement commands, commands and quantifications quite freely. Feel free to play around with those, there is always u and in the worst case :q!.

You should now have the necessary knowledge about Vim to start using it! Even if you are not very fast in the beginning, you will get faster soon. Do not give up, Vim is a good and handy tool to know. The next two pages will show you how you can enhance Vim, feel free to come back to them later when you are more comfortable with Vim. Welcome to the dark side ;)