Important shortcuts in Bash/Zsh

Rajashekar Chintalapati
4 min readJul 18, 2019

Below are some important shortcuts built-in in Bash (and also in Zsh) which enables user to quickly move around current command, edit it and also utilize previous commands.

Going through previous commands (history)

Typically if you are about execute a curl or some long command or some kubectl commands, it would be best to check command history to see if you already executed previously.

Ctrl-r # Search commands backwards in history
!! # Get previous command

Note: in above demo you see history of 10+ commands starting with echo showing up as I type, usually you will only see 1 command. If you want above behavior install install (Command-line fuzzy finder) fzf — https://github.com/junegunn/fzf

Also having below configurations at ~/.bashrc or ~/.zshrc will help to have more commands in history.

# Appends every command to the history file once it is executed
setopt inc_append_history
# Reloads the history whenever you use it
setopt share_history
# save each command's beginning timestamp and the duration to the history file
setopt extended_history
setopt hist_ignore_all_dups

Moving around current command

Now let’s say you have long command, to move around the command, if you are using arrows it will be time consuming. Below shortcuts will help move around current command quickly.

Ctrl-a # Go to the start of the command
Ctrl-e # Go to the end of the command
Alt-f # Go forward one word
Alt-b # Go back one word

Editing current command

If you want delete a word or rest of the command, check below shortcuts.

Ctrl-k # Cut from cursor to end of command
Ctrl-u # Cut from cursor to start of command
Alt-d # delete one word forward
Ctrl-w # delete one word backward
Ctrl-y # paste last cut word/line

Editing current command in Vi editor

Above shortcuts like Alt-d & Ctrl-w will work, but let’s say you want to edit current command in an editor.

Ctrl-x, e # Edit previous command in Editor

Note: If you are using ZSH, then add below lines in ~/.zshrc for above to work

autoload -U edit-command-line
zle -N edit-command-line
bindkey '^xe' edit-command-line
bindkey '^x^e' edit-command-line

Also you need set the editor (you can change this to emacs)

export EDITOR='vi'

Retrieving previous command arguments

If you created a directory or a file in previous command and if you want to quickly access previous command arguments like file name or folder name then check below shortcuts.

!*    # Get all previous arguments
!:0 # Get just the command of previous command
!:1 # Get first argument of previous command
!:2-$ # Get second argument to last from previous command
!^ # Get first argument of previous command
!$ : # Get last argument of previous command
Alt-. OR ESC-. # Get last argument of previous command (on repeat will go to through history of arguments)^abc^def # Substitute first occurance of abc in previous command with def$!!:s/abc/def/ # Same as above
!!:gs/abc/def/ # Does the same as above but replaces all occurences of abc with def

Retrieving current command arguments

If you want to create directory or file and immediately do some action on it. then you can refer current command arguments.

!# current command
!#:0 current command first word
mkdir test1; cd !#:1 to create directory and cd to that directory

for above !! and !# use :h for head of the word and :t for the tail of the word

Undo all the changes done on current command

You made a lot of changes and want to undo last 3 edits on current command.

C-x, C-u or C-_ : Undo changes done

Quickly jumping to recent directory

If you are in workspace directory and want to quickly jump to log directory, you can use cd - which toggles between current $PWD and $OLDPWD

Note: cd or cd ~ will change the directory to home directory.

--

--