Effective shell navigations & operations in bash
I see many of my colleagues still using arrow keys (↑
, ↓
, ←
, →
) to
navigate and Ctrl + C/D
to do copy/paste in the
bash shell environment, which is slow. In
fact, you could boost your productivity in just a few minitues by mastering the
following key bindings. I summarized them from many years of experience, and
here I try to show you the most handy ones.
Note: In the Mac OS X Terminal.app, the alt key doesn’t work by default. You
need to open the terminal setting (Command-,
), and in the Keyboard tab, tick
Use Option as Meta key
, and then you’re good to go.
Navigation:
Navigation by line, word or letter offers different levels of granularity.
Ctrl-a
: Go to the beginning of line, replacingHome
key. Think a as in ahead.-
Ctrl-e
: Go to the end of line, replacingEnd
key. Think e as in end. Alt-f
: Skip one word forward. Think f as in forward.-
Alt-b
: Skip one word backward. Think b as in backward. Ctrl-f
: Skip one letter forward, replacing→
key.Ctrl-b
: Skip one letter backward, replacing←
key.
Deletion:
Deletion by line, word or letter also offers different levels of granularity.
Ctrl-u
: delete to the beginning of line-
Ctrl-k
: delete to the end of line Alt-d
: delete to the next word-
Alt-Delete
: delete to the previous word Ctrl-d
: delete to the next letterDelete
: delete to the previous letter
Cut/Paste:
Ctrl-u
,Ctrl-k
,Alt-d
andAlt-Delete
: as introduced in the Delete section above not only delete the targeted content, but also store the deleted content in memory for later use, so they’re more like a typical Cut operation.Ctrl-y
: paste the cutted content. Think y as in yank. Yank is related to paste or copy for some reason I am not sure of. If you happen to know why, please drop me a message.Alt-y
: paste the previously cutted content in reverse order.
I don’t know any straightforward copy operation in the shell environment, but
it could be easily accomplished with 1 cut and 2 paste
operations. e.g. Ctrl-d
-Ctrl-y
-Ctrl-y
.
History commands management:
Ctrl-p
: Go back to a previous command, replacing↑
key. Think p as in previous.-
Ctrl-n
: Go to the next command letter forward, replacing↓
key. Think n as in next. Ctrl-r
: Reverse search history commands containing certain key word.Ctrl-s
: Forward search history commands containing certain key word. This is the opposite ofCtrl-r
, if it doesn’t work. Try executingstty -ixon
first, then if it works, putstty -ixon
in your~/.bashrc
.
Case manipulation:
Alt-c
: Capitalize a wordAlt-u
: Uppercase a wordAlt-l
: Lowercase a word
Space manipulation:
Alt-\
: Remove all continuous spaces starting from the current position of the marker on both sides.
Do/Undo:
Ctrl-Shift--
: this can do both do & undo. For exmaple, you’ve done some editing and get into the state of s4: s1-s2-s3-s4. You want to go back to s2, then you type the key binding twice, ending up at s2. If you type the key binding a third time, you’ll go back to s1. However, assume for some reason, actually you want to go forward to s3. To reverse the order of undo, you need to do some temprary editing (e.g. just type a space), and then type the key-binding twice, you now should get to s3. Hopefully, this little example demonstrates how do/undo works.
I think that’s all what you need, with a bit more practise, you’re well on your way to become an expert in the shell environment.
Bonus:
If you happens to use Emacs as your
favorite editor, most of the above commands (except Ctrl-u
as far as I am
aware of) applies to navigation and operations in the Emacs buffer, too. So that’s
killing two birds with one stone!
Of course, in Emacs, you will need to navigate more than a single line, e.g. paragraphs, pages, or the whole file. These commands are all available online, but may be a bit scattered. I will summarize those most handy ones to me in a later post.
Cheers.