Surround Visual Text with more than 1 character
Asked Answered
F

3

6

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

Firkin answered 20/11, 2017 at 20:59 Comment(0)
B
6

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.

Bridoon answered 20/11, 2017 at 21:36 Comment(1)
Nice one @Bridoon - Will accept this as this is using Vim Surround. Though romainl answer was really good toolFirkin
C
11

This is not what you asked but this can be done without surround:

(from visual mode)
c
```
<C-r>"
```
<Esc>

See :help i_CTRL-R.

  1. c: Delete text and start insert. The deleted text is put in the unnamed register ".
  2. Type the prefix characters.
  3. Use <C-r>" to paste the content from the unnamed register.
  4. Type the suffix characters.
Capone answered 20/11, 2017 at 22:32 Comment(2)
Here is why I think it work - :help registers says Unnamed register "" : Vim fills this register with text deleted with the "d", "c", "s", "x" commands or copied with the yank "y" command. With c, the visually selected text is moved to the unnamed register. You type the prefix characters (triple backticks in the above example) and press <C-r>" to paste text from the unnamed register. Then add the suffix characters (again triple backticks in the example).Television
See also #3997578Television
B
6

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.

Bridoon answered 20/11, 2017 at 21:36 Comment(1)
Nice one @Bridoon - Will accept this as this is using Vim Surround. Though romainl answer was really good toolFirkin
H
2

Here another ultisnips solution.

snippet code "add backtics codes" w
`!v repeat(nr2char(96),3)` ${1:markdown}
${0:${VISUAL:type here}}
`!v repeat(nr2char(96),3)`
endsnippet

If you do not want "markdown" after the first line just get rid of it. I am showing this solution only to show how to avoid backslashing so much.

Harmonious answered 30/11, 2017 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.