One of the best investments I’ve ever made is to be proficient with a good cross-platform editor, in my case Vim. It took me a good few months before I really became comfortable with it, but those few months’ struggle yielded huge dividend since then!
So after years of Vim usage, I consider myself a power user. Yet from time to time, I come across some nifty tips that remind me why I fall in love with it in the first place: the sense of wonder, awe, beauty, and intelligence of its creators!
Here are two things I learned recently:
- Sort based on a pattern
I use sort and sort u all the time. sort does what the word implies, sorting all lines in the buffer. sort u (unique) does the sort, but in addition to that, removes duplicate lines. Those two commands are extremely useful.Yesterday I was doing some email log analysis, and had a bunch of email addresses in my file. And I thought, wouldn’t it be nice if I could sort those addresses based on domain names? So I searched the web, then looked through :help sort. Sure enough, I can absolutely do that.
Say you’ve got the following lines:
person1@b.com
person2@a.com
person3@a.com
person4@c.com
person5@a.comTo sort them based on domain names, type :sort /.\+@/ in normal mode will do just that.
- Yank all matching lines into a register
I use :g/pattern/d fairly often. What that line does is to delete all lines inside the document that match the pattern. Since you can use regex with pattern, this can be pretty powerful.However, before deleting them, sometime it is a good idea to save them away. To do that, run
:g/pattern/yank CapitalLetterThis command will put matching lines into a register. Let use X as an example. At a different buffer, you can run
"Xp
And it’ll paste those lines!