Function Key Mappings In Vim

I enjoy using Vim. While I use PHPstorm to fulfill most of my programming needs, I still use Vim quite a bit. Recently I was making some improvements to Opal my command line framework. I wanted to improve my Vim user experience. So I decided to add some function key mappings to trigger commands. This allows you to reduce your keystrokes, and it all started with the pastetoggle option.

I’ve been using the pastetoggle option for a while. It had been mapped to F2. It wasn’t the best keyboard UX. So, that was the first thing I changed. The pastetoggle is a special case in the way it gets defined. Once I changed that, then I started exploring what functionality I should map to my function keys.

Here’s how to assign pastetoggle to F10

" Turn paste mode on/off using <F10>
set pastetoggle=<F10>

This is the minimum you need to map a function key to a command.

map <F9> :echo 'Hello World'<CR>

In the next sample, the :ls lists the currently open buffers. To understand the option, the Vim help documentation says:

If the first argument to one of these commands is <unique> and it is used to define a new mapping or abbreviation, the command will fail if the mapping or abbreviation already exists.

map <F9> :ls<CR>
map <unique> <F9> :ls<CR>

This second line will display an error when you open your file, as your .vimrc file gets processed. It’s a good way to debug your mappings, to check if they’re already being used.

If however you want to check first if a function key has already been mapped, use a conditional.

if maparg("<F9>") == ""
    " List the open buffers
    map <silent> <F9> :ls<CR>
endif

Suggestions

Start with your highest function key number. For me, this was F10. The Keys F11 and F12 are owned by MacOS. F12 show the Dashboard. F11 shows the desktop. the progress with assignment in descending order, by how often you’ll use a particular function key. That way, you can still use F1 and F2, and only be slightly annoyed.

This is because it’s the most comfortable of two-hand movements. The Fn key is the bottom left corner of the MacOS keyboard. You’ll need to hold down the Fn button with your left hand. So reaching for the top-right of your keyboard, with your right hand, is more comfortable.

Keep your Vim Function Key Mappings Together

This will make them easier to manage. I’d suggest putting them at the bottom of your .vimrc to make them easy to find. This also has the advantage of allowing you to define anything you need first.

Use functions

Just like other kinds of code, creating a function is a great way to make thing more modular and re-usable. They’re particularly used when multiple actions need to happen, and they are much easier to type. For example if you had a function called TrimWhitespace, you would call it like so:

:call TrimWhiteSpace()<CR>

Wrap Up

For this article, I focused on making use of the function key mappings in Vim. It should be said though, that you can map more than just the function keys – abbreviations are a kind of mapping. The leader commands also come to mind. I think those would be best for a different article.

Sorry, but comments are closed. I hope you enjoyed the article