Improve your efficiency with VIM

From juliano.info

Jump to: navigation, search

VIM is my text editor of choice, it has plenties of features that, when you learn how to use them, they boost your efficiency greatly on both programming and general text editing.

Here are two small tips for those who does a lot of programming with VIM:

Quick block shift

vnoremap <Tab> >gv
vnoremap <S-Tab> <gv 

With this, just select a block of code and press Tab or Shift-Tab to quickly shift or unshift it.

Automatic parenthesis

function! <SID>J_CloseParenWrapper(ch)
    let line = getline('.')
    let col = col('.') - 1
    if line[col] =~ a:ch
        return "\<Delete>" . a:ch
    else
        return a:ch
    endif
endfunction
inoremap <buffer> ( ()<Left>
inoremap <buffer> ) <C-R>=<SID>J_CloseParenWrapper(')')<CR>
inoremap <buffer> <leader>( (
inoremap <buffer> <leader>) )
inoremap <buffer> [ []<Left>
inoremap <buffer> ] <C-R>=<SID>J_CloseParenWrapper(']')<CR>
inoremap <buffer> <leader>[ [
inoremap <buffer> <leader>] ]
inoremap <buffer> {<CR> {<CR>}<Up><End><CR>

This closes parenthesis automatically for you. When you press "(", VIM will insert ")" automatically and place the cursor between them. If you press ")" right before a closing parenthesis, VIM just advances the cursor over it.

Soon I'll post some tab-completion tips.

Views