<dot>vimrc file for vim users

2009-10-01 7 min read Linux Vim Tips

Example vimrc file below:

let g:VIM_CUSTOM = &#8221;/home/amitag/.vim_custom/&#8221;

&#8221; Don&#8217;t use vi compatibility; I want all the new features in Vim

set nocompatible

&#8221; Version 6.0-specific stuff

if version >= 600

syntax enable

filetype on

filetype plugin on

filetype indent on

else

syntax on

endif

set showfulltag &#8221; Get function usage help automatically

set showcmd &#8221; Show current vim command in status bar

set showmatch &#8221; Show matching parentheses/brackets

set showmode &#8221; Show current vim mode

set nu &#8221; Show Line number

set background=dark

&#8221; set tags=~/.tags

set bs=2 &#8221; allow backspacing over everything in insert mode

set viminfo=&#8217;20,&#8221;50 &#8221; read/write a .viminfo file, don&#8217;t store more

&#8221; than 50 lines of registers

set history=50 &#8221; keep 50 lines of command line history

set ruler &#8221; show the cursor position all the time

set nohlsearch &#8221; don&#8217;t highlight search matches

set selection=exclusive &#8221; don&#8217;t include character under cursor in selection

set incsearch &#8221; incremental (emacs-style) search

set wildmenu &#8221; use a scrollable menu for filename completions

set ignorecase &#8221; case-insensitive searching

&#8221; Indentation / tab replacement stuff

set shiftwidth=4 &#8221; > and < move block by 4 spaces in visual mode

set sts=4

set et &#8221; expand tabs to spaces

set autoindent &#8221; always set autoindenting on

&#8221; Color Scheme (only if GUI running) {{{

&#8221; if has(&#8221;gui_running&#8221;)

&#8221; colorscheme evening

&#8221; endif

&#8221; }}}

&#8221; Key mappings {{{

&#8221; Allow the . to execute once for each line in visual selection

vnoremap . :normal .

&#8221; Make &#8217; function behave like ` usually does and then change ` to replay

&#8221; recorded macro a (as if @a was typed). In visual mode, ` (which now acts

&#8221; like @a) should function on all selected lines.

noremap &#8217; `

nnoremap ` @a

vnoremap ` :normal @a

&#8221; Make tab perform keyword/tag completion if we&#8217;re not following whitespace

inoremap <c -r>=InsertTabWrapper()

&#8221; Make F7 spellcheck the buffer

noremap :call IspellCheck()

&#8221; Programming Keys:

&#8221; F9 = Make

&#8221; F10 = Next Error

&#8221; F11 = Prev Error

inoremap :make

inoremap :cnext

inoremap :cprev

noremap :make

noremap :cnext

noremap :cprev

&#8221; Buffer Switching:

&#8221; F2 = next buffer

&#8221; F3 = previous buffer

&#8221; F4 = kill buffer

&#8221; inoremap :bn

inoremap :bp

&#8221; inoremap :bd

&#8221; noremap :bn

noremap :bp

&#8221; noremap :bd

&#8221; Make p in Visual mode replace the selected text with the &#8221;&#8221; register.

vnoremap p :let current_reg = @&#8221;gvdi<c -R>=current_reg

&#8221; Key mappings }}}

&#8221; Autocommands {{{

if has(&#8221;autocmd&#8221;)

&#8221; When vim is used in a console window, set the title bar to the

&#8221; name of the buffer being editted.

if !has(&#8221;gui_running&#8221;)

auto BufEnter * let &titlestring=&#8221;VIM – &#8221;.expand(&#8221;%:p&#8221;)

endif

&#8221; In text and LaTeX files, always limit the width of text to 76

&#8221; characters. Also perform logical wrapping/indenting.

autocmd BufRead *.txt set tw=76 formatoptions=tcroqn2l

autocmd BufRead *.tex set tw=76

&#8221; Programming settings {{{

augroup prog

au!

au BufRead *.c,*.cc,*.cpp,*.h,*.java set formatoptions=croql cindent nowrap nofoldenable

au BufEnter *.java map <c -Return> :w\|:!javac %

au BufEnter *.c map <c -Return> :w\|:!gcc %

au BufEnter *.cc,*.cpp map <c -Return> :w\|:!g++ %

au BufLeave *.java,*.c,*.cc unmap <c -Return>

&#8221; Don&#8217;t expand tabs to spaces in Makefiles

au BufEnter [Mm]akefile* set noet

au BufLeave [Mm]akefile* set et

&#8221; Set up folding for python

au FileType python set nofoldenable foldmethod=indent

augroup END

&#8221; }}}

&#8221; Reread configuration of Vim if .vimrc is saved {{{

augroup VimConfig

au!

autocmd BufWritePost ~/.vimrc so ~/.vimrc

autocmd BufWritePost vimrc so ~/.vimrc

augroup END

&#8221; }}}

&#8221; &#8221; C programming auto commands {{{

augroup cprog

au!

&#8221;

&#8221; &#8221; When starting to edit a file:

&#8221; &#8221; For C and C++ files set formatting of comments and set C-indenting on.

&#8221; &#8221; For other files switch it off.

&#8221; &#8221; Don&#8217;t change the order, it&#8217;s important that the line with * comes first.

autocmd FileType * set formatoptions=tcql nocindent comments&

autocmd FileType c,cpp set formatoptions=croql comments=sr:/*,mb:*,el:*/,://

&#8221;

&#8221; &#8221; Automatic &#8221;folding&#8221; in C code. This is cool.

if version >= 600

&#8221;au FileType c set foldenable foldmethod=indent

au FileType c,cpp set nofoldenable foldmethod=syntax

au FileType c,cpp syn region Block start=&#8221;{&#8221; end=&#8221;}&#8221; transparent fold

&#8221;au FileType c syn region Comment start=&#8221;/\&#8221; end=&#8221;\/&#8221; fold

endif

augroup END

&#8221; &#8221; }}}

endif &#8221; has(&#8221;autocmd&#8221;)

&#8221; }}}

&#8221; Functions {{{

&#8221; IspellCheck() {{{

function! IspellCheck()

let l:tmpfile = tempname()

execute &#8221;normal:w!&#8221; . l:tmpfile . &#8221;\&#8221;

if has(&#8221;gui_running&#8221;)

execute &#8221;normal:!xterm -e ispell &#8221; . l:tmpfile . &#8221;\&#8221;

else

execute &#8221;normal:! ispell &#8221; . l:tmpfile . &#8221;\&#8221;

endif

execute &#8221;normal:%d\&#8221;

execute &#8221;normal:r &#8221; . l:tmpfile . &#8221;\&#8221;

execute &#8221;normal:1d\&#8221;

endfunction

&#8221; IspellCheck }}}

&#8221; InsertTabWrapper() {{{

&#8221; Tab completion of tags/keywords if not at the beginning of the

&#8221; line. Very slick.

function! InsertTabWrapper()

let col = col(&#8217;.&#8217;) – 1

if !col || getline(&#8217;.&#8217;)[col – 1] !~ &#8217;\k&#8217;

return &#8221;\&#8221;

else

return &#8221;\<c -p>&#8221;

endif

endfunction

&#8221; InsertTabWrapper() }}}

&#8221; Functions }}}

&#8221; Settings for specific syntax files {{{

let c_gnu=1

let c_comment_strings=1

let c_space_errors=1

&#8221;let perl_fold=1 &#8221; turn on perl folding capabilities

&#8221; }}}

&#8221; Modeline {{{

&#8221; vim:set ts=4:

&#8221; vim600:fdm=marker fdl=0 fdc=3 vb t_vb=:

&#8221; }}}

inoremap :wa

nnoremap :wa

vnoremap :wa

inoremap :if exists(&#8221;syntax_on&#8221;) syntax off else syntax on endif

ab aka Amit Agarwal

ab pr printf (&#8221;

ab epr \n&#8221; );

map :execute Clean_up() :execute Re_source(&#8221;c-vimrc&#8221;)

map :execute Clean_up()

&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;

&#8221; Custom functions &#8221;

&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;&#8221;

&#8221; Re-source the rc files

:function! Re_source(file_name)

let path_file_name = g:VIM_CUSTOM . a:file_name

if filereadable(path_file_name)

execute &#8217;source &#8217; . path_file_name

echo path_file_name . &#8221; Loaded sucessfully&#8221;

else

echo path_file_name . &#8221; does NOT exist&#8221;

return 0

endif

:endfunction

&#8221; This function allows me to quickly remove extra tabs and whitespace

&#8221; from the beginning of lines. This seems to be a problem when I cut

&#8221; and paste or when people don&#8217;t use resizeable tabs.

&#8221; TODO The only problem with this is after you execute it it jumps to the

&#8221; beginning of the file. I need to figure out how to fix that.

:function! Dump_extra_whitespace(rows)

let com = &#8221;.,+&#8221; . a:rows . &#8221;s/^[ ]*//g&#8221;

execute com

:endfunction

&#8221; This function was created by Dillon Jones (much better than my first attempt)

&#8221; it reverses the background color for switching between vim/gvim which have

&#8221; different defaults.

&#8221; TODO The only problem with this is after you execute it it jumps to the

&#8221; beginning of the file. I need to figure out how to fix that.

:function! ReverseBackground()

let Mysyn=&syntax

if &bg==&#8221;light&#8221;

se bg=dark

else

se bg=light

endif

syn on

exe &#8221;set syntax=&#8221; . Mysyn

&#8221;: echo &#8221;now syntax is &#8221;&syntax

:endfunction

&#8221; Cleanup

:function! Clean_up()

:set visualbell&

:set background&

:set tabstop&

:set showmatch&

:set showcmd&

:set autowrite&

:endfunction

&#8221;date/time abbreviations

iab mdyl <c -r>=strftime(&#8221;%a %d %b %Y&#8221;)

iab mdys <c -r>=strftime(&#8221;%y%m%d&#8221;)

iab mdyc <c -r>=strftime(&#8221;%c&#8221;)

iab hml <c -r>=strftime(&#8221;%d/%m/%y %H:%M:%S&#8221;)

iab hms <c -r>=strftime(&#8221;%H:%M:%S&#8221;)

set complete-=k complete+=k

set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words

&#8221;set dictionary-=/usr/share/dict/linux.words dictionary+=/usr/share/dict/linux.words

set complete-=k complete+=k

set sr fo=roqm1

im <c -B> <c -O>:setl sr! fo<c -R>=strpart(&#8221;-+&#8221;,&sr,1)=tc_

noremap :%!fmt -w 70 -cGA

noremap :! email_vim %

noremap :! email_vim_roamware %

imap <c -K> :%! perl -MText::Autoformat -e &#8221;{autoformat{justify=>&#8217;full&#8217;,right=>65, all=>1};}&#8221;

nmap <c -K> :%! perl -MText::Autoformat -e &#8221;{autoformat{justify=>&#8217;full&#8217;,right=>65, all=> 1};}&#8221;

vmap <c -K> :%! perl -MText::Autoformat -e &#8221;{autoformat{justify=>&#8217;full&#8217;,right=>65, all=> 1};}&#8221;

map :r! dateA

imap datesp :r! dateddA

imap pd if ($debug) {print __LINE__.&#8221;–» \n&#8221;;} hhhhhi

&#8221; Transparent editing of GnuPG-encrypted files

&#8221; Based on a solution by Wouter Hanegraaff

augroup encrypted

au!

&#8221; First make sure nothing is written to ~/.viminfo while editing

&#8221; an encrypted file.

autocmd BufReadPre,FileReadPre *.gpg,*.asc set viminfo=

&#8221; We don&#8217;t want a swap file, as it writes unencrypted data to disk.

autocmd BufReadPre,FileReadPre *.gpg,*.asc set noswapfile

&#8221; Switch to binary mode to read the encrypted file.

autocmd BufReadPre,FileReadPre *.gpg set bin

autocmd BufReadPre,FileReadPre *.gpg,*.asc let ch_save = &ch|set ch=2

autocmd BufReadPost,FileReadPost *.gpg,*.asc

\ '[,&#8217;]!sh -c &#8217;gpg –decrypt 2> /dev/null&#8217;

&#8221; Switch to normal mode for editing

autocmd BufReadPost,FileReadPost *.gpg set nobin

autocmd BufReadPost,FileReadPost *.gpg,*.asc let &ch = ch_save|unlet ch_save

autocmd BufReadPost,FileReadPost *.gpg,*.asc

\ execute &#8221;:doautocmd BufReadPost &#8221; . expand(&#8221;%:r&#8221;)

&#8221; Convert all text to encrypted text before writing

autocmd BufWritePre,FileWritePre *.gpg set bin

autocmd BufWritePre,FileWritePre *.gpg

\ '[,&#8217;]!sh -c &#8217;gpg –default-recipient-self -e 2>/dev/null&#8217;

autocmd BufWritePre,FileWritePre *.asc

\ '[,&#8217;]!sh -c &#8217;gpg –default-recipient-self -e -a 2>/dev/null&#8217;

&#8221; Undo the encryption so we are back in the normal text, directly

&#8221; after the file has been written.

autocmd BufWritePost,FileWritePost *.gpg,*.asc u

augroup END

map :rG

comments powered by Disqus