Mastodon hachyterm.io

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