Showing posts with label vim. Show all posts
Showing posts with label vim. Show all posts

Saturday, October 23, 2010

vim indentation

Some useful lines for configuring the indentation behavior of vim.
These commands can be executed inside vim, all preceded by a colon (:), or added to ~/.vimrc.

This changes the display width of the tab (to save precious screen space):
  set shiftwidth=3
  set tabstop=3

and adding this, replaces the tabs with spaces
  expandtab
  set softtabstop=3

To indent (or re-indent) a block of code, first select some lines with Shift-V, and then press =

Sources:
http://vim.wikia.com/wiki/Indenting_source_code
http://tedlogan.com/techblog3.html

Saturday, July 24, 2010

Code folding with Vim

I've learned about code folding using Vim.
The command zfa} collapses (folds) a C block using the } symbol as guide.

The following code maps the Spacebar to a function that folds/unfolds the current block (add this to ~/.vimrc), based on this.

"Fold coloring 
hi Folded        ctermfg=white ctermbg=NONE

fun! ToggleFold()
if foldclosed('.') < 0
". foldclose
:execute "normal zfa}<cr>"
else
. foldopen
endif
" Clear status line
echo 
endfun

" Map fold/unfold function to Space key.
"nmap <space> zfa}<cr>
"nmap <space> :call ToggleFold()<cr>
noremap <space> :call ToggleFold()<cr>

Here there is a more sophisticated function for folding.

vmap <space> zf

" inspired by Max Ischenko's http://www.vim.org/tips/tip.php?tip_id=108
function ToggleFold()
   if foldlevel('.') == 0
      " No fold exists at the current line,
      " so create a fold based on braces
      let x = line('.')    " the current line number
      normal 0
      call search("{")
      normal %
      let y = line('.')    " the current line number
      " Create the fold from x to y
      execute x . "," . y . " fold"
   else
      " Delete the fold on the current line
      normal zd
   endif
endfunction

nmap <space> :call ToggleFold()<cr>