i cli Kung Fu · Dark Matter Industries

cli Kung Fu

I am going to reveal a few secret moves that will turn you into a command line interface ninja on bash and zsh. If you have been using the up-arrow key to look for past commands, then using the left arrow key repeatedly until you get to what you want to modify, then painstakingly delete and insert words, be prepared to take your kung-fu to the next level.

For the moves below, “C” is shorthand for the control key. To really be a black belt, you want to remap the useless caps lock key on your keyboard to “Control”. That way your hand strays less from the home row. You touch type, right?

Editing

C-u		delete whole line
C-k		delete from cursor to the end of the line

C-h		backspace (like the mac backspace key)
C-d		delete one character (like the PC delete key)

C-w		delete from cursor to start of _preceeding_ word

C-t		take the letter left of the cursor and move it one word right!

C-y		restore (paste last deleted text)
C-j		jusr like return

C-t can be memorised as “transpose”. Handy for typos like “gerp”.

C-y can be memorised as “yank”, as in yank back from the blackhole and restore.

Navigation

C-a		move to start of the line
C-e		move to end of the line

C-f		move cursor forward 1 character
C-b     move cursor back 1 position

C-n		next command in history
C-p		previous command in history

C-l		clear screen

C-o		run the command on the command line and insert the next command in the history. Useful for running a number of commands in history serially

Why have C-f and C-b when you have the arrow keys? This is so you do not need to stray away from the home row, improving speed and smugness.

Of course you have the bck-i-search:

C-r		bck-i-search: use C-r to scroll back and backspace key to move forward!

So just do C-r and start typing any words in the command that you issued in any of your sessions. Note that you should set up your history configuration properly such as extending the number of lines preserved and defining what gets discarded when you run out of space, etc. The default settings may not be sufficient to support your cli-fu.

Advanced Stuff

You may have noticed that the key bindings above look very familiar. In fact, they are for the most part, emacs key bindings.

In zsh the zsh line editor or zle is what you interact with on the command line, and you can change the key bindings individually, or select to use other sets, such as vi key bindings.

If, like me, you use vi or vim, add the following to your zsh config to use vi key bindings: bindkeys -v.

You can actually list out all of the keybindings from the command line if you are running zsh. A keymap is a group of key bindings. vi and emacs are two such keymaps.

 bindkey      # all key bindings of the current keymap
 bindkey -l   # a list of available keymaps
 bindkey -M <keymap>   # once you know the name of a keymap, use this to list all bindings