Apparently you can use Esc in norm commands. Very useful!

Most of you might know already but I just found out about this today because I needed it. I had thought there’s just no way to use escape in norm commands. So I had this file with list of items which were mostly separated by newlines but some of them were separated by spaces so I had to clean it up. It looked something like this:


<span style="color:#323232;">begin A
</span><span style="color:#323232;">begin B begin C
</span><span style="color:#323232;">begin D
</span><span style="color:#323232;">begin E begin F begin G
</span><span style="color:#323232;">begin H
</span>

and I needed it to be like this:


<span style="color:#323232;">begin A
</span><span style="color:#323232;">begin B
</span><span style="color:#323232;">begin C
</span><span style="color:#323232;">begin D
</span><span style="color:#323232;">begin E
</span><span style="color:#323232;">begin F
</span><span style="color:#323232;">begin G
</span><span style="color:#323232;">begin H
</span>

The beginning of every item was the same string of characters which was helpful. So I had an idea but it required the use of escape in a norm command. I was about to think of some other way that doesn’t require escape but then decided to google it and find out if there was a way to use escape. To my surprise it was possible! Why I haven’t thought of this before? So this is what I came up with:

:g/.*begin/norm /begin^[hr^M

So the ^[ is an escape and you get it with C-v Esc. Simple as that. The command to organize my list isn’t perfect though as it has to be run few times to go through every item but it was enough for my purposes.

TL;DR: Press C-v Esc in command line mode to get escape.

sorrybookbroke,

Huh, that’s pretty sick. I’ll admit I’ve not used the norm command much myself as recordings seem to do the trick. Out of curiosity, why is this better?

siipale,

I’ve always thought that macros in vim are slow and clumsy to use. You have to think about getting back to initial state as you record if your intent is to repeat the macro several times. If you make a mistake you have to either record an action that corrects the mistake or edit the macro later after recording. You have to know beforehand how many times you want to repeat the macro or run it one at a time which is clumsy at least with Finnish keyboard layout.

Norm command is much faster to use for my purposes because I can use it for several lines at a time without thinking about the state. I can use it with ranges or with g/re/ and v/re/ commands. If I make a mistake I just delete it. Only thing on the plus side I can think of for macros is it’s WYSIWYG approach. You have to have a visual editor in your mind when you write long norm commands.

Here’s a tip for you: use :%norm @a to run the macro in register a for every line of the file.

sorrybookbroke,

Oh wow I hadn$)'t thought about using them together like that. I think I’ll start using the command quite a bit more to bridge the ‘find next placement’ step. Though, I was just using a search before the recording and ‘n’ to get the next occurance the norm command sticks it in my command history

  • All
  • Subscribed
  • Moderated
  • Favorites
  • neovim@sopuli.xyz
  • DreamBathrooms
  • ngwrru68w68
  • InstantRegret
  • magazineikmin
  • thenastyranch
  • rosin
  • khanakhh
  • tacticalgear
  • Youngstown
  • slotface
  • Durango
  • kavyap
  • mdbf
  • ethstaker
  • JUstTest
  • GTA5RPClips
  • modclub
  • normalnudes
  • cisconetworking
  • osvaldo12
  • tester
  • everett
  • cubers
  • Leos
  • anitta
  • provamag3
  • megavids
  • lostlight
  • All magazines