Ivan Tomica

My Vim configuration

Although recently I’ve switched most of my workflow into the Emacs ? I still edit things here and there in Vim. Vim doesn’t have many nice and fancy features enabled by default, but that’s OK, I like it light.

But not so light… soo… this post will show configuration options I have stacked throughout the years by using Vim and explanation of some.

Recently to have NerdTree enabled I’ve added Pathogen which is loaded by

" Enable pathogen
execute pathogen#infect()

I don’t like to have backup and swap files laying around so I have following enabled:

set nobackup
set noswapfile

A bit stupid as same functionality in Emacs saved me few times already…

In order to know the position in the file ruler is likely required:

set ruler

Some prefer tab to be width 4, some 8. I’m in the first group:

set tabstop=4
set softtabstop=4
set shiftwidth=4

Although this kind of changes depending on the filetype:

" Set filetypes
autocmd BufRead,BufNewFile *.{yaml,yml} set filetype=yaml
autocmd BufRead,BufNewFile *.{json,json.j2} set filetype=json
autocmd BufRead,BufNewFile *.pp set filetype=puppet

" Configure filetypes
autocmd FileType yaml :setlocal shiftwidth=2 tabstop=2 softtabstop=2 expandtab ai
autocmd FileType json :setlocal shiftwidth=2 tabstop=2 softtabstop=2 ai
autocmd FileType puppet :setlocal tabstop=2 expandtab shiftwidth=2 softtabstop=2 ai

Otherwise programs utilizing those files will complain, and I don’t like that much. On that node Puppet really doesn’t like whitespace on the end of the line so:

" Strip whitespace
autocmd BufWritePre *.pp %s/\s\+$//e
autocmd BufEnter *.pp match ExtraWhitespace /\s\+$/

" Highlight whitespace
highlight ExtraWhitespace ctermbg=red guibg=red

While I edit the files I like to have things indented automatically, so I don’t wear out my tab key endlessly. Also I like to have all of my columns aligned while doing tab. Some kind of OCD I guess.

set autoindent
set copyindent
set shiftround

Once in a while I need to paste something in a file, and while doing so, input is due to indentation option pretty ugly and not really correct, which is bummer, especially in languages respecting indentation (looking at you ?). Luckily there is paste mode, but exiting insert mode, typing :set paste and entering insert mode again can be quite tedious, especially when after that you also need to do :set nopaste. Waste of time. Thus, to resolve this I’ve mapped toggle to F2 key:

set pt=<F2>

Vim can also adjust title with:

set title

Doing search can also be weird in Vim so I like to ignore case, except if I do first letter capital. I also like to see results immediately, nice and highlighted:

set ignorecase
set smartcase
set incsearch
set hlsearch

When I do some sequence that likes to break things I like to see it, but want to avoid constant pinging:

set visualbell
set noerrorbells

At last, syntax should be easily readable and I like to see relative numbers as it is easier to do prefix commands on the fly that way (instead of calculating number of lines between 2993 and 3014). Color scheme I use almost everywhere is Solarized, and sometimes I have transparent terminals.

syntax on
set number
set relativenumber
set cursorline
set background=dark
colorscheme solarized
hi Normal ctermbg=None

All those changes are more or less functional or graphical ones. I don’t do much hacking in keyboard shortcuts department, instead I do my best to learn them. What I in fact do is I disable arrow keys and use only hjkl combination

" Disable arrow keys
inoremap    <Up>    <NOP>
inoremap    <Down>  <NOP>
inoremap    <Left>  <NOP>
inoremap    <Right> <NOP>
noremap    <Up>    <NOP>
noremap    <Down>  <NOP>
noremap    <Left>  <NOP>
noremap    <Right> <NOP>

There is one thing I like to have and that is if I smash jk or kj in any order, it acts as Escape key

" Smash ESC
inoremap jk <Esc>
inoremap kj <Esc>

Tagged in:, , ,

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *