It’s time to blog about my .vimrc — maybe someone finds some useful lines in it. It has grown over the years, aggregated from various sources all over the nets, and also includes some stuff I figured out myself.

The first few lines adjust some default settings like line numbering, etc.

set number
set lbr
set fdc=0
set vb
set expandtab
set shiftwidth=2
set tabstop=2
set linespace=0
set autoindent
set smartindent
set mouse=a
set cursorline
syntax on
set showcmd

set autowrite
set noautochdir

The more interesting options are autowrite and autochdir. autowrite automatically saves everyting when you do a :make, and autochdir automatically sets the working directory to the directory where the file in the current buffer is. I disabled this though, because I got used to manually changing directories again.

Yes, I use space indentation where I have the choice.

Next are some key mappings:

nmap <silent> <c-n> :NERDTreeToggle<CR>

set hlsearch
map <silent> <c-k> :nohlsearch<CR>

map j gj
map k gk

The first mapping is for the NERDTree-Plugin, a little file browser for vim.

The hlsearch option activates the highlighting of all hits when you search for something with / or do a search and replace. When I press Ctrl-K, the highlighting is disabled. It automatically re-activates the next time I search for something though.

I mapped j and k to gj and gk respectively, so the cursor always goes straight up or down when I press k or j, even when I’m going through long lines which wrap at the right end of the screen.

The next part is commented out, but I’m including it anyway because I still think it’s a useful feature:

"set list
"set listchars=tab:»·,trail:·
"hi SpecialKey ctermfg=cyan

This shows little arrows for tabs and dots at the end of lines with trailing spaces.

The next part makes the status line at the bottom of the windows a bit more populated:

set laststatus=2    "make a status line above the command line. otherwise this won't work
set statusline=     "clear statusline
" set statusline+=%#comment#           "make the statusline comment-color
set statusline+=%-3.3n                      " buffer number
set statusline+=%f                          " file name
set statusline+=%h%m%r%w                     " flags
set statusline+=[%{strlen(&ft)?&ft:'none'},  " filetype
set statusline+=%{strlen(&fenc)?&fenc:&enc}, " encoding
set statusline+=%{&fileformat}]              " file format
set statusline+=%=                           " right align
set statusline+=%{synIDattr(synID(line('.'),col('.'),1),'name')}  " highlight
set statusline+=%b,0x%-8B                   " current char
set statusline+=%-14.(%l,%c%V%) %<%P        " offset

Here is my tabline:

function! MyTabLine()
  let s = ''
  let wn = ''
  let t = tabpagenr()
  let i = 1
  while i <= tabpagenr('$')
    let buflist = tabpagebuflist(i)
    let winnr = tabpagewinnr(i)
    let s .= '%' . i . 'T'
    let s .= (i == t ? '%1*' : '%2*')
    let s .= ' '
    let wn = tabpagewinnr(i,'$')

    let s .= (i== t ? '%#TabNumSel#' : '%#TabNum#')
    let s .= i
    if tabpagewinnr(i,'$') > 1
        let s .= '.'
        let s .= (i== t ? '%#TabWinNumSel#' : '%#TabWinNum#')
        let s .= (tabpagewinnr(i,'$') > 1 ? wn : '')
    end

    for j in range(tabpagewinnr(i,'$'))  " show a + if the tab contains modified buffers
      let bufmodified = getbufvar(buflist[j], "&mod")
      if bufmodified
        let s .= '+'
      endif
    endfor

    let s .= '%*'
    let s .= (i == t ? '%#TabLineSel#' : '%#TabLine#')
    let bufnr = buflist[winnr - 1]
    let file = bufname(bufnr)
    let buftype = getbufvar(bufnr, 'buftype')
    if buftype == 'nofile'
        if file =~ '/.'
            let file = substitute(file, '.*/ze.', '', '')
        endif
    else
        if i != t
          let file = fnamemodify(file, ':p:~:.:gs?([^/])[^/]*/?1/?') " shorten path name to first letter of each directory
        else
          let file = fnamemodify(file, ':p:~:.')
        endif
    endif
    if file == ''
        let file = '[none]'
    endif
    let s .= file
    " let s .= (i == t ? '%m' : '')
    let i = i + 1
  endwhile
  let s .= '%T%#TabLineFill#%='
  return s
endfunction

set showtabline=2 " always show tab line
set tabline=%!MyTabLine()

Most of the code for the tabline is from http://www.offensivethinking.org/data/dotfiles/vimrc.

It shows a nice tab line which shows tab numbers and the number of windows in a tab. I added the display of a path name which is shortened to the first letter of each directory when the tab is inactive. A also added the code that shows a little + in front of a tab name for each window in the tab which contains a modified buffer.

Next is more commented out code.

"set colorcolumn=80,120
"hi colorcolumn ctermbg=NONE ctermfg=red cterm=bold guibg=NONE guifg=red gui=bold

This highlights the 80th and 120th column of each line.

Since I’m mostly using gvim, except for quickly editing some config files, I also have a .gvimrc which sets guioptions=a.

I’m synchronizing these files on my netbook and desktop PC via Unison. Because I want to use different fonts and colorschemes on each computer, my .gvimrc ends with the line source ~/.gvimrc-local, and the .gvimrc-local, which is not synchronized, contains the font and colorscheme settings.

I hope my blog or your browser didn’t eat too many of the angle brackets and other special characters in the code fragments.