qa$F.d0xjq

You love the headline, admit it. Let me explain what’s it about.

Imagine having a huge file with hundreds of lines in which instance paths are located, something like:

As with every path, it looks like a.b.c.d.e.f.g. Suppose you just need to extract “g” from it. While you can write some Python script for it, it can actually be done in VIM quite elegantly, albeit it looks a bit cryptic:

qa$F.d0xjq

Let me explain what it does.

First, we want to make a macro – a series of commands in VIM – that we can easily replicate. From Normal mode, we initiate macro recording by “qa”.

Next, we have a series of commands:

  1. Move to the end of the current line: $
  2. Locate the first previous dot: F.
  3. Delete from that point until the beginning of the line: d0
  4. Delete the dot: x
  5. Move to the next line: j
  6. End macro recording: q

You can apply a macro from the first line in your file to the whole file with X@a, where X>number of lines in your file. In my case 150@a was enough.
Result – eliminated everything from the path except the last part:

There you go, another way to demonstrate the magic of VIM editor! 🙂 As I’ve said a few times, you never go back once you go VIM.

Share with me your favorite VIM tips!

You may also like

Join my Mailing list!