Advanced Shell Topics: Command line movement and editing


Since these examples are based more on cursor location than program output, I will need to use some Dynamic HTML to indicate where the cursor is at by inverting the color of the text and background over the cursor location. If you are using lynx or some browser that doesn't support DHTML then you probably won't understand this page very well.

Besides just using the arrow keys and doing tab completion of filenames, there are many other ways to move around on the command line in the bash shell. By default your shell is in emacs editing mode. You can set it up to use vi editing mode, but I've found it a rather difficult to get used to even though I'm an avid vi user.

Also, to show what character sequence I am typing in on the command line to move around there will be a line inbetween the before cli and after cli of typing in the sequence.

First of all, let's talk about moving your cursor around. Say that you've typed in a big long change directory command line and you realize that you forgot the c in cd. You can use the Ctrl-a sequence to move to the beginning of the line and Ctrl-e to move back to the end.

[user@host www]$ d /usr/local/src/apache-1.3.14/modules
[user@host www]$ d /usr/local/src/apache-1.3.14/modules    (Ctrl-a)
[user@host www]$ cd /usr/local/src/apache-1.3.14/modules
[user@host www]$ cd /usr/local/src/apache-1.3.14/modules   (Ctrl-e)

Just remember the e in Ctrl-e means end and the a in Ctrl-a doesn't mean anything. I guess you could think of the a as in the beginning of the alphabet.

If you want to go back and forth a word at a time you can use Esc-b and Esc-f for forward(f) one word and backwards(b) one word at a time.

[user@host ~]$ echo "The quick brown fox" 
[user@host ~]$ echo "The quick brown fox"  (Esc-b)
[user@host ~]$ echo "The quick brown fox"  (Esc-b)
[user@host ~]$ echo "The quick brown fox"  (Esc-b)
[user@host ~]$ echo "The quick brown fox"  (Ctrl-a)
[user@host ~]$ echo "The quick brown fox"  (Esc-f)
[user@host ~]$ echo "The quick brown fox"  (Esc-f)
[user@host ~]$ echo "The quick brown fox"  (Esc-f)

In the above example the cursor jumps around until it reaches a space, but in this next example you'll see that it doesn't just use the space character as a word sepearator.

[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm 
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)
[root@host ~]# rpm -Uhv gimp-1.1.29-1.i386.rpm   (Esc-b)

Not very useful in this situation, is it? This is because emacs mode counts a word as a set of alpha numeric characters. Since characters like the dash, period and underscore are not alphanumeric, the backwords and forwards word searches stop at those characters.

You can also remove words and whole lines at a time, which will put them into a buffer from which you can paste out of later. This is done using Esc-DEL, Esc-d, Ctrl-k and Ctrl-y. Esc-DEL is Escape and the Delete key and it will delete(cut) the previous word. Esc-d does the same but forward one word, Ctrl-k will cut from the current cursor position to the end of the line and Ctrl-y will paste what is in the cut buffer to the current cursor position.

[user@host ~]$ echo four two five three one 
[user@host ~]$ echo four two five three one      (Ctrl-a)
[user@host ~]$ echo four two five three one      (Esc-f 2 times)
[user@host ~]$ echo four  five three one         (Esc-d)  ["two" in paste buffer]
[user@host ~]$ echo four five three one          (Esc-f 2 times)
[user@host ~]$ echo four two five three two one  (Ctrl-y)
[user@host ~]$ echo four five three two one      (Ctrl-a)
[user@host ~]$ echo four five three two one      (Esc-f) 
[user@host ~]$ echo five three two one           (Esc-d)  ["four" in paste buffer]
[user@host ~]$ echo five three two one           (Esc-f) 
[user@host ~]$ echo five four three two one      (Ctrl-y)
[user@host ~]$ echo five four three two one      (Ctrl-e)

Well I guess I didn't get to demonstrate Ctrl-k or Ctrl-DEL, but I think you get the idea.

[user@host www]$
© 2000 Suso Banderas - suso@suso.org