Saturday, 13 February, 2021 UTC


Summary

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:
...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).
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, 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 
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 
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, and 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. 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" 
This is great, but it sometimes annoys me that the terms remain highlighted after you are finished. So I find it helpful to add a quick command to toggle that highlighting back off (and it automatically comes back on if you hit n or N to continue navigating the results). Here's a one-liner for getting rid of that highlighting when you're finished with your search: ```
nnoremap <CR> :noh<CR>
`
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.
:wq!
Hopefully you've found some of these interesting or helpful. 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, or ask questions about any of these:
  • Email: [email protected]
  • Twitter: @Sagnewshreds
  • Github: Sagnew
  • Twitch (streaming live code): Sagnewshreds