!-- Syntax Highlighter Additions START -->

Monday, February 27, 2012

VIM useful tips and tricks for novice

Being tired of using arrow keys for navigation inside vim ?
wondered about tabbed editing in vim ?
tired of using Esc wq! in vim ?
Want to make effective use of your Vim editor.?
Ever wondered you could use ctrl+(key) for editing operations as you do in other GUI editots.?
Here are some tips which would turn your vim to a powerful editing tool.
I am sure you will get addicted to and will be falling in love with VIM,as i did :)

Do i need any additional files to be downloaded?

I have used many plugins here, that i needed most. If your main editor is vim and you have no option of GUI to do your work, then you should be using these to have a pleasant working :)

How Do I Make these changes in My VIM ?

copy the section that you wish you need to try, and paste the test to your .vimrc file.
When you have not changed the settings of vim, the file you need to paste in is ~/.vimrc If there is no such file, dont worry create it.

 if &term == "xterm"
    set t_ti=
endif
set autoindent
set background=dark
set ruler
set laststatus=2

If only hitting enter starts the search,
add the below lines to search as you type in and highlight your search results.

set incsearch
set hlsearch
loosing your history of commands you give in your vim ?
Tell vim to save the last 999 commands you typed in !!
set history=999
***********************************************************************************
       File Operations P**********************************************************************************
clears highlighting

map  :nohl
map    
Use Ctrl+w to save the file. is used by the terminal program to stop the display on the terminal you can follow the workaround here to make Ctrl+s work!! (I am used to Ctrl+s for stopping long logs that displays on my terminal)
map   :w 
Ignore changes and Quit
map  :q!
save and Quit command in visual mode
map wq  :wq!
imap wq  :wq!
Undo command in visual mode
map  u
Redo,copy and paste command in visual mode
map  U
map  yy
map  p
***********************************************************************************
" Insert mode key Mappings ***********************************************************************************
Use contrl+w to save in insert mode
:imap  :w
use ctrl+space to autocomplete in vim !!
:imap    
Save and Quit in insert mode
:imap  :wq
Ignore changes and Quit in insert mode
:imap  :q!
Undo and redo command in insert mode
:imap  u
:imap  U
Copy and paste command in insert mode
imap  yy
imap  p
Toggle between Tlist and mouse mode.
function! ToggleMouse()
        if &mouse == 'a'
                set mouse=
                TlistClose
                echo "Mouse usage disabled"
        else
                set mouse=a
	        TlistOpen
                echo "Mouse usage enabled"
        endif
endfunction
:imap mm :call ToggleMouse() 
***********************************************************************************
" set the ctags path ***********************************************************************************
search for ctags from the current directory upwards till the root(without this line, you need to be in the directory in which the ctags was built, to use the c tags)
set tags=tags;/
***********************************************************************************
" Tab navigation ***********************************************************************************
map  :tabprevious 
nnoremap   :tabnext
***********************************************************************************
" Cscope in vim ***********************************************************************************
"cscope mapping
if has("cscope")
" Find this C symbol
map  :tab split:cs find 0 =expand("")
" Find This Global Definiton
map  :tab split:cs find 1 =expand("")
" Find Funtions called by  this function
map  :tab split:cs find 2 =expand("")
" Find functions calling this function
map  :tab split:cs find 3 =expand("")
" Find This Text String
map  :tab split :cs find 4 =expand("")
" Find this definition
map  :tab split:exec("tag ".expand(""))
" Find the egrep
map  :cs find e =expand("")
" Find the file
map  :cs find f
map  :vsp :exec("tag ".expand(""))
" Find this C symbol
map  :cs find 0 =expand("")
" Find This Global Definiton
map  :cs find 1 =expand("")
" Find Funtions called by  this function
map  :cs find 2 =expand("")
" Find functions calling this function
map  :cs find 3 =expand("")
" Find This Text String
map  :cs find 4 =expand("")
map  :tab split :cs find 3 =expand("")
" Find this definition
map  :exec("tag ".expand(""))
" Find the egrep
map  :cs find e =expand("")
" Find the file
map  :cs find f
map  :vsp :exec("tag ".expand(""))
endif

map  :ta
map  
map  
***********************************************************************************
" Mouse control ***********************************************************************************
map :set mouse =a map :set mouse -=a noremap mm :call ToggleMouse() ***********************************************************************************
" Plugin Commands ***********************************************************************************
"Toggle Tablist nnoremap :Tlist ***********************************************************************************
" Misc ***********************************************************************************
filetype plugin on ***********************************************************************************
Disable the arrow keys ! use hjkl instead ***********************************************************************************
nnoremap  
nnoremap  
nnoremap  
nnoremap  
inoremap  
inoremap  
inoremap  
inoremap  
nnoremap j gj
nnoremap k gk
***********************************************************************************
" save on lost focus ***********************************************************************************
au FocusLost * :wa
***********************************************************************************
" use jj for ***********************************************************************************
inoremap jj 
***********************************************************************************
" for srcexpl plugin ***********************************************************************************
" The switch of the Source Explorer
nmap  :Tlist  :SrcExplToggle
" Set the height of Source Explorer window
let g:SrcExpl_winHeight = 8
" Set 100 ms for refreshing the Source Explorer
let g:SrcExpl_refreshTime = 100
"  Set 'Enter' key to jump into the exact definition context
let g:SrcExpl_jumpKey = ""
"  Set 'Space' key for back from the definition context
let g:SrcExpl_gobackKey = ""
"  In order to Avoid conflicts, the Source Explorer should know what
" plugins  are using buffers. And you need add their bufname into the list below
"   according to the command 'buffers!'
let g:SrcExpl_pluginList = [
                        \ "__Tag_List__",
                        \ "_NERD_tree_",
                        \ "Source_Explorer"
                        \ ]
"    Enable/Disable the local definition  searching, and note that this is not
"    guaranteed to work, the Source Explorer  doesn't check the syntax for now.
"    It only searches for a match with the  keyword according to command 'gd'
let g:SrcExpl_searchLocalDef = 1
"      Do not let the Source Explorer update the tags file when opening
let g:SrcExpl_isUpdateTags = 0
"     Use 'Exuberant Ctags' with '--sort=foldcase -R .' or '-L cscope.files' to
"      create/update a tags file
let g:SrcExpl_updateTagsCmd = "ctags --sort=foldcase -R ."
"    Set '' key for updating the tags file artificially
let g:SrcExpl_updateTagsKey = ""