Below are some essential shortcuts built-in in Bash (and in Zsh), which enables a user to quickly move around the current command, edit it, and utilize previous commands.
- Going through previous commands (history)
- Moving around the current command
- Editing current command
- Editing current command in Vi editor
- Retrieving previous command arguments
- Undo all the changes done on the current command
- Quickly jumping to the recent directory
Going through previous commands (history)
Typically, if you are about to execute a curl or some long command or kubectl commands, it would be best to check command history to see if you have already executed it previously.
Ctrl-r # Search commands backward in history
!! # Get the 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
Also, having the below configurations at ~/.bashrc or ~/.zshrc will help have more history commands.
# 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 the current command
Now let’s say you have a long command to move around the command; if you are using arrows, it will be time-consuming. Below shortcuts will help move around the 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 to delete a word or the rest of the command, check the 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 the 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 to 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 a previous command and want to quickly access previous command arguments like file name or folder name, then check the below shortcuts.
!* # Get all previous arguments
!:0 # Get just the command of the previous command
!:1 # Get the first argument of the previous command
!:2-$ # Get the second argument to last from the previous command
!^ # Get the first argument of the previous command
!$ : # Get the last argument of the previous command
Alt-. OR ESC-. # Get last argument of the previous command (on repeat will go to through history of arguments)
^abc^def # Substitute first occurrence of abc in the previous command with def$!!:s/abc/def/ # Same as above
!!:gs/abc/def/ # Does the same as above but replaces all occurrences of abc with def
for above !! and !# use :h for head of the word and :t for the tail of the word
Undo all the changes done on the current command
You made a lot of changes and want to undo the last 3 edits on the current command
C-x, C-u or C-_ : Undo changes done
Quickly jumping to the recent directory
If you are in the workspace directory and want to jump to previous directory quickly, you can use cd -
, which toggles between the current $PWD
and $OLDPWD
Note: cd
or cd ~
will change the directory to home directory.
– RC
Comments