Featured

    Featured Posts

Dr Hjkl on the Command Line

May 21, 2015 By Kyle Rankin  inHOW-TOsvi

The first time I used vi was in a college programming course. It was thedefault editor on the computer lab's UNIX systems we used to compile ourassignments. I remember when our professor first introduced vi andexplained that you used the hjkl keys to move your cursor around instead ofthe arrow keys. Before this point, I was a pico user (that dates me a bitnow), and it seemed so backward to me that vi used hjkl instead.

It wasn'tuntil I became a heavy vim user that I began to appreciate the speed yougain from navigation keys appearing on home row. As a touch typist, Irealized the arrow keys are in a no-man's land outside the home rowcompared to hjkl, and even though vim supported arrow keys, I used hjklinstead. I've been pleased to discover a number of different programs thatalso support the same level of key bindings.

I've written a few othercolumns in Linux Journal through the years along those lines (all with DrHjkl in the title), and here, I've decided to revive Dr Hjkl foranother round of time-saving command-line navigation tips that will help keepyour hands on the home row and off those arrow keys.

Most of my tips in this article are about reducing your reliance on thearrow keys and increasing your speed when on the command line. For manyyears, whenever I would find a mistake in a command I typed, I would do oneof two things: use a combination of Home, End and the arrow keys (all waytoo far away from home row) to move the cursor back to the mistake so Icould fix it, or sometimes I found it was faster to press Ctrl-C and type thewhole command again. One day I observed another Linux user fly back andforth across words on the command line and realized there was a better way.

Moving Between Words

The first simple speed improvement is the use of Alt-b and Alt-f to movebackward or forward one word on the command line. This behaves somewhatlike the b and w keys in vi to skip between words instead of one letter ata time. Alt-b acts like just like b in vim. Press Alt-b, and the cursor willmove back one word and sit at the first letter of the previous word. Alt-fis slightly different; the cursor moves forward until it ends up at thespace between words instead of at the beginning of the following word. Sogiven the following command:ls -l /var/log

If my cursor were at the end of the line and I pressed Alt-b, it now would beover the l in log. If I pressed it again, it would move to the v in var. If mycursor were at the beginning of the line and I pressed Alt-f, it would end upon the space between ls and -l. This annoys me enough that often I'll findmyself going forward an extra word and then pressing Alt-b sothe cursor is where I want it. Even though it's more keystrokes than theright-arrow key, it keeps my hands on the home row. Alternatively (as you'llsee later), I simply could press Ctrl-f to move forward an extra letter instead.

Delete Words

The bulk of the time that I use Alt-b and Alt-f on the command line is tocorrect a typo earlier in the line. Now, you certainly could move the cursorover to the right position and then use Delete or Backspace to erase theerror, but for minor mistakes, I've found it's much faster to delete thewhole word and retype it. In vim, I would type cw to change the word undermy cursor, or a slightly slower approach is to type dw to delete the wordand then enter insert mode to make my changes. On the command line, youcan replicate the behavior of dw with Alt-d. The Alt-d key will remove the word under the cursor completely so you can retype it. So,taking thesame example from above:ls -l /var/log

If my cursor were at the end of the line and I realized I wanted to change-l to -ltr, I would press Alt-b three times to move it over the l in -l, thenI would press Alt-d to delete the l, and finally type ltr.

Replace Home and End

It turns out the arrow keys aren't the only ones on the hit list. Home andEnd, although useful, also are out in that no-man's land away from the homerow.In vi, you would just use ^ or $ to go to the beginning or end of the line.On the command line, you can replace Home and End with Ctrl-a and Ctrl-e,respectively. If you use screen or tmux with screen key bindings, you knowthat Ctrl-a already is called for, so you'll need to press Ctrl-a a to sendthat Ctrl-a through to the command line. Ctrl-a can be particularly usefulwhen you realize you need to pipe some initial command through somethingyou've already typed, and Ctrl-e is useful to move back to the end of theline afterward to finish your command after editing it somewhere in themiddle.

A Few Final Shortcuts

Although the above shortcuts are enough to get started, before I finish, Iwant to highlight a few extra shortcuts that are less useful but worthknowing all the same. First, although Alt-b and Alt-f move backward andforward a word, respectively, their counterparts Ctrl-b and Ctrl-f willmove backward and forward a single letter. Somewhat less useful, but stillinteresting, is the fact that you can use Alt-u to uppercase the full wordunder the cursor, Alt-l to lowercase the word, and Alt-c to capitalize thefirst letter in the word.

With all of these tips, I recommend posting a reminder to yourselfsomewhere on your computer. It may take a few weeks to ingrain a new habitlike this into your command-line use, but once you get used to it, you won'tgo back. I've found that these shortcuts also apply in command-lineclients like Irssi, so if I notice I have a typo in something I'm about tosay in IRC, I can just press Alt-b until it's under the cursor, and press Alt-d todelete it, and then correct the error and press Enter.

author

Author Name

Author Description!

Get Free Email Updates to your Inbox!

Post a Comment

www.ccna.ma

yu
Powered by Blogger.

Translate

Copyright © Learn Linux Th3pro.xyz | Blogger Templates | Designed By Code TAOUSSI