vimvim-plugin

Surround Visual Text with more than 1 character


How would you surround the following text with 3 backticks ``` using tpope's Vim Surround.

All I can is 1 backtick using S` in visual mode:

enter image description here


Solution

  • Define a custom surround:

    (Insert following in your .vimrc or file specific config ~/.vim/after/ftplugin/markdown.vim )

    " Custom surrounds
    let b:surround_{char2nr('c')} = "```\r```"
    

    now visual select and Sc will give you desired surround.

    Or use a snippet solution; for example using Ultisnips define a snippet like so:

    snippet code
    \`\`\`${1}
    ${0:${VISUAL}}
    \`\`\`
    endsnippet
    

    now visual select your desired lines then hit snippet expansion key ( mine is Tab ) type code and hit Tab again. that's it.