Disclaimer: These work in my current setup using iTerm2 on macOS. Other environments may vary.
Keyboard shortcuts when typing at the command line
When typing a command name or path:
- Tab to complete it with the unique match in the file system
- double-Tab to display a list of completion options if there are multiple matches
Navigating within the line
- Ctrl+A to jump to the beginning of the line
- Ctrl+E to jump to the end of the line
- Alt+← to jump left by word
- Alt+→ to jump right by word
- Option+click to move the cursor with the mouse
- Ctrl+C will cancel the command being typed or executed and give a fresh prompt.
- Ctrl+W deletes left by a word
- Ctrl+K deletes (“kills”) the rest of the line
- Ctrl+U deletes the line up to the cursor
Accessing command history
- ↑ and ↓ to scroll through previous commands
- Ctrl+R to reverse-search commands by typing
Enter Ctrl+R, then type part of the command that you want to retrieve. If there are multiple matches, keep typing or enter Ctrl+R until the one you want displays.
- The recent bash command history is stored in the file ~/.bash_history
Some of the above commands will be familiar to Emacs users. Another one: Ctrl+Y pastes (“yanks”) material previously deleted by Ctrl+W, Ctrl+K, or Ctrl+U.
More keyboard shortcuts here.
In a file path:
- . stands for the current working directory (where you currently are in the file system when executing a command)
- .. stands for the parent directory
- ~ or $HOME stands for your user home directory
Variables and aliases
In Bash, the notation x=y (without spaces around the = sign!) sets a variable x to the string value y. If the string value contains spaces it should be surrounded with quotes. To get the value of a variable, precede it with $ — e.g.:
x='a long string'y=./$x.txtecho $y
./a long string.txt
Thus if you need to reuse a string in a bunch of commands in your current session, this can save you time.
To set an environment variable that can be accessed by programs you run at the command line in your current session, precede the variable name with the word export. This is usually used for special variables like PATH.
The alias command can be used to create a custom shortcut for frequent commands: e.g.
alias ll='ls -l'
will mean that the ll command acts as a shortcut for ls -l for the remainder of the session.
Storing variables/aliases across sessions
If you want to specify information that will live across Bash sessions, you can add commands to a script that runs automatically whenever Bash starts. This is particularly useful for aliases and for environment variables expressing something about the system configuration. On my system I have these commands in the script ~/.bash_profile (~/.bashrc and ~/.profile are similar but there are some technical differences in when they are executed).
Some of the contents of my ~/.bash_profile script:
alias ls='ls -G'
alias ll='ls -l'
alias lsa='ls -A'
The first 3 are aliases for variants of the ls command. The last 2 are environment variables that help Bash find executables for the MySQL installation.
If you modify ~/.bash_profile and want to import it into the current session, use the source command:
Thanks to Michael Kranzlein for providing feedback on a draft of this post.