Hack #39. Exploit the Power of Vim
Use Vim's recording and keyboard macro features to make monotonous tasks lightning fast.
Every administrator, at some point in his career, runs into a scenario in which it's unclear whether a task can be performed more quickly using the Vim command . (a period) and one or two other keystrokes for every change, or using a script. Often, admins wind up using the . command because they figure it'll take less time than trying to figure out the perfect regex to use in a Perl, sed, or awk script.
However, if you know how to use Vim's "recording" feature, you can use on-the-fly macros to do your dirty work with a minimum of keystrokes. What's more, if you have tasks that you have to perform all the time in Vim, you can create a keyboard macros for those tasks that will be available any time you open your editor. Let's have a look!
Recording a Vim Macro
The best way to explain this is with an example. I have a file that is the result of the dumping of all the data in my LDAP directory. It consists of the LDIF entries of all the users in my environment.
One entry looks like this:
dn: cn=jonesy,ou=People,dc=linuxlaboratory,dc=org objectClass: top objectClass: person objectClass: organizationalPerson objectClass: inetOrgPerson objectClass: posixAccount objectClass: evolutionPerson uid: jonesy sn: Jones cn: Brian K. Jones userPassword: {crypt}eRnFAci.Ie2Ny loginShell: /bin/bash uidNumber: 3025 gidNumber: 410 homeDirectory: /u/jonesy gecos: Brian K. Jones,STAFF ...