5 Awesome Vim Tricks to Spice up your .vimrc

February 13, 2021
Written by
Sam Agnew
Twilion

Copy of Generic Blog Header 4(5).png

If there's one thing Vim users love more than saving a few seconds per day on keystrokes, it's trading tips and tricks with others about their own setup. As someone who's been using Vim for 7 years now as his primary text editor, I'm going to do my part by sharing some neat things from my .vimrc that people have asked me about at conferences and hackathons in the past.

For each of these examples, I'll provide the code for you to copy/paste into your own .vimrc and try to explain what's going on. Take what you think works for you and leave the rest!

Crosshair style cursor highlighting

One thing that people ask me about often is the crosshair style cursor that I use in my configuration. All you need to do to make your cursor look like this:

Cursor Crosshair

...is add these lines to your .vimrc file (typically located in your home directory, or ~ on most unix based systems):

" Allow crosshair cursor highlighting.
hi CursorLine   cterm=NONE ctermbg=0
hi CursorColumn cterm=NONE ctermbg=0
nnoremap <Leader>c :set cursorline! cursorcolumn!<CR>

This will enable horizontal and vertical highlighting for the row and column you're currently navigating in your file. Honestly, I just think it looks cool. I also included a command that allows you to toggle this on and off, by pressing your leader key followed by c (for "cursor" or "crosshair"). You can change this to fit your liking though.

For the record, I have my leader key set to , and am preserving the original , functionality by remapping with the line nnoremap <leader>, , so that if I ever want to repeat my most recent f or t command in reverse, which I don't use frequently, I just have to hit the key twice.

Copying to your system clipboard

Not being easily able to copy and paste from the system clipboard is a common complaint that many newcomers have when getting started with Vim. While some users can just use the * or + registers to refer to their system clipboard, this doesn't work by default in every installation of Vim. It particularly tends not to work in the Vim that comes pre-installed on many versions of OSX. So here is another way to copy to the system clipboard on OSX:

" Copies the currently selected text to the system clipboard (OSX)
let @c = ':w !pbcopy'

And here is one for pasting:

let @p = ':r !pbpaste'

These are macros that you would add to your .vimrc. To copy or paste with your system's clipboard in Vim with these you would use @c and @p respectively, after selecting text with visual mode (press v).

GIF displaying clipboard pasting

Alternatively, for some Linux distros, pbcopy can be replaced with xclip -i -sel c or xsel -i -b and pbpaste with xclip -o -sel -c or xsel -o -b.

On another note, if you're using this method it's a good idea to make sure you have a quick way to toggle back and forth between paste mode, for when you are going to be pasting large amounts of text into Vim. I do that with the following line:

set pastetoggle=<leader>p

Alternatively, if you do compile Vim with your equivalent X11 support to be able to use the * or + registers (in Vim type :reg and see if these are in the list of registers to check for availability), then you can change your system's clipboard to be your default clipboard in Vim. I personally don't prefer this method as I actually enjoy having my Vim clipboards be separate from my system clipboard. But you might want to be able to just use the y key to paste whatever you have in your clipboard (like code from Stackoverflow) or c to copy whatever you want from Vim to paste into another program (like your code that you want to post on Stackoverflow) without pressing an extra key. This way, your copying and pasting habits in Vim function as you would expect them to relative to other program. Add the following line to your .vimrc for that behavior:

set clipboard=unnamedplus

Changing whitespace size based on file type

Many polyglot developers, or anyone who writes code in multiple different languages or works on a variety of projects, might want to have different whitespace settings for different file types. I write a lot of Python, so to follow PEP8 style compliance I typically use four-space tabs, but when I switch to writing JavaScript I prefer two-spaced tabs as there tends to be more code nesting.

You can configure Vim to behave differently when it comes to whitespace in different types of files. My default settings when it comes to whitespace are the following:

set tabstop=4 "Sets indent size of tabs"
set softtabstop=4 "Soft tabs"
set expandtab "Turns tabs into spaces"
set shiftwidth=4 "Sets auto-indent size"
set autoindent "Turns on auto-indenting"
set copyindent "Copy the previous indentation on autoindenting"
set smartindent "Remembers previous indent when creating new lines"

But to change those for specific file types, I added the following lines:

au BufNewFile,BufRead *.js,*.jsx*,*.rb,*.html,*.xml :setlocal sw=2 ts=2 sts=2
au FileType make setlocal noexpandtab

GIF displaying different whitespace settings for different file types

You can configure these to your liking depending on what kind of code you tend to work with.

Remapping colon to semicolon, for convenience

This one is fairly simple, yet polarizing. It might seem unimportant, but the : key is probably one of the most frequently used things in Vim. Remapping it to ; prevents you from having to constantly press the shift key. However, this overrides the default functionality of the ; key, which can be used to repeat your most recent f or t commands, something that some people use religiously and others not as often. To account for this, I also add a mapping to place that functionality behind the leader key as I use it far less often:

nnoremap <leader>; ;
map ; :

With this mapping, instead of having to press shift all the time to enter commands, you would be adding an extra keypress to the ; functionality. There already exist discussions about this remapping, with strong opinions on both sides. Your mileage may vary and you might have different preferences or habits than I do, but I personally love just hitting semicolon to enter commands.

Search settings and highlighting

Another thing that anybody who writes code does very often is search for phrases or keywords. It's a good idea to enable some settings to increase the usability of this behavior, such as adding highlighting to your search terms. In my .vimrc, this is what I have for my search settings:

set hlsearch "Highlights search terms"
set incsearch "Highlights search terms as you type them"
set showmatch "Highlights matching parentheses"
set ignorecase "Ignores case when searching"
set smartcase "Unless you put some caps in your search term"

I decided to use the return key because somebody on the internet suggested it, and I don't already use it for anything else. You can change this to fit your preferences. Here's a quick example of what it looks like. The highlighting is going away when I hit the enter key.

GIF showing search highlighting

:wq!

Hopefully you've found some of these interesting or helpful. There might be stuff in here that you disagree with because they might go against your habits, preferences, or personal Vim philosophy. So in any of those cases, I would suggest taking what you like and leaving the rest. Often, Vim users talk about these things as if there is an objectively "correct" way to do things, but I personally like to think of Vim in terms of a musical instrument. As a Vim enthusiast, I am always looking forward to hearing about more things I can add to my .vimrc. I don't consider myself an expert so there is a whole world of Vim sorcery to discover!

Feel free to contact me about some Vim tricks that you think are cool, ask questions about any of these, or want to express your undying anger at any of my suggestions: