Dotfiles: Vim

I always use Vim as text editor (or vi, if is not installed). I love it because is lightweight, fast, never breaks and I'm now pretty proficient at it. I will describe a little bit my configuration file.

Basic config

This is the basic configuration section. Inline comments.

filetype plugin indent on
syntax on
set encoding=utf-8
set fileencoding=utf-8

set tabstop=4      "Number of spaces for Tab
set shiftwidth=4   "Number of spaces used for auto indent.
set noexpandtab    "Use tabs (if on) not spaces
set smarttab
set autoindent     " Autoindent de code. Use  for scape.
set number         " Show the numbers of the lines
set hidden
set history=1000
set undolevels=1000

set hlsearch       " Highlight the searches done
set incsearch      " Show the tokens that matches while typing a search command
set showmatch
set scrolloff=3    " Padding at the top/bottom of the screen when doing scroll

set wildmode=longest:list  " Configure how completion will behave
set mouse=n        " Set mouse to "normal mode"
let mapleader = "," " Change the  key from \ to ,

set cryptmethod=blowfish  # Set the encryption algorithm to blowfish when using "vim -x"
"set noswf         " Don't create .swf files

Mappings

These are very simple mappings that correct common typos when saving/closing a file. The way of reading this command is: when I press "WQ" as a command, please rewrite it automagically to "wq". Sometimes is a little bit unconvinient because it remaps all the "W" (uppercase) to "w" (lowercase) when you really meant W. In all the other cases it avoids the 98% of the errors when saving or savin and quitting.

cmap WQ wq
cmap Wq wq
cmap wQ wq
cmap W w
cmap Q q
cmap q1 q!

There are commands for being able to create new buffer, and move from one buffer to another in the case that the Control+NextPage or Control+PreviousPage don't work. That usually happens when you have more than one tab open in gnome-terminal (or equivalent) and the keybindings Control+{Next,Previous}Page are used for changing between the tabs of the console, not the vim buffers.

nmap <leader>n <Esc>:tabnew<CR>      " Create new tab
nmap <leader>[6~ <Esc>:tabnext<CR> " <leader><NextPage>
nmap <leader>[5~ <Esc>:tabprev<CR> " <leader><PreviousPage>

These one is a very useful one if you use ma for recording you current position and 'a for returning at the same line. If instead of using 'a you use `a you will not just return to the same line, but in the same position. The problem is that in a spanish keyboard the character ` is very difficult to use, and I'm remapping the ' to `

nnoremap ' `
nnoremap ` '

Keybindings

The keybindings are really really useful. I recommend you to configure as a keybinding the stuff you are using most. You will save a lot of time.

Saving the file

Enable two other ways of saving a file. The <Return> is required if you want the command to be executed inmediately.

nmap ññ :w <Return>
nmap <F8> :w <Return>

Plugins options

I encourage you to try the NerdTree plugin if you are not using already and the CommandT one. Both are for the same propouse, but with a different approach. NerdTree is a file browser, and CommantT lets you to writte some parts of the path and the name of the file and it will show you the possible results of what you are looking for. Very useful in some cases.

" NerdTree Options
map  <F2> :NERDTreeToggle <cr>   " Open NerdTree with F2
let NERDTreeIgnore=['.*pyc$']
" Command-t Options
map  <F3> :CommandT <cr>         " Open CommantT with F3...
map <leader>f :CommandT <cr>     " or LeaderKey + f

Trailing spaces/tabs

I hate with all my soul the trailing spaces and the trailing tabs. I think that the code is something that must be clean and easily editable and if you have 15 empty spaces at the end of a line, when I go to that line and press Fin key I end up in and the end of an infinite line, in the middle of f*cking nowhere.

That's why, most of the time when I'm writting code by my own I make my editor to highlight all this trailing spaces and delete them just when I see them. Sometimes I have to turn of the trailing spaces highlighter when reading code from other projects because the code is full trailing spaces (Trailing spaces. Trailing spaces everywhere), and my brain starts to hurt me.

Yes, I'm on medication. My Sysadmin Doctor tells me I will be fine and I'll recover soon, but if I don't feel better in a few weeks, he will proupose a harder way of solving mi problem like writting Lisp using just two fingers of my left hand, or helping people in Freenode's #php channel. I want to really thanks my fellow worker and friend Isaac, because he was the one who introduced me in the art of hating trailing spaces. Thanks Isaac.

set list
"set listchars=tab:…\ ,trail:·
set listchars=trail:·,tab:\ \        " This line end (paradogically) with a trailing space in order to work
"set listchars=trail:→,tab:\ \      " This line end (paradogically) with a trailing space in order to work

As you can see, there are different flavours of the way we highlight the trailing spaces.

Custom configuration

This is my custom configuration for some of file types or languages. As you can see in C, for example, the lines are hightlighted after the 78th charater. That's because in the university I cannot have lines larger than that. It can be useful sometimes.

aug foo_bar
au!
au FileType python setl expandtab
au FileType erlang setl expandtab
au BufWinEnter *.mako setl expandtab syntax=mako

" C++ lang
au BufNewFile,BufRead *.cc highlight OverLength cterm=underline
au BufNewFile,BufRead *.cc highlight OverLength ctermbg=DarkMagenta ctermfg=white guibg=#592929
au BufNewFile,BufRead *.cc match OverLength /\%78v.\+/
au BufNewFile,BufRead *.cc setl expandtab


" C lang
au BufNewFile,BufRead *.c highlight OverLength cterm=underline
au BufNewFile,BufRead *.c highlight OverLength ctermbg=DarkMagenta ctermfg=white guibg=#592929
au BufNewFile,BufRead *.c match OverLength /\%78v.\+/
au BufNewFile,BufRead *.c setl expandtab

" GO lang
au BufRead,BufNewFile *.go set filetype=go
aug END