Vim Tip #12: Search and Replace


tags: Vim

Vim Tips

Vim and NeoVim have an incredibly powerful search and replace capability. Like so many things with Vim, it's a steep learning curve. The basics aren't too bad, so we'll try to cover them today.

The / character (in Normal Mode) starts a search, and must be followed by the word (or regex - but that's the lesson for another day) that you're searching for. For example, /the followed by pressing the <Enter> key will go to the next occurrence of the word "the" (if there is one). To search for the next occurrence after that, press n. You can continue pressing n to keep jumping forward until you find the one you want. But if you decide you need to look farther back, you can search in the other direction with N - and in the same way, keep searching in the other direction by pressing it repeatedly.

There are a couple settings around this that are worth understanding. :set ignorecase will mean that your searches will find results regardless of the case you type, or the case of the results. But I've developed a taste for :set smartcase. I recommend that you do :help smartcase to understand it, but the gist of it is that if your search is lowercase, a non-case-sensitive search is done, but if you capitalize any part of the search it becomes case-sensitive. Note that smartcase only works if ignorecase is also set! (Add them to your ~/.vimrc if you want them set permanently.)

By default, a search goes downward from your current location. You can reverse this behaviour by starting the search with a question-mark: ?the. Note that n and N behave as before ... the first continues in the search direction, the second reverses the search direction.

Search and replace is very similar, but first understand two of Vim's default behaviours related to this: without you telling it otherwise, it only works on the current line and it stops after doing the first replacement. To replace the first occurrence of "the" with "a" on the current line (even if that occurrence occurs to the left of the current cursor position): :s/the/a/ and press return. (The trailing slash isn't a requirement, but it will be if you apply any options, so I recommend getting in the habit of always using it.) To replace all occurrences on the current line, use s/the/a/g ('g' is usually presented as 'global', although that doesn't seem quite right on a single line ...).

This can of course be expanded to a range of lines or the entire file. If you're not familiar with it, :set number will show you line numbers. To perform the substitution on lines 5 through 10, use :5,10s/the/a/gc. I've also added the c option this time, which means "confirmation." You'll be asked in each case if the substitution should be made. I commonly use c because as well as letting you choose "y" or "n", the "a" option means "all" - so using c allows you to see your search-and-replace is working well, and then you can press "a" to do all the rest without confirmation. To do the entire file, use :%s/the/a/ . The % indicates the whole file.

Like all things Vim, using these requires you to remember a fair bit. Use it, practise, and eventually it becomes second nature.