DEV Community

Pavol Z. Kutaj
Pavol Z. Kutaj

Posted on

How to Create a Markdown Code Block in Vim With a Single Keystroke

The aim of this pageđź“ť is to simplify the creation of markdown codeblock to code that is already in a file. There may be other scenarios. This is working for me. I often have a JSON in markdown or other pieces of code.

Instead of first creating the block, I paste/create the content that should go into the block, then visually select it and then press c to create the block with cursor in INSERT mode after the first triple backtick, so that I can define a language/style of the block.

  • there may be better ways just using vanilla-vim, but I just can't resist this has been my VSCODE workflow for a long time
  • therefore - select the code block visually and go with c (or whatever letter you define)
  • the implementation is simple, just put the following into your .vimrc:
  • NOTE: it is a triple backtick, not a double as shown below but it is breaking the parsing of the blog and I don't know how to escape it reliably :/
vnoremap <leader>c <esc>o``<esc>gvo<esc>O``
Enter fullscreen mode Exit fullscreen mode
  • Then select the block visually, ideally with line selection
  • Press <leader>c --> DONE

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs