A Selection of Vim Options
A selection of Vim options that I like to use all the time and make my life so much easier when I go programming, writing prose or manipulating data.
There are more ways to set options with Vim than I can care to count. You can do so in the ~/.vimrc
file, plugins, modelines, but the options I'm showing here are more the kind you may want to change often depending on the situation, in which case you'll get to type the :set
command directly in the command line.
1 The Clipboard
The 'clipboard'
option is by default set to something looking like clipboard=autoselect,exclude:cons\|linux
, which I've never bothered to try and understand. What I do understand however is that it's very convenient at times to set it as follows:
:set clipboard=unnamed
What this does is to make the unnamed register (""
) the same as the "*
selection and drop register, more commonly known as the windowing system clipboard. And what this means is that all you'll subsequently yank will be implicitly stored into the clipboard, saving you from the need of typing "*
before y
to yank something into the clipboard.
2 Copying Over-Long Lines with Wrapping and Line-Breaking
Being a windowing system feature, using the clipboard may not necessarily work if you're using Vim e.g. in an SSH session, unless maybe you've used ssh -X
, which again you may not want to do when you're the type to open hundreds of windows.
Anyway, if for whichever reason you can't yank text into the clipboard and need to use the mouse, you'll have to be careful that:
- all your text is visible, which you can help ensure by wrapping long lines with
:set wrap
; - that words are non-breaking, which you can guarantee with
:set linebreak
lest some unwanted spaces might appear when you paste your text somewhere else.
3 Cursor Lines and Columns
I do love Python, don't get me wrong. But I never understood why the authors are so proud of not having curly braces. It's just a hassle when it comes to isolating blocks. No matter, Vim comes with a nifty option called 'cursorline'
and 'cursorcolumn'
to respectively draw a line and a column across the buffer, which you can use as a cross-hair.
4 Background Colours
Do you sometimes land on nodes with different Vim settings expecting different terminal background colours? Does syntax highlighting become unreadable because of this? You could of course muck about with the :colorscheme
command, but it's probably much easier to use the 'background'
option which does just the right thing:
:set background=dark
:set background=light
5 Disabling Wrap Scans
Using Vim to pore over log files is useful for its syntax highlighting and powerful regular expression system. What's annoying however is that repeatedly searching for a pattern with n
or N
makes you wrap around the file by default. There's a warning message whenever this happens but it's rather inconspicuous. It would be better if you could stop Vim wrapping at all, and that's possible by disabling the 'wrapscan'
option:
:set nowrapscan
6 Macros and Lazy Redraws
If you use macros to repeat the same operations on millions of lines, you might be interested in disabling redraws after each small operation, offering a performance boost. This is done by enabling the 'lazyredraw'
option. Don't forget however that you might often be better off performing substitutions with clever patterns.
7 Spelling Checker
As I was in the process of writing my thesis, it was a revelation when I discovered that Vim has a built-in spelling checker, which you can enable by turning it on and choosing a language:
:set spell
:set spelllang=en_gb
8 Folding
Folding is the art of temporarily hiding regions of text. There are tons of ways to do so. I quite like using the indent method, for e.g. Python; or the marker method, which lets you specify where folds start and end respectively with {{{
and }}}
markers. The fold method is set with the 'foldmethod'
option:
:set foldmethod=indent
:set foldmethod=marker
9 Virtual Editing
When working with tables in visual mode, it's convenient to be able to position the cursor anywhere in the buffer, even where there's no text yet. This is done by setting the 'virtualedit'
option as follows:
:set virtualedit=all
10 History
As I suggested earlier, these options my not be suitable all the time and you may need to turn them on and off quite often. Beyond showing the current values of options, toggling them and resetting them to their default values as is explained in the options.txt file, you may be interested in putting your command history to some good use. I've made mine very long with the 'history'
option:
:set history=10000
I also like working with it with the command-line window which is opened with the q:
command. It allows me to read, search, edit my history in the comfort of a regular Vim buffer.
One thing I still haven't been able to sort out yet it how to merge cleverly history when you have many Vim processes sharing the same ~/.viminfo
. What currently happens is that no matter how long your history is set up to be, a Vim process will always end up writing its history into ~/.viminfo
, discarding other Vim processes'. Food for thought.
11 Reference
The Vim documentation is all you'll ever need, even though it's true it's not always easy to see the forest for the trees. Reading tips and tricks from other people on the interwebs may give you an inkling as to what you're looking for.