<dot>vimrc file for vim users
Example vimrc file below:
let g:VIM_CUSTOM = ”/home/amitag/.vim_custom/”
” Don’t use vi compatibility; I want all the new features in Vim
set nocompatible
” Version 6.0-specific stuff
if version >= 600
syntax enable
filetype on
filetype plugin on
filetype indent on
else
syntax on
endif
set showfulltag ” Get function usage help automatically
set showcmd ” Show current vim command in status bar
set showmatch ” Show matching parentheses/brackets
set showmode ” Show current vim mode
set nu ” Show Line number
set background=dark
” set tags=~/.tags
set bs=2 ” allow backspacing over everything in insert mode
set viminfo=’20,”50 ” read/write a .viminfo file, don’t store more
” than 50 lines of registers
set history=50 ” keep 50 lines of command line history
set ruler ” show the cursor position all the time
set nohlsearch ” don’t highlight search matches
set selection=exclusive ” don’t include character under cursor in selection
set incsearch ” incremental (emacs-style) search
set wildmenu ” use a scrollable menu for filename completions
set ignorecase ” case-insensitive searching
” Indentation / tab replacement stuff
set shiftwidth=4 ” > and < move block by 4 spaces in visual mode
set sts=4
set et ” expand tabs to spaces
set autoindent ” always set autoindenting on
” Color Scheme (only if GUI running) {{{
” if has(”gui_running”)
” colorscheme evening
” endif
” }}}
” Key mappings {{{
” Allow the . to execute once for each line in visual selection
vnoremap . :normal .
” Make ’ function behave like ` usually does and then change ` to replay
” recorded macro a (as if @a was typed). In visual mode, ` (which now acts
” like @a) should function on all selected lines.
noremap ’ `
nnoremap ` @a
vnoremap ` :normal @a
” Make tab perform keyword/tag completion if we’re not following whitespace
inoremap
” Make F7 spellcheck the buffer
noremap
” Programming Keys:
” F9 = Make
” F10 = Next Error
” F11 = Prev Error
inoremap
inoremap
inoremap
noremap
noremap
noremap
” Buffer Switching:
” F2 = next buffer
” F3 = previous buffer
” F4 = kill buffer
” inoremap
inoremap
” inoremap
” noremap
noremap
” noremap
” Make p in Visual mode replace the selected text with the ”” register.
vnoremap p
” Key mappings }}}
” Autocommands {{{
if has(”autocmd”)
” When vim is used in a console window, set the title bar to the
” name of the buffer being editted.
if !has(”gui_running”)
auto BufEnter * let &titlestring=”VIM – ”.expand(”%:p”)
endif
” In text and LaTeX files, always limit the width of text to 76
” characters. Also perform logical wrapping/indenting.
autocmd BufRead *.txt set tw=76 formatoptions=tcroqn2l
autocmd BufRead *.tex set tw=76
” 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>
” Don’t expand tabs to spaces in Makefiles
au BufEnter [Mm]akefile* set noet
au BufLeave [Mm]akefile* set et
” Set up folding for python
au FileType python set nofoldenable foldmethod=indent
augroup END
” }}}
” Reread configuration of Vim if .vimrc is saved {{{
augroup VimConfig
au!
autocmd BufWritePost ~/.vimrc so ~/.vimrc
autocmd BufWritePost vimrc so ~/.vimrc
augroup END
” }}}
” ” C programming auto commands {{{
augroup cprog
au!
”
” ” When starting to edit a file:
” ” For C and C++ files set formatting of comments and set C-indenting on.
” ” For other files switch it off.
” ” Don’t change the order, it’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:*/,://
”
” ” Automatic ”folding” in C code. This is cool.
if version >= 600
”au FileType c set foldenable foldmethod=indent
au FileType c,cpp set nofoldenable foldmethod=syntax
au FileType c,cpp syn region Block start=”{” end=”}” transparent fold
”au FileType c syn region Comment start=”/\” end=”\/” fold
endif
augroup END
” ” }}}
endif ” has(”autocmd”)
” }}}
” Functions {{{
” IspellCheck() {{{
function! IspellCheck()
let l:tmpfile = tempname()
execute ”normal:w!” . l:tmpfile . ”\
if has(”gui_running”)
execute ”normal:!xterm -e ispell ” . l:tmpfile . ”\
else
execute ”normal:! ispell ” . l:tmpfile . ”\
endif
execute ”normal:%d\
execute ”normal:r ” . l:tmpfile . ”\
execute ”normal:1d\
endfunction
” IspellCheck }}}
” InsertTabWrapper() {{{
” Tab completion of tags/keywords if not at the beginning of the
” line. Very slick.
function! InsertTabWrapper()
let col = col(’.’) – 1
if !col || getline(’.’)[col – 1] !~ ’\k’
return ”\
else
return ”\<c -p>”
endif
endfunction
” InsertTabWrapper() }}}
” Functions }}}
” Settings for specific syntax files {{{
let c_gnu=1
let c_comment_strings=1
let c_space_errors=1
”let perl_fold=1 ” turn on perl folding capabilities
” }}}
” Modeline {{{
” vim:set ts=4:
” vim600:fdm=marker fdl=0 fdc=3 vb t_vb=:
” }}}
inoremap
nnoremap
vnoremap
inoremap
ab aka Amit Agarwal
ab pr printf (”
ab epr \n” );
map
map
””””””””””””””””””””””””””””””””””””””””””””””””””
” Custom functions ”
””””””””””””””””””””””””””””””””””””””””””””””””””
” 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 ’source ’ . path_file_name
-
echo path_file_name . ” Loaded sucessfully”
-
else
-
echo path_file_name . ” does NOT exist”
-
return 0
-
endif
:endfunction
” This function allows me to quickly remove extra tabs and whitespace
” from the beginning of lines. This seems to be a problem when I cut
” and paste or when people don’t use resizeable tabs.
” TODO The only problem with this is after you execute it it jumps to the
” beginning of the file. I need to figure out how to fix that.
- :function! Dump_extra_whitespace(rows)
-
let com = ”.,+” . a:rows . ”s/^[ ]*//g”
-
execute com
:endfunction
” This function was created by Dillon Jones (much better than my first attempt)
” it reverses the background color for switching between vim/gvim which have
” different defaults.
” TODO The only problem with this is after you execute it it jumps to the
” beginning of the file. I need to figure out how to fix that.
- :function! ReverseBackground()
-
let Mysyn=&syntax
-
if &bg==”light”
-
se bg=dark
-
else
-
se bg=light
-
endif
-
syn on
-
exe ”set syntax=” . Mysyn
”: echo ”now syntax is ”&syntax
:endfunction
” Cleanup
:function! Clean_up()
:set visualbell&
:set background&
:set tabstop&
:set showmatch&
:set showcmd&
:set autowrite&
:endfunction
”date/time abbreviations
iab mdyl <c -r>=strftime(”%a %d %b %Y”)
iab mdys <c -r>=strftime(”%y%m%d”)
iab mdyc <c -r>=strftime(”%c”)
iab hml <c -r>=strftime(”%d/%m/%y %H:%M:%S”)
iab hms <c -r>=strftime(”%H:%M:%S”)
set complete-=k complete+=k
set dictionary-=/usr/share/dict/words dictionary+=/usr/share/dict/words
”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(”-+”,&sr,1)
noremap
noremap
noremap
imap <c -K>
nmap <c -K> :%! perl -MText::Autoformat -e ”{autoformat{justify=>’full’,right=>65, all=> 1};}”
vmap <c -K> :%! perl -MText::Autoformat -e ”{autoformat{justify=>’full’,right=>65, all=> 1};}”
map
imap datesp
imap pd if ($debug) {print __LINE__.”–» \n”;}
” Transparent editing of GnuPG-encrypted files
” Based on a solution by Wouter Hanegraaff
augroup encrypted
au!
” First make sure nothing is written to ~/.viminfo while editing
” an encrypted file.
autocmd BufReadPre,FileReadPre *.gpg,*.asc set viminfo=
” We don’t want a swap file, as it writes unencrypted data to disk.
autocmd BufReadPre,FileReadPre *.gpg,*.asc set noswapfile
” 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
\ '[,’]!sh -c ’gpg –decrypt 2> /dev/null’
” 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 ”:doautocmd BufReadPost ” . expand(”%:r”)
” Convert all text to encrypted text before writing
autocmd BufWritePre,FileWritePre *.gpg set bin
autocmd BufWritePre,FileWritePre *.gpg
\ '[,’]!sh -c ’gpg –default-recipient-self -e 2>/dev/null’
autocmd BufWritePre,FileWritePre *.asc
\ '[,’]!sh -c ’gpg –default-recipient-self -e -a 2>/dev/null’
” Undo the encryption so we are back in the normal text, directly
” after the file has been written.
autocmd BufWritePost,FileWritePost *.gpg,*.asc u
augroup END
map
Authored By Amit Agarwal
Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.