April 22, 2021, 6:33 a.m.

rockyourcode: TIL About the Global Command in Normal Mode in Vim

rockyourcode

Hello 👋! Thanks for subscribing.

TIL About the Global Command in Normal Mode in Vim

Published on: 2021-04-21

tags: Vim, TIL

Vim’s command line mode is as arcane as the rest of the editor.

Today I learned how to use the global command with normal mode

Use case:

I had a file with a few lines that were separated with a symbol:


com.google.android.googlequicksearchbox | Search Widget
com.google.android.inputmethod.latin | Gboard
com.google.android.marvin.talkback | Talkback
com.google.android.music | Google Play Music
com.google.android.onetimeinitializer
com.google.android.printservice.recommendation | Mobile printing service

I wanted to deleted everything after the first “word”: the empty space, the | and everything else.

There are several ways to achieve this. But one is using a global command:

:g/ |/norm nD

Explanation:

:g         : Start a Global Command (:h :g for extra help on global commands)
/ |        : Search for empty space and |
/norm nD   : Execute nD in Normal Mode where
               n - jumps to the match
               D - delete to the end of the line

Now my file looks like this:


com.google.android.googlequicksearchbox
com.google.android.inputmethod.latin
com.google.android.marvin.talkback
com.google.android.music
com.google.android.onetimeinitializer
com.google.android.printservice.recommendation

Further Reading

  • Your problem with Vim is that you don’t grok vi.
  • Vim delete starting from character to end of line

Thank you for reading my blog posts.

Don't hesitate to reach out via email or Twitter!

You just read issue #8 of rockyourcode. You can also browse the full archives of this newsletter.

Brought to you by Buttondown, the easiest way to start and grow your newsletter.