10 Assorted Vim Hacks for More Effective Coding

Using Vim was one of the best decisions I ever made as a developer. Once you practice Vim’s keystrokes to the point where they become muscle memory, you will be lightning-fast at coding. It’s definitely not possible to achieve this same editing speed and agility with a standard IDE. In addition to basic keystrokes for more efficient coding and file navigation, there are also a few lesser-known commands that I either learned from documentation or (more often) stumbled across accidentally. These have come in handy quite a few times…


Visual line mode and visual block mode

In addition to visual mode, which is used for selecting text one character at a time, Vim has two visual mode variations called visual line mode and visual block mode. Visual line mode is entered using Shift+V and selects an entire line at a time. Visual block mode is entered using Ctrl+V and selects a rectangular block of horizontally and vertically adjacent characters.

Visual block mode might not seem useful at first, but it does occasionally come in handy if you have a sequence of characters that repeats itself for several lines (one example might be if you have a dashed list and you want to delete the dash and space characters at the beginning of each line all at once). I have seen tricks like this used in tutorials that fall under the “using Vim as an IDE” umbrella.


Lisp mode

The original vi editor upon which Vim is based was written largely with Lisp coding in mind. Consequently there is a setting in Vim that will tell it to automatically indent lines based on the current parenthesis level. This setting is called Lisp mode and it is entered by typing:


:set lisp

You exit Lisp mode in the same way you would turn off any other setting in Vim:


:set nolisp

Command history

The two-character keystroke q: opens up a new Vim window at the bottom of the screen that lists all ex commands in your command history. This includes commands from the current Vim session as well as from the last session. The commands in the command history can be edited just like any other text in Vim, using any of Vim’s editing keystrokes.

Once you have the command you want to run and have applied any modifications you want to make to that command, you can run it by pressing Enter just like in the normal ex mode. To exit the command history without running a command, just type :q to close the history window.


Indent or unindent an entire section at once

You can indent or unindent the line under the cursor using the keystrokes >> and << respectively, but this is not always efficient if you want to indent an entire passage (for example if you’ve just copied a section of code from a different part of the program that was at a different indent level). Fortunately Vim provides a way to easily indent multiple lines in a single keystroke. To do this, you type Shift+V to enter visual line mode, then move the cursor down to select the lines you want and then type either > to indent the passage one level or < to unindent one level. To indent/unindent multiple levels, type a number after selecting the lines and before hitting the indent/unindent key. This will tell Vim to add or remove that many tabs from each line.


Jump to the file under the cursor

This one comes in handy if you’re doing C/C++ programming and want to view a header file you’ve included, either to make sure a function is declared in that header, or to see what its exact prototype is. To do this, move the cursor over the filename you want to navigate to and type gf. There are actually quite a few commands initiated with the g keystroke, depending on what character or sequence you type after it. Navigating to the file under the cursor is one of these functions. I don’t know if there’s any specific keystroke to navigate back, but going back and forward in the buffer list to reset the file buffer accomplishes this task easily enough.


Increment or decrement the number under the cursor

The number under the cursor can be incremented or decremented using the keystrokes Ctrl+A and Ctrl+X respectively. This doesn’t just increment or decrement the digit under the cursor, but the entire number containing that digit. The keystrokes work with both positive and negative numbers, and if there is a 0x preceding the number, Vim will treat it as a hexadecimal number and alter it accordingly.


Get the code for the character under the cursor

Another useful command that uses the g keystroke is ga. This command displays the ASCII code for the character under the cursor. If the character under the cursor is a Unicode character, it will display the UTF-8 encoding of that character.


Record keystrokes

A method of creating macros that I find a lot easier than the standard method given in Vim texts is to record keystrokes into a macro on-the-fly. To do this, you first type q and then the key that is to invoke the macro. This puts Vim in record mode. After entering record mode, you go through all the keystrokes you want to record in real time. When you are finished, type q again to exit command mode. The macro you have recorded can be invoked by typing @ followed by the key for the macro.

A useful hack that I’ve found is to record a macro with an associated key, say w (because it’s right next to q) and then use the following mapping:


:map = @w

Then I can just hold down the = key to run the macro over and over very quickly, possibly to perform the same edit on many different files in rapid succession.


Spellcheck a file

Vim has a built-in spellchecker that is activated using the following ex command:


:set spell spelllang=<language>

In this command <language> can be replaced by any ISO-639 standard language code (e.g. en_us for US English). It will check the document against a dictionary for the given language and highlight any words that are misspelled. The best part is Vim’s spellchecker is completely programmer-friendly, as it recognizes words that are part of a programming language’s syntax (including variable names and other user-defined symbols) and doesn’t report them as spelling errors.


Select a colorscheme

Vim allows the user to create different colorschemes that determine what ANSI colors and styles will be used to highlight a given syntax element. Colorschemes are distinct from syntax files in that whereas a syntax file sets the rules for mapping different syntax elements to syntax classes, a colorscheme simply determines what color and style to use for each syntax class. For example, in the Peachpuff colorscheme, line numbers and statements are highlighted in yellow/brown, comments in blue, types in green, etc.

You change the colorscheme by typing:

colorscheme <name>

You replace <name> with the filename of the colorscheme you want to use (minus the extension). In addition to letting you define your own colorschemes, Vim also provides a number of predefined colorschemes to choose from. Here’s a list of all of them:


blue
darkblue
default
delek
desert
elflord
evening
industry
koehler
morning
murphy
pablo
peachpuff
ron
shine
slate
torte
zellner

All of these colorschemes are stored in the directory /usr/var/vim/<version>/colors.


So there’s my list of hacks, and I hope I was able to teach you something new today. Please consider following my blog and also sharing this content on social media if you enjoyed it. Thanks!

Advertisement

2 thoughts on “10 Assorted Vim Hacks for More Effective Coding

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s