Home Writing

til / using the normal command in vim

Say we’ve copied a list of fruits and want to create a JSON object with quantities using the words. This is a perfect use case for the :norm command in vim.

Note: This uses the excellent vim-surround plugin to simplify a bit.

We’re starting with:

apples
bananas
cherries
strawberries

Visually select everything, then type :norm csw"A: 0, and press enter. This gives us:

"apples": 0,
"bananas": 0,
"cherries": 0,
"strawberries": 0,

Now we just need to add the brackets, so run gvS{, and we get:

{
  "apples": 0,
  "bananas": 0,
  "cherries": 0,
  "strawberries": 0,
}

  • Loading next post...
  • Loading previous post...