Skip to content

Instantly share code, notes, and snippets.

@rkumar
Created October 11, 2009 18:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rkumar/207771 to your computer and use it in GitHub Desktop.
" place this in .vimrc file
" after marking a block, using key (,m) will prompt for a char
" which will be placed on both sides of the block
" A block start char such as {, [ or ( will be replaced by its corresponding
" end character.
" Useful if creating Markdown documents, or in README, TODO etc files
vmap ,m <ESC>:call VisualMarkdownTagWrap()<CR>
function! VisualMarkdownTagWrap()
let a:tag = input( "Chars to wrap block: ")
let braces_at_start = ['[','{','(','[','<']
let braces_at_end = [']','}',')',']','>']
let a:jumpright = 0 + len( a:tag )
normal `<
let a:init_line = line( "." )
exe "normal i".a:tag
normal `>
let a:end_line = line( "." )
" Don't jump if we're on a new line
if( a:init_line == a:end_line )
" Jump right to compensate for the
" characters we've added
exe "normal ".a:jumpright."l"
endif
let m = index(braces_at_start,a:tag)
if m >= 0
"exe "normal a".strpart(braces_at_end,m,1)
exe "normal a".get(braces_at_end,m,"")
else
exe "normal a".a:tag
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment