Friday, September 16, 2011

Vim configurations: Part 1

Installation of (g)vim

Vim or it's graphical version (gvim) is one of the best text editors. (Emacs is another great editor - just mentioned it because I don't want to start a flame war here. ;))
However, some of it's features are hidden and can be activated by playing with it's config files a bit. In ubuntu vim-tiny is installed by default. To get more out of it one should install the vim-gtk package.
sudo apt-get install vim-gtk
Before we start create the following directory structure.
mkdir -p ~/.vim/plugin
mkdir -p ~/.vim/ftplugin
Notice the dot at the beginning of ``vim" which makes the directory hidden.

Vim configs

My personal vimrc is available here. Copy my vimrc or create the file $HOME/.vimrc. As before notice the dot in front of vimrc which makes the file hidden.
Even though my vimrc is heavily commented, for the sake of completeness let me point out the benefits of couple of the options.

" Set vim to be nocompatible, so as not to be compatible with vi
" Highly recommended: Has to be the first line.
set nocompatible

" To set status line, so as to highlight the status bar below.
set laststatus=2

" When editing a file, always jump to the last known cursor
" position so that when you reopen the file you don't have 
" to scroll to your edit position.
autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif

" For recognizing specific file types
let file_ext = bufname("%")

" Wraps line at the 5th column from the right margin and an 
" <EOL> is inserted 
if file_ext =~ '\.txt$' || file_ext =~ '\.tex$' 
    set wrapmargin=5
endif

" To wrap long lines
set wrap

" Allow backspacing over everything in insert mode
set backspace=indent,eol,start

" To getrid of annoying ~ files
set nobackup

" To show current mode
set showmode

" Show line number,column number always
set ruler

" Show report when N lines were changed
" report=0 means "show all changes"!
set report=0

" Show title of file
set title

" Ignore filename with the following suffixes when using
" :edit or :sp  
set suffixes=.aux,.log,.pdf,.ps,.tar,.gz,.tgz,.dvi,.bbl,.blg,
    \.eps,.out,.png,.los,.lof,.lot,.dat,.sty,.xml,.toc,
    \.latexmain,.bm,.idx,.ilg,.ind,.fdb_latexmk

" Auto indent
set autoindent
" Smart indent
set smartindent

" To toggle between set number and set nonumber
nmap <C-N> :set number! <CR>

" To ignore case in search pattern
set ignorecase
set smartcase

" To incrementally search pattern
set incsearch

" No tabs in the source file
" All tab characters are of 4 space characters
set softtabstop=4
set tabstop=4
set shiftwidth=4
set expandtab

" Fortran tabs won't be colored red (Have to loaded before
" 'syntax' command)
let fortran_have_tabs=1

" Switch on syntax highlighting if it wasn't on yet.
if !exists("syntax_on")
  syntax on
endif

" Allow switching buffers, which have unsaved changes
set hidden

" Switch on search pattern highlighting.
set hlsearch

" For filetype plugins
filetype plugin on

" To handle common typos in commands
command! Q  quit
command! W  write
command! Wq wq
command! WQ wq

" Spelling auto correction: Auto correcting typos
iabbr adn and
iabbr nad and
iabbr teh the
iabbr hte the
iabbr alos also
iabbr aslo also
iabbr qed QED
iabbr qcd QCD
Okay, I discussed almost all my vimrc configs :).  

GVim configs

GVim is the GUI version of vim. Some configurations are specific to gvim. You can download my personal gvimrc config file from here. Again as before copy my gvimrc or create the file $HOME/.gvimrc.
" Auto Change Directory
set acd

" For shortcut keys as in Windows
" Block selection "Ctrl+v" gets mapped to "Ctrl+q"
source $VIMRUNTIME/mswin.vim
The line source $VIMRUNTIME/mswin.vim would be most useful for people who are used to shortcut keys.

In my next posting I will discuss some (g)vim plugins that I have written along with a couple of third party plugins which I find very useful.

No comments:

Post a Comment