Hack and / - Lightning Hacks—the Command Next Door
Every year or so, I like to write a column I title “Lightning Hacks”. This column is inspired by the lightning talks you encounter at most conferences. In a lightning talk, instead of having one speaker give a 60-minute presentation, multiple speakers give short 5–10-minute presentations. By the end of a lightning talk, you end up hearing about all sorts of cool topics that wouldn't have gotten their own time slot. In this column, I get a chance to talk about a few cool “hacks” I've run across that by themselves wouldn't fill an entire column.
In past Lightning Hacks columns, I've covered a wide variety of different topics, and in the previous edition, I focused strictly on hacks with the ssh command. In this column, however, I showcase a few interesting tricks with commands you might use every day. It's easy to take your favorite programs for granted, but I've found that no matter how long I use Linux, it seems I'm always picking up new time-saving tips, and a Lightning Hacks column is as good as place as any to list a few of my favorites.
It isn't news to frequent readers of this column that I edit all of my text files with vim. I've been using vim for so long, my muscle memory for the Esc key is second nature. When you press the Esc key in vim, you return back to the default navigation mode from whatever mode you were in. When I'm at all unsure where I am or what mode I'm in, my brain automatically has my hand bang on that Esc key like I'm a champion Hungry Hungry Hippos player. Of course, on a modern keyboard, the Esc key is quite a way from home row, and what's worse, on some keyboards (like on ThinkPads), the Esc key is in a different place, so someone like me hits F1 half the time. It turns out, however, that Ctrl-[ functions just like Esc in vim (and other programs that accept vi-style syntax, like vimperator). Both of those keys are just a pinkie-reach away and keep most of your fingers on home row. I have to say though, old habits die hard. I had to put a post-it note on my monitor that read “Ctrl-[” for a few weeks before my fingers started to catch the hint. It takes only a few weeks for the habit to form though, and before you know it, you'll be shaving that extra millisecond off your typing time.
Now that I've removed yet another function from the poor Esc key, let's give it something else to do. When I first set up my N900 with terminal sessions, I ran into a problem a lot of portable computer users face: half the special keys aren't on the tiny keyboard. In my case, this presented quite a problem, because I am a heavy Irssi user and my keyboard had no way to press Alt even in the special symbols menu. Without the Alt key, it was a pain to switch between different Irssi windows. I wasn't sure what to do until I discovered that in most terminal sessions, the Esc key can substitute for Alt. It turned out my terminal emulator had an Esc touchscreen key-mapped, so from that point on, I just pressed Esc whenever I needed Alt. It also turns out this works the same way on all of my laptop terminal sessions.
This is a quick-and-simple tip that I can't believe took me so long to discover. Quite often, you will be in a situation where you want to open a file to a specific line number. A few good examples include when you re-kickstart a server, go to ssh in to it and realize the host keys have changed. The error message lists the line number with the conflicting key, so it is easy to go in and erase it. Another common example occurs when you are working on a script or configuration file and see a reference to a syntax error on a specific line.
Back in the day, when I wanted to go to a specific line in vim, I would open the file and press :<number><Enter> to go to the line, but if you want to save a step, just add +<number> to the command line as an argument when you execute vim. For instance, if I want to jump ahead to line 27 in my ~/.ssh/known_hosts file, I would type:
vim ~/.ssh/known_hosts +27
Grep is one of the crucial tools you should master if you want to spend any time on the command line. One of the most common ways I use grep is to check whether a certain process is running and see its process ID. For instance, if I want to check for the process ID of my cron dæmon, I might type:
$ ps -ef | grep cron root 1215 1 0 Feb23 ? 00:00:00 cron greenfly 1252 1976 0 20:57 pts/0 00:00:00 grep cron
In this example, cron has a process ID of 1215. The annoying thing though is that because I had cron in my grep command, it also showed up in the output. This means every time I run this command, I have to be sure to ignore that line in the output. Of course, I also could use grep to filter itself:
$ ps -ef | grep cron | grep -v grep root 1215 1 0 Feb23 ? 00:00:00 cron
That works; however, it's unnecessary. If I enclose one of the characters in my first grep filter in brackets, it does the filtering for me:
$ ps -ef | grep [c]ron root 1215 1 0 Feb23 ? 00:00:00 cron
This works because when I enclose c in brackets, it turns it into a character class that contains only one character, c. However, the grep command itself shows up in the process list with those brackets, so it doesn't match my [c]ron pattern anymore.
The problem with powerful server hardware is that it always takes a little time to reboot. Often when I have to reboot a server, I want to know the moment it comes back up, because I want to log back in and finish some work. Usually when I reboot a machine, I start a ping process, so I can watch to see when it is available. The problem is that servers seem to take just long enough to come back up that I end up getting distracted in another window on my desktop and don't see the ping replies. My solution to this is to make ping audible:
$ ping -a 10.0.1.7
When you pass the -a command to ping, it generates an audible bell for every ping reply. I get to hear a few system beeps as the server goes down, and then I hear only silence. If I do become distracted and the server comes back, I immediately start hearing this annoying system beep in my ear and know the server is ready.
Kyle Rankin is a Systems Architect in the San Francisco Bay Area and the author of a number of books, including The Official Ubuntu Server Book, Knoppix Hacks and Ubuntu Hacks. He is currently the president of the North Bay Linux Users' Group.