Vim Shortcuts and Text Searches

4612

In our previous Vim how-to, An Introduction to Vim for Sysadmins, we learned enough about using Vim to bang around in short text files and get something done. Now we’re moving on to some of Vim’s excellent power tools: abbreviations (autotext), mapping hotkeys, and text searches.

Abbreviations

Vim’s abbreviations are glorious custom autotext. You can use these for anything: copyright notices, signatures, page headers, code snippets, anything your heart desires. Create a new abbreviation in command mode, like this example of my new signature:

:ab sig Carla Schroder, angel with a lariat

Switch to insert mode to use your new signature. In my example it is mapped to sig, so I type sig and press Enter. You can make your abbreviations concise and mysterious, or make them a little bit longer and mnemonic, like sig. List your abbreviations in command mode:

:ab
! sig Carla Schroder, angel with a lariat 1,0-1 All

Remove an abbreviation:

:una sig

Remove all abbreviations:

:abclear

When you create abbreviations this way, they are not permanent and will disappear when you close Vim. This is good when you need them only for a particular document and don’t expect to use them again. To save them permanently, put them in your ~/.vimrc. They look exactly the same in ~/.vimrc:

:ab sig Carla Schroder, angel with a lariat

If you use una or :abclear your abbreviations in ~/.vimrc become unavailable until you close Vim, but are not removed from ~/.vimrc, so they will be active when you start Vim again.

What if you want to type your abbreviation’s name, like sig, and not print your abbreviation? No problem, just type sig followed by CTRL+V.

Another use for abbreviations is auto-correcting typos. This corrects teh to the:

:iab teh the

:iab is for insert mode only, :cab for command mode, and :ab operates in both modes.

Run :help abbreviations to learn the many finer points. Leave the help screen with :q.

Fast Markup Tagging

Now we’ll use Vim’s maps to create custom keystrokes for inserting markup tags. These examples insert both opening and closing HTML tags around single words.

:map <F4> <strong><Esc>ea</strong><Esc>a
:map <F5> <em><Esc>ea</em><Esc>a
:map <F6> <del><Esc>ea</del><Esc>a

After creating your keymaps switch to insert mode, place your cursor on the first letter of the word, press the appropriate function key, and poof! Nice HTML tags surrounding your word. So what the heck just happened here? Let’s dissect the first mapping.

:map creates a new keymap that works in insert mode. (:map! works in both command and insert modes.) F4 is the hotkey, and remember to type it out when you create the new mapping rather than pressing the key. <strong> is the opening HTML tag. <Esc> switches to command mode, e navigates to the end of the word and then a appends </strong> after the cursor. Cool, eh? And not so mysterious when you crack the Vim code.

You may map commands to any key combinations you choose. F2-F12 and shifted F2-F12 should be safe. When you use other hotkey combinations think up odd combinations that you are unlikely to use in text, like q-consonant combinations, comma-letter, or F-key-letter or number.

The examples above may not be practical, as they are limited to tagging single words. You can also map single tags, like this:

:map <F4>1 <strong>
:map <F4>2 </strong>

Just like abbreviations, you can preserve your key mappings in ~/.vimrc. Of course you may map any text you like.

Text Search

Vim’s basic text search is fast and easy. In command mode, type /string, replacing string with your own text, and then continue the search forward with lowercase n (next), and search backwards with uppercase n. This matches your search string even when it is inside longer strings, so searching for “is” also finds “this” and “isn’t”.

To find an exact match you have to make it a regular expression, like this:

/<is>

Another way to find an exact word is to place the cursor on a word, enter command mode, and type an asterisk to find more occurrences of that word. Again, use n and N to repeat the search.

The default is a case-sensitive search. Set ignorecase for a case-insensitive search:

:set ignorecase

:set smartcase is a rather slick tool for smart case-sensitive searches. This works only on typed search strings. If you search for /wOrd then it will find only that exact match. If you search for /word then it will find all matches regardless of case.

You can override these settings with c (case insensitive) and C (case sensitive) like this:

/wOrdC
/wordc

One more cool search feature, and that is using Vim’s search history to repeat previous searches. Press / or ? to enter your search history, and navigate it with the arrow keys. You may edit any search in your history, and press the Enter key to use the search.

In our next installment, we’ll take an in-depth look at search-and-replace, and how to perform lovely complex feats of searching and replacing in large and multiple documents.

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