Wednesday, October 08, 2008

Basic .vimrc file



My first Vim configuration files: .vimrc, plugins, color schemes, etc were very small and modest. It's been eight years since I started using Vim and these configuration files have morphed, evolved, grown and recently shrink in too many ways, more than I can remember, and every time I check www.vim.org I find new ways I would like my Vim to evolve even further.

Here is the simplest version of my .vimrc file with no ctags or desktop integration as these topics require a separate post to explain. Most of the options here can be understood directly from Vim help files and by the comments in the file itself.

If you would like to use this file make sure to copy it in your home directory and read the comments as to get full functionality.

For comparison see the default install and my older configuration screenshot at the end of the post and compare it with my current configuration screenshot at the top of this post.



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" General Vim Settings

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"   In Kubuntu/Ubuntu make sure you have a complete vim install

"   i.e.  sudo aptitude install vim-ruby vim-scripts vim-common

"



" Disable compatibility with vi

set nocompatible



" Use the Unix file format

set fileformats=unix,dos



" Display line number in front of each line in the left margin.

set number



" I keep my own backups and all those files ending in '~' are

" anoying.

set nobackup



" Display commands in the bottom right corner as they are typed.

set showcmd



" Smoother redraws

set ttyfast



" Avoid vim complains about not written file when jumping

" between buffers using ctags.

set autowrite



" Vim Tip #1160 - Auto Save files when focus is lost.

au FocusLost * :wa



" Vim Tip #1279 - Highlight current line in Insert Mode

autocmd InsertLeave * se nocul

autocmd InsertEnter * se cul



" Enable syntax highlighting with 256 color support so we

" can use nicer color schemes. For more information and to

" get some nice color schemes check:

"   http://www.frexx.de/xterm-256-notes/

" Tested with Konsole-2.1 and yakuake 2.9.4

set t_Co=256

syntax on



" Set this background depending on your console and color scheme.

set background=light

"set background=dark



" My favorite color schemes. You must download the color themes

" and put them inside the color folder of your vim installation.

"colors default     " Good fro transparent consoles

"colors zenburn     " Vim Tip #415

colors 256_asu1dark " http://www.frexx.de/xterm-256-notes/

"colors pyte        " Vim Tip #1492



" If in diff mode (vimdiff) use the inkpot color scheme

" that better highlights file differences

if &diff

  colors inkpot    " Vim Tip #1143

endif



" Additional filetypes

au BufRead,BufNewFile sconstruct setlocal filetype=python

au BufRead,BufNewFile *.rl       setlocal filetype=ragel

au BufRead,BufNewFile *.rb       setlocal filetype=ruby



" Additional Syntax Files. The ragel.vim file can be downloaded

" from Ragel's home page http://www.complang.org/ragel/.

au! Syntax ragel source ragel.vim



" Set path to search (i.e. gf) files recursively

set path=/usr/include,/usr/local/include,**;$HOME



" Vim Tip #1274 - Highlight trailing whitespace characters

set list

set listchars=tab:->,trail:·



" Number of spaces used for (auto)indenting

"set shiftwidth=2



" Number of spaces to insert for a tab

"set softtabstop=2

set tabstop=2



" Insert spaces when the tab key is pressed

" If you want a real tab use "ctrl-v, tab" in insert mode. This

" is usefull for Makefiles that require real tabs to work.

set expandtab



" Word wrap with line breaks if they are longer than 80 characters

" long. If you prefer to wrap lines visually only use the wrap and

" lbr commands below instead. If you have a document that has lines

" longer than 80 characters you can use "gq}" to format it as a

" paragraph. This is more easier format for navigating the text.

set textwidth=80

set formatoptions=tcq



" Vim Tip #989: Word wrap without line breaks

"set wrap

"set lbr



" Enable specific indenting

set autoindent

set smartindent



" Enable code folding

set foldmethod=syntax



" Show briefly matching bracket when closing it.

set showmatch



" Show cursor position

set ruler



" Additonal key mappings

map <F12> <ESC>ggVGg?                             " ROT13 fun



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" Mini buffer explorer features

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

let g:miniBufExplSplitBelow=0

let g:miniBufExplSplitToEdge = 0

let g:miniBufExplVSplit = 20

let g:miniBufExplMapCTabSwitchBufs = 1

let g:miniBufExplUseSingleClick = 1

let g:miniBufExplorerMoreThanOne=0 

let g:miniBufExplModSelTarget = 1



" Map Tab and Shift-Tab for buffer navigation

" Note that S-TAB does not work in certain consoles (i.e. KDE Konsole)

map <TAB> <ESC><C-TAB>

map <S-TAB> <ESC><C-S-TAB>



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"" Improve VIM autocomplete features

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Enable search as you type:

set incsearch



"improve autocomplete menu color

highlight Pmenu ctermbg=238 gui=bold



" http://www.cuberick.com/2008/10/ruby-autocomplete-in-vim.html

autocmd FileType ruby,eruby set omnifunc=rubycomplete#Complete

autocmd FileType ruby,eruby let g:rubycomplete_buffer_loading = 1

autocmd FileType ruby,eruby let g:rubycomplete_rails = 1

autocmd FileType ruby,eruby let g:rubycomplete_classes_in_global = 1



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Vim tip #90 Enable VCS integration in vim

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

"  Get vcscommand.zip from http://www.vim.org/scripts/script.php?script_id=90

"  Uncompress to your vim directory:  unzip -d ~/.vim vcscommand.zip

"

"  Now when editing a file that is under revision control (SVN, CVS, Git,...)

"  we can access the versioning commands:

"

"  VCSAdd

"  VCSAnnotate

"  VCSCommit

"  VCSDelete

"  VCSDiff

"  VCSGotoOriginal

"  VCSGotoOriginal!

"  VCSInfo

"  VCSLog

"  VCSLock

"  VCSReview

"  VCSStatus

"  VCSUpdate

"  VCSUnlock

"  VCSVimDiff



" Map F4 to VimDiff to check differences

map <F4> <ESC>:VCSVimDiff<CR>



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Latex Configuration

" http://vim-latex.sourceforge.net/documentation/latex-suite/recommended-settings.html

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

au BufRead,BufNewFile *.cls      setlocal filetype=tex

"let g:tex_flavor='latex'   " loads latexsuite with empty tex files.

set iskeyword+=:           " type /ref{fig: and press <C-n> to autocomplete references



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Working with Makefiles

"

" Press F5 to compile and open the error window if there

" are errors. If there are errors you can use :cn and :cN to

" jump foward and backward thru the error list. Press :ccl to

" close the error list or F5 again to recompile

map <F5> <ESC>:make<CR><ESC>:botright cwindow<CR> " Compile and open quick fix list

map <F6> <ESC>:cN<CR>                             " Jump to prev error/warn

map <F7> <ESC>:cn<CR>                             " Jump to next error/warn



""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Enable spell checking in latex, bib and txt files

" Commands:

"            [s      ->  jump to next bad word

"            ]s      ->  jump to prev bad word

"            z=      ->  suggest word

"            zg      ->  mark word as good (add to dictionary)

"            zw      ->  mark word as bad  (remove from dictionary)

"            :h spell -> get more details about spelling

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

au BufRead,BufNewFile *.txt,*.tex,*.bib  setlocal spell spelllang=en_us



"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Other Good Vim tips not used here

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

" Vim Tip #64   - Set working dir to the file you are editing

"                 Why not used: Makes difficult the management of sessions and

"                   makefiles.

" Vim Tip #305  - List of editing vim tips

"                 Why not used: I can remember all the commands I use.

" Vim Tip #1203 - mapping to set up vtreeexplorer and taglist in left window

"                 Why not used: I use MiniBufExpl instead of vtreeexplorer

" Vim Tip #1318 - An attempt to emulate TextMate's snippet expansion

"                 Why not used: I am unable to get this working correctly.

" Vim Tip #???? - Force gf to open in new tabs  

"                 Command: nnoremap gf <C-W>gf

"                 Why not used: I use MiniBufExpl so no more tabs for me.

" Vim Tip #565  - Never see ^M and pesky trailing spaces again

"                 Command: autocmd BufRead * silent! %s/[\r \t]\+$//

"                 Why not used: Not a good idea when working with others source

"                  code. All the automatically deleted chars will be seen as

"                  changes in the diff files making it difficult to review the

"                  changes. Never use this if you plan to submit patches to ffmpeg

"                  or the Linux kernel or they will spam filter you forever!




3 comments:

  1. Some updates:
    - Found out that shift-tab does not work in Konsole so the binding to :tabprev does not work.

    - Auto deleting trailing spaces and DOS end lines (tip #565) is not a good idea when modifying other people source. Especially if the code is under versioning (Git, SVN) as the deleted trailing spaces will look as changes in diff making review very difficult.

    ReplyDelete
  2. Anonymous9:01 PM

    thanks a ton dude.. that was really helpful.. i totally replaced my vimrc with your contents!!

    ReplyDelete
  3. Good to hear this is useful to others... I just updated the .vimrc to add line breaks for lines longer than 80 chars.

    Before I used to wrap long lines visually. That is no line break was added. This works good for programming but when editing latex or text documents it can be very difficult to navigate long paragraphs as these are in fact a single long line. Adding a break will allow you to step in each paragraph line as in other word processor, if no line break is added then pressing down will jump to the end of the paragraph that is very anoying.

    ReplyDelete