Hack and / - Dr hjkl and Mr Hack

 in
Arrow keys, schmarrow keys—some of the best programs out there let you move around from the home row just like vim intended it.

Without diving headfirst into an ancient Linux holy war, let me set the record straight. I am a card-carrying, home-row hugging, Esc-key hammering, vim user. If you love Emacs, JOE, ed, Kate, gedit or the magnifying-glass-and-magnet approach to text-file editing, that's fine, and I'm not here to judge. It's just that for me, once I got over the initial vi(m) learning curve, I started looking for other tools that take the same approach to key bindings. Specifically, I am talking about the h, j, k and l keys and how you can use them to move left, down, up and right, respectively, in a document. What I found was that most of my favorite tools either already had vi-style key bindings or there was a simple way to enable them. Some programs even offered advanced bindings that closely mimic vim in a number of ways. In this column, I highlight some programs that either have vi key bindings or can be made to have them with a few simple steps.

Before I start talking about specific programs, I probably should explain why navigation with hjkl is better than with the arrow keys. It's a dirty secret among vim users that many people just use the arrow keys and backspace to edit their documents. The main reason hjkl navigation is great is that all of those keys are on the home row. In case you never took formal typing, the home row is the asdfghkl row of keys on a qwerty keyboard. If you learn to touch type, you are taught to rest your fingers on this row by default. This means the hjkl keys are within easy reach, but every time you reach for the arrow keys you have to move your right hand off the home row. Now, if you aren't a touch typist, that isn't a big deal. But if you are, it is almost as disruptive as reaching for the mouse. Granted, I know it is awkward at first, but if you are a vim user and touch type at all, it's worth it to force yourself to use hjkl for navigation until it becomes second nature.

In case you are new to vi key bindings, here are some of the main keys that you'll find work similarly in other programs:

  • h — move left

  • j — move down

  • k — move up

  • l — move right

  • ^ — move to the beginning of a line

  • $ — move to the end of a line

  • G — move to the bottom of the document

  • g — move to the top of the document (gg in vim)

  • w — move the cursor ahead one word

  • b — move the cursor behind one word

  • / — enter search mode

  • n — go to the next search result

  • N — go to the previous search result

Paging Programs

A number of standard command-line programs use vi-style navigation out of the box, and the first I want to mention is less. The less program allows you to page through a text file, and if you needed yet another reason to use less instead of more, use it because j and k will move down and up a document. In addition, you can type G to scroll to the very bottom of a document and g (gg in vim) to move to the very top. As with vim, you also can press / to type a search term, and press Enter, then press n and N to find the next and previous matches, respectively. Like with less, by default, you can scroll through man page output with the same keys.

Screen also can use vi key bindings to navigate through its copy mode. Screen is an amazing shell program that allows you to open multiple shell sessions and detach and re-attach to them. If you've started using screen after being used to a regular terminal session, you likely ran into the strange behavior screen exhibits when you press Shift and PgUp and PgDn (or use the scroll bar) to scroll up and down through the output. In screen, if you want to view output that has scrolled past the top of the terminal, simply press Ctrl-A Esc to enter copy mode. Within copy mode, now you can use the arrow keys (shame on you) or hjkl to scroll around the output. As with less, you also can use g and G to scroll to the top and bottom of the output. When you are done scrolling, simply press the q key to exit copy mode.

Even bash itself can be set so that you can navigate the command line in true vi style. In your bash shell, just type set -o vi. Now, keep in mind that once you enable this option, you will have to enter insert mode (press the i key) to insert text just like in vi. And, if you want to use h or l to move the cursor left or right, or w or b to move forward or back a word, you will have to press Esc to leave insert mode. For those of you who tried this and want to undo it, simply press i to enter insert mode, and then type set -o emacs.

______________________

Kyle Rankin is a systems architect; and the author of DevOps Troubleshooting, The Official Ubuntu Server Book, Knoppix Hacks, Knoppix Pocket Reference, Linux Multimedia Hacks, and Ubuntu Hacks.

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState