Alex Rivera | Logout

What is in your .vimrc?

Asked 2008-10-02T22:14:18.833
155

Vi and Vim allow for really awesome customization, typically stored inside a .vimrc file. Typical features for a programmer would be syntax highlighting, smart indenting and so on.

What other tricks for productive programming have you got, hidden in your .vimrc?

I am mostly interested in refactorings, auto classes and similar productivity macros, especially for C#.

Edit
Report

12 Answers

8

My heavily commented vimrc, with readline-esque (emacs) keybindings:

if version >= 700

"------ Meta ------"

" clear all autocommands! (this comment must be on its own line)
autocmd!

set nocompatible                " break away from old vi compatibility
set fileformats=unix,dos,mac    " support all three newline formats
set viminfo=                    " don't use or save viminfo files

"------ Console UI & Text display ------"

set cmdheight=1                 " explicitly set the height of the command line
set showcmd                     " Show (partial) command in status line.
set number                      " yay line numbers
set ruler                       " show current position at bottom
set noerrorbells                " don't whine
set visualbell t_vb=            " and don't make faces
set lazyredraw                  " don't redraw while in macros
set scrolloff=5                 " keep at least 5 lines around the cursor
set wrap                        " soft wrap long lines
set list                        " show invisible characters
set listchars=tab:>·,trail:·    " but only show tabs and trailing whitespace
set report=0                    " report back on all changes
set shortmess=atI               " shorten messages and don't show intro
set wildmenu                    " turn on wild menu :e <Tab>
set wildmode=list:longest       " set wildmenu to list choice
if has('syntax')
    syntax on
    " Remember that rxvt-unicode has 88 colors by default; enable this only if
    " you are using the 256-color patch
    if &term == 'rxvt-unicode'
        set t_Co=256
    endif

    if &t_Co == 256
        colorscheme xoria256
    else
        colorscheme peachpuff
    endif
endif

"------ Text editing and searching behavior ------"

set nohlsearch                  " turn off highlighting for searched expressions
set incsearch                   " highlight as we search however
set matchtime=5                 " blink matching c
answered 2009-10-28T19:04:04.577
6

Some fixes for common typos have saved me a surprising amount of time:

:command WQ wq
:command Wq wq
:command W w
:command Q q

iab anf and
iab adn and
iab ans and
iab teh the
iab thre there
answered 2008-10-03T12:44:37.817
4

I use cscope from within vim (making great use of the multiple buffers). I use control-K to initiate most of the commands (stolen from ctags as I recall). Also, I've already generated the .cscope.out file.

if has("cscope")

set cscopeprg=/usr/local/bin/cscope
set cscopetagorder=0
set cscopetag
set cscopepathcomp=3
set nocscopeverbose
cs add .cscope.out
set csverb

"
" cscope find
"
" 0 or s: Find this C symbol
" 1 or d: Find this definition
" 2 or g: Find functions called by this function
" 3 or c: Find functions calling this function
" 4 or t: Find assignments to
" 6 or e: Find this egrep pattern
" 7 or f: Find this file
" 8 or i: Find files #including this file
" 
map ^Ks     :cs find 0 <C-R>=expand("<cword>")<CR><CR>
map ^Kd     :cs find 1 <C-R>=expand("<cword>")<CR><CR>
map ^Kg     :cs find 2 <C-R>=expand("<cword>")<CR><CR>
map ^Kc     :cs find 3 <C-R>=expand("<cword>")<CR><CR>
map ^Kt     :cs find 4 <C-R>=expand("<cword>")<CR><CR>
map ^Ke     :cs find 6 <C-R>=expand("<cword>")<CR><CR>
map ^Kf     :cs find 7 <C-R>=expand("<cfile>")<CR><CR>
map ^Ki     :cs find 8 <C-R>=expand("%")<CR><CR>

endif

answered 2008-10-03T00:38:28.330
3
set tabstop=4 softtabstop=4 shiftwidth=4 expandtab autoindent cindent 
set encoding=utf-8 fileencoding=utf-8
set nobackup nowritebackup noswapfile autoread
set number
set hlsearch incsearch ignorecase smartcase

if has("gui_running")
    set lines=35 columns=140
    colorscheme ir_black
else
    colorscheme darkblue
endif

" bash like auto-completion
set wildmenu
set wildmode=list:longest

inoremap <C-j> <Esc>

" for lusty explorer
noremap glr \lr
noremap glf \lf
noremap glb \lb

" use ctrl-h/j/k/l to switch between splits
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h

" Nerd tree stuff
let NERDTreeIgnore = ['\.pyc$', '\.pyo$']
noremap gn :NERDTree<Cr>

" cd to the current file's directory
noremap gc :lcd %:h<Cr>
answered 2009-10-28T12:37:19.670
1

What's in my .vimrc?

ngn@macavity:~$ cat .vimrc
" This file intentionally left blank

The real config files lie under ~/.vim/ :)

And most of the stuff there is parasiting on other people's efforts, blatantly adapted from vim.org to my editing advantage.

answered 2008-11-02T20:57:59.327
1

This is my humble .vimrc. It is a work in progress (As it should always be), so forgive the messy layout and the commented out lines.

" =====Key mapping
" Insert empty line.
nmap <A-o> o<ESC>k
nmap <A-O> O<ESC>j
" Insert one character.
nmap <A-i> i <Esc>r
nmap <A-a> a <Esc>r
" Move on display lines in normal and visual mode.
nnoremap j gj
nnoremap k gk
vnoremap j gj
vnoremap k gk
" Do not lose * register when pasting on visual selection.
vmap p "zxP
" Clear highlight search results with <esc>
nnoremap <esc> :noh<return><esc>
" Center screen on next/previous selection.
map n nzz
map N Nzz
" <Esc> with jj.
inoremap jj <Esc>
" Switch jump to mark.
nnoremap ' `
nnoremap ` '
" Last and next jump should center too.
nnoremap <C-o> <C-o>zz
nnoremap <C-i> <C-i>zz
" Paste on new line.
nnoremap <A-p> :pu<CR>
nnoremap <A-S-p> :pu!<CR>
" Quick paste on insert mode.
inoremap <C-F> <C-R>"
" Indent cursor on empty line.
nnoremap <A-c> ddO
nnoremap <leader>c ddO
" Save and quit quickly.
nnoremap <leader>s :w<CR>
nnoremap <leader>q :q<CR>
nnoremap <leader>Q :q!<CR>
" The way it should have been.
noremap Y y$
" Moving in buffers.
nnoremap <C-S-tab> :bprev<CR>
nnoremap <C-tab> :bnext<CR>
" Using bufkill plugin.
nnoremap <leader>b :BD<CR>
nnoremap <leader>B :BD!<CR>
nnoremap <leader>ZZ :w<CR>:BD<CR>
" Moving and resizing in windows.
nnoremap + <C-W>+
nnoremap _ <C-W>-
nnoremap <C-h> <C-w>h
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
nnoremap <leader>w <C-w>c
" Moving in tabs
noremap <c-right> gt
noremap <c-left> gT
nnoremap <leader>t :tabc<CR>
" Moving around in insert mode.
inoremap <A-j> <
answered 2009-10-28T18:52:23.930
1
    " insert change log in files
    fun! InsertChangeLog()
    let l:flag=0
    for i in range(1,5)
        if getline(i) !~ '.*Last Change.*'
            let l:flag = l:flag + 1
        endif
    endfor
    if l:flag >= 5
        normal(1G)
        call append(0, "File: <+Description+>")
        call append(1, "Created: " . strftime("%a %d/%b/%Y hs %H:%M"))
        call append(2, "Last Change: " . strftime("%a %d/%b/%Y hs %H:%M"))
        call append(3, "author: <+your name+>")
        call append(4, "site: <+site+>")
        call append(5, "twitter: <+your twitter here+>")
        normal gg
    endif
endfun
map <special> <F4> <esc>:call InsertChangeLog()<cr>

" update changefile log
" http://tech.groups.yahoo.com/group/vim/message/51005
fun! LastChange()
    let _s=@/
    let l = line(".")
    let c = col(".")
    if line("$") >= 5
        1,5s/\s*Last Change:\s*\zs.*/\="" . strftime("%Y %b %d %X")/ge
    endif
    let @/=_s
    call cursor(l, c)
endfun
autocmd BufWritePre * keepjumps call LastChange()

    function! JumpToNextPlaceholder()
    let old_query = getreg('/')
    echo search("<+.\\++>")
    exec "norm! c/+>/e\<CR>"
    call setreg('/', old_query)
endfunction
autocmd BufNewFile * :call LoadFileTemplate()
nnoremap <special> <leader>j :call JumpToNextPlaceholder()<CR>a
inoremap <special> <leader>j <ESC>:call JumpToNextPlaceholder()<CR>a

" Cientific calculator
command! -nargs=+ Calc :py print <args>
py from math import *
map ,c :Calc


set statusline=%F%m%r%h%w\
\ ft:%{&ft}\ \
\ff:%{&ff}\ \
\%{strftime(\"%a\ %d/%m/%Y\ \
\%H:%M:%S\",getftime(expand(\"%:p\")))}%=\ \
\buf:%n\ \
\L:%04l\ C:%04v\ \
\T:%04L\ HEX:%03.3B\ ASCII:%03.3b\ %P
set laststatus=2 " Always show statusline
answered 2010-11-16T23:22:49.067
1

This isnt in my .vimrc but its related.

In your .bashrc add this line alias :q=exit

Its amazing how much I have used this, sometimes without even realising!

answered 2011-03-20T11:10:29.570
0

http://github.com/elventails/vim/blob/master/vimrc

Has extras for CakePHP/PHP/Git

enjoy!

Will add nice options you guys are using to it and update the repo;

Cheers,

answered 2009-10-29T20:00:40.327
0

I can't live without TAB Completion

" Intelligent tab completion
inoremap <silent> <Tab> <C-r>=<SID>InsertTabWrapper(1)<CR>
inoremap <silent> <S-Tab> <C-r>=<SID>InsertTabWrapper(-1)<CR>

function! <SID>InsertTabWrapper(direction)
    let idx = col('.') - 1
    let str = getline('.')

    if a:direction > 0 && idx >= 2 && str[idx - 1] == ' '
                \&& str[idx - 2] =~? '[a-z]'
        if &softtabstop && idx % &softtabstop == 0
            return "\<BS>\<Tab>\<Tab>"
        else
            return "\<BS>\<Tab>"
        endif
    elseif idx == 0 || str[idx - 1] !~? '[a-z]'
        return "\<Tab>"
    elseif a:direction > 0
        return "\<C-p>"
    else
        return "\<C-n>"
    endif
endfunction
answered 2009-11-20T21:16:38.927
0

My favorite bit of my .vimrc is a set of mappings for working with macros:

nnoremap <Leader>qa mqGo<Esc>"ap
nnoremap <Leader>qb mqGo<Esc>"bp
nnoremap <Leader>qc mqGo<Esc>"cp
<SNIP>
nnoremap <Leader>qz mqGo<Esc>"zp

nnoremap <Leader>Qa G0"ad$dd'q
nnoremap <Leader>Qb G0"bd$dd'q
nnoremap <Leader>Qc G0"cd$dd'q
<SNIP>
nnoremap <Leader>Qz G0"zd$dd'q

With this \q[a-z] will mark your location, and print the contents of the given register at the bottom of the current file and \Q[a-z] will put the contents of the last line into the given register and go back to your marked location. Makes it really easy to edit a macro or copy and tweak one macro into a new register.

answered 2010-04-15T20:16:52.050
0

I have this in my ~/.vim/after/syntax/vim.vim file:

What it does is:

  • highlights the word blue in the color blue
  • highlights the wrod red in the color red
  • etc

Ie: so if you go:

highlight JakeRedKeywords cterm=bold term=bold ctermbg=black ctermfg=Red

The word red will be red and the word black will be black.

Here is the code:

syn cluster vimHiCtermColors contains=vimHiCtermColorBlack,vimHiCtermColorBlue,vimHiCtermColorBrown,vimHiCtermColorCyan,vimHiCtermColorDarkBlue,vimHiCtermColorDarkcyan,vimHiCtermColorDarkgray,vimHiCtermColorDarkgreen,vimHiCtermColorDarkgrey,vimHiCtermColorDarkmagenta,vimHiCtermColorDarkred,vimHiCtermColorDarkyellow,vimHiCtermColorGray,vimHiCtermColorGreen,vimHiCtermColorGrey,vimHiCtermColorLightblue,vimHiCtermColorLightcyan,vimHiCtermColorLightgray,vimHiCtermColorLightgreen,vimHiCtermColorLightgrey,vimHiCtermColorLightmagenta,vimHiCtermColorLightred,vimHiCtermColorMagenta,vimHiCtermColorRed,vimHiCtermColorWhite,vimHiCtermColorYellow

syn case ignore

syn keyword vimHiCtermColorYellow yellow contained 
syn keyword vimHiCtermColorBlack black contained
syn keyword vimHiCtermColorBlue blue contained
syn keyword vimHiCtermColorBrown brown contained
syn keyword vimHiCtermColorCyan cyan contained
syn keyword vimHiCtermColorDarkBlue darkBlue contained
syn keyword vimHiCtermColorDarkcyan darkcyan contained
syn keyword vimHiCtermColorDarkgray darkgray contained
syn keyword vimHiCtermColorDarkgreen darkgreen contained
syn keyword vimHiCtermColorDarkgrey darkgrey contained
syn keyword vimHiCtermColorDarkmagenta darkmagenta contained
syn keyword vimHiCtermColorDarkred darkred contained
syn keyword vimHiCtermColorDarkyellow darkyellow contained
syn keyword vimHiCtermColorGray gray contained
syn keyword vimHiCtermColorGreen green contained
syn keyword vimHiCtermColorGrey grey contained
syn keyword vimHiCtermColorLightblue lightblue c
answered 2010-05-15T17:34:48.383

Your Answer