Skip to content

Instantly share code, notes, and snippets.

@nivbend
Last active August 29, 2015 14:27
Show Gist options
  • Save nivbend/d3b0833ec91e944edcd9 to your computer and use it in GitHub Desktop.
Save nivbend/d3b0833ec91e944edcd9 to your computer and use it in GitHub Desktop.
Wrap C/C++ code in #if 0/#endif.
" Disable sections in C/C++ code using #if 0/#endif blocks.
" This uses <Leader>-i/e in normal/visual mode. In normal mode, it also takes a count (3<Leader>i to wrap 3 lines).
" The <Leader>-e option places the user inside the #else block.
function! s:wrap_disabling_if(num_lines)
execute "normal O#if 0"
execute "noraml " . a:num_lines . "j"
execute "normal o#endif"
endfunction
autocmd FileType c,cpp nmap <Leader>i :<C-U>call <SID>wrap_in_disabling_if(v:count1)<CR>
autocmd FileType c,cpp nmap <Leader>e :<C-U>call <SID>wrap_in_disabling_if(v:count1)<CR>O#else<CR>
autocmd FileType c,cpp vmap <Leader>i <Esc>'<O#if 0<Esc>'>o#endif<Esc>'<
autocmd FileType c,cpp vmap <Leader>e <Esc>'<O#if 0<Esc>'>o#else<CR>#endif<Esc>O
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment