Is there a convenient way to surround a code block or a range of lines with markdown backticks? That amounts to doing a surround op to insert multiple chars.

Ie. turn:

line 1
line 2


line 1
line 2

The surround plugin has allowed me to insert single ticks, but it just creates more work.

I guess something like {3i`<ESC>}. might do, but it's still a bit finickety.

  • Is there something open in your question? How can we help you further? Otherwise maybe could you accept of of the solution using the v button next to the arrow voting buttons. It allow the question to rest :-) Commented Mar 1 at 11:24

5 Answers 5


Presuming your lines are already formatted as a paragraph as in your example, you can type:


You can substitute in any motion/text object for ip if you want to surround other things.

See also the technique described in the second half of this earlier answer if you'd like the edit to be repeatable with the . dot command.


If you have to do that often I would introduce the following mapping:

vnoremap Sq <esc>o```<esc>gvo<esc>O```<esc>gv

Then you can select your lines and hit Sq to surround them with ```


Turning common but overly expensive sequences of commands into convenient mappings is an integral part of the Vim experience. If you can clearly express that sequence of commands it is generally relatively easy to map it.

One way to do what you want manually would be:


which can easily be turned into a visual mode mapping:

" in after/ftplugin/markdown.vim
xnoremap <buffer> <key> c```<CR><C-r>"```

You can use a single key for maximum convenience.

So, basically:

  1. take note of the repetitive sequence of commands,
  2. turn it into a mapping,
  3. put it at the most appropriate place.

I use my template engine for this task, but it can be done with simple mappings.


  • I consider this should be enabled only for markdown buffers. -> ftplugin + <buffer> (for Doxygen comments, we should reuse comment leaders)
  • I define the mapping only for pure visual, and not select mode. -> Xmap
  • As Vivian has already shown a good way to do it. Here are a few others with pro and cons
" Expected to go into ~/.vim/ftplugin/markdown_mymappings.vim

" With :put
" Here, I don't reselect (gv), but leave the cursor intentionally a the end of the first triple back-tick -- This way, we can append the language name with `avim` for instance.

xnoremap <buffer> <silent> µ <c-\><c-n>:'>put='```'<cr>:'<put!='```'<cr>$

" -----------
" with append()
" pro: doesn't move the cursor => the one we tend to prefer in plugins (NB: I wasn't aware or CTRL-_CTRL-O so far...)
" con:
" * the most complex one
" * not redoable

xnoremap <buffer> <silent> µ <c-\><c-n>:call append("'>", '```')<bar>:call append(line("'<")-1, '```')<cr>

" -----------
" with s + ctrl-r
" Pro: the simple way that we shall be able to remember in order to surround anything without plugin assistance
" Con: 
" * Alters default register
" * This particular implementation works only with [linewise-visual](http://vimhelp.appspot.com/visual.txt.html#linewise%2dvisual) mode.

xnoremap <buffer> <silent> µ s```<cr><c-r>"```<esc>

" Or better: the reoable version (thanks Rich for CTR-R_CTR-O!)
" Same cons as the previous one otherwise
xnoremap <buffer> <silent> µ s```<cr><c-r><c-o>"```<esc>

If you use the surround.vim plugin:

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

Now you can use for instance yssc or vipSc.

If you want a solution without a plugin and prefer a function to a sequence of normal mode keys:

function! SurroundWithTripleBackticks() range
  call append(a:firstline - 1, '```')
  call append(a:lastline + 1, '```')
  call cursor(a:firstline, 3)

nnoremap <silent> <LocalLeader>z :call SurroundWithTripleBackticks()<CR>
vnoremap <silent> <LocalLeader>z :call SurroundWithTripleBackticks()<CR>

By default, <LocalLeader> is the backslash key.

The cursor is put at the end of the first triple backticks, so you can easily enter the filetype.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.