Hitting the key sequence Ctrl-X Ctrl-E
opens the command line buffer in an editor, then executes the output as a command. (The $EDITOR
environment variable is used to select the editor binary.)
To see all available key bindings run bind -P
. To rebind the command to a simpler key combination, e.g. Ctrl-O
, add the following line to your .inputrc
file:
"\C-o": edit-and-execute-command
When I use the mv
command I often want to move the file in question to a slightly modified version of its current path. This little wrapper function saves on typing:
function mv { if [ $# -ne 1 ]; then command mv "$@" return fi read -ei "$1" new_filename mv -v "$1" "$new_filename" }
If you supply two or more arguments the default mv
command will run. If instead you supply a single argument you'll be given a chance to edit that argument using the standard line-editing shortcuts, then the file will be renamed to the edited string.
Moving more than one file? There's a tool for that.
Hitting Ctrl-Z
while a process is running temporarily suspends that process and restores control to the shell. In the shell, the jobs
command lists all currently active and suspended processes. Use the fg
command to restart a suspended process in the foreground or the bg
command to restart a suspended process in the background. Both comands take a numerical ID argument, available from the jobs
command, e.g.
$ fg %1
To start a process in the background append an ampersand to the command:
$ sometask &
By default Ctrl-P
(previous) and Ctrl-N
(next) scroll backwards and forwards through the command history.
This feature becomes much more powerful if you enable prefix-matching by adding the following lines to your .inputrc
file:
"\C-p": history-search-backward "\C-n": history-search-forward
With these settings, only commands matching the prefix you've already typed will be matched.
I think using the 'z' jump command has improved my everyday experience of Bash more than any other single tip, trick, or technique I've ever learned.
This command tracks your most used directories and lets you jump to fuzzily-matched directory names ranked by frecency, a combination of frequency and recency.
Vim really seems to struggle with syntax highlighting for Bash scripts. Adding the following line to your .vimrc
file forces Vim to use its builtin Zsh syntax plugin for all shell scripts:
autocmd BufNewFile,BufRead *.sh set filetype=zsh
Surprisingly, this actually works a lot better for Bash scripts than the default Bash plugin.
if test -f /path/to/file; then echo "file exists" fi
if ! test -f /path/to/file; then echo "file does not exist" fi