< My .vimrc File
Although I've come to be a big fan of Microsoft's VS Code, there are always reasons why I may have to temporarily drop back to using the classic vim editor. Over the years I've collected a few helpful vim configuration directives that make life a little easier for me.
You store your vim directives in a file called .vimrc that usually sits in your home folder (~/).
Here's my .vimrc file with an explanation of what each line does:
My .vimrc File
:set tabstop=2 shiftwidth=2 expandtab :set hlsearch :set cursorline :set number :set autoindent :set syntax=enable :set backupdir=~/.vim/tmp// :set directory=~/.vim/tmp// :set undodir=~/.vim/tmp// :set foldmethod=syntax :set foldnestmax=10 :set foldlevel=2
Maybe before we explain what each line does, here's the result of the file when running vim:
So, what's notable here?
- A line number gutter along the left side of the screen
- The line of code that my cursor is on is underlined
- Tabs are set to 2 spaces
- (not fantastic) syntax highlighting
- vim backups are put in ~/.vim/tmp
- (not fantastic) syntax folding
Now let's get into the directives.
Setting Tabs, Tabstops and Tab-Conversion
:set tabstop=2 shiftwidth=2 expandtab
Firstly, there's the expandtab directive. This tells VIM to replace a single TAB-key with spaces.
This single directive probably yields the greatest sanity when you're opening someone else's work.
"Tab stops" define how far the cursor indents when you press the TAB-key on your keyboard. They define the 'width', in columns, of a single tab character.
So, when VIM runs into TAB characters (\t), it will render those TABs as the number of spaces defined in this setting.
It seems that, by default, vim's tab setting is something extremely huge. I think it is 8 spaces. This appears to me to be a big waste of screen-space: pushing your code in vast swaths of eastward movement until it finally falls off your screen into oblivion.
True, vim wraps lines but this leads to an even uglier and confusing experience for me personally. Having 8-space tabs only quickens the descent into the eastern nether-land of confusion and ugliness.
Setting tabs to 2 spaces makes for a much neater experience for my taste.
"Shift width" defines how many spaces VIM will indent text.
So, what's the difference between shiftwidth and tabstop?
shiftwidth is used when (auto) indenting. For example, when you grab text with 'v' (visual mode), then indent the text with '>', shiftwidth defines how many spaces the indent will be.
There's nothing worse than reading someone else's code where their tabs are all over the map. The above settings will corral all of those ugly tabs into at least something consistent.
When you search for something in VIM its nice to see the results highlighted. The below incantation will do just that:
Underline Your Current Line
As I move around in some code I like seeing my current line clearly indicated. Use cursorline to do this:
Now, your current line will be highlighted. I admit this setting, like all of them in this post, may not be for everyone!
Enable Line Numbers in the Left Margin
Have VIM Try to AutoIndent Your Code
I'm sure this setting is standard for Python programmers.
Have VIM Try to Do Syntax Highlighting
Put VIM's Work Files Elsewhere
:set backupdir=~/.vim/tmp// :set directory=~/.vim/tmp// :set undodir=~/.vim/tmp//
I seem to recall having some experiences where code-watchers, like Nodemon will accidentally trigger a restart in my Nodejs code while I'm editing. There have also been circumstances where my VIM session would disconnect (let's say I was working through a particularly spotty SSH connection). When that happens VIM usually leaves behind a little mess.
One solution to issues like this is telling VIM to put its work-files in another location. The above commands will put VIM's temp files in a directory under ~/.vim.
:set foldmethod=syntax :set foldnestmax=10 :set foldlevel=2
Ok, the above has not worked 100% to my liking. I'm still searching for the right setup to reliably get code-folding working right.
One of the best sources for vim tips is the Vim Tips Wiki.