Turn on bash shortcuts using key-bindings and macros to supercharge your bash productivity.
'bashortcuts' or bash-shortcuts is a skinny python script that helps build a personalized inputrc, which is a customized configuration of GNU Readline, the input related library used by bash and most other shells. Additionally, it makes it easy to define macros that can be used to tie shell_commands and functions to key sequences.
For starters, each key press is handled by a binding in bash. These bindings can be viewed using bind -P
. So when you press the UP
arrow to go to the previous command you executed, it is translated into a character sequence of "[A" and is handled by the binding "\e[A": previous-history"
. This binding maps the key-sequence \e[A
with the readline function previous-history
. bashortcuts leverages these readline functions in addition to custom defined functions, and allows mapping them to key combinations easily.
Search through your history by partially typing a command. Hit CTRL
+UP
and CTRL
+DN
to cycle through auto-complete options from history. (startswith)
Hit SHIFT
+UP
and SHIFT
+DN
to cycle through all usages of a phrase from history. (contains)
Auto-complete when ambiguous:
- First
TAB
press yields the list of ambiguous matched (default behavior). - Subsequent
TAB
andSHIFT
+TAB
presses cycle through all the possible completions.
ALT
+BACKSPACE
to delete word before cursorALT
+DEL
to delete word after cursor.
List history usage. Use CTRL
+ H
while typing a command to view all past usages from history.d
Git shortcuts with key bindings.
CTRL
+G
+A
to getgit add
CTRL
+G
+O
to getgit checkout
CTRL
+G
+B
to getgit branch
CTRL
+G
+P
+O
to getgit push origin
- Clone the repository:
git clone https://github.com/nihal111/bashortcuts/
- Run the setup script.
cd bashortcuts
python setup.py
- The setup would proceed to take a backup of the files it modifies and saves it, by default, inside
bashortcuts/backups/BACKUP_<DATE>
. You can change the name of the folder, when asked. - The setup proceeds to make changes to .bashrc, .inputrc and creates a .bash_bindings file from the
inputrc_config
andbash_bindings_config
files in the bashortcuts directory. - After the setup completes, restart the terminal to make the changes take effect.
The inputrc_config
and bash_bindings_config
files can be edited and setup.py
re-run to update the settings.
This is (presently) a mirror of the ~.inputrc
file that will be created/updated. This is used to assign key sequences to readline functions, and set readline variables. It has a few settings defined and a few commented, for you to play around with.
This is (presently) a mirror of the ~.bash_bindings
file that will be created. This is used to bind keysequences to shell_commands and custom functions.
Global values are set in /etc/inputrc. Personal (local) user values are set in ~/.inputrc.
The settings in ~/.inputrc file will override the global settings file. Bash uses /etc/inputrc if there is no .inputrc for a user when /etc/profile is read (usually at login). Ref
The character sequence \e[1;5A
maps to Ctrl
+ UP
key combination.
These are ANSI control-code escape sequences that are transmitted when various non alphanumeric keys are pressed on a "terminal" keyboard. You can determine the character sequence emitted by a key by-
- Pressing Ctrl-v at the command line, then pressing the key you're interested in. Ref
- Run
sed -n l
and type the key combo followed by Enter on the keyboard. Ref - Run
showkey -a
and type in the key combinations.
Man page here. Bash bind reference here
bind -P
: Lists current readline function names and bindings in a user friendly fashion.
bind -p
: Display readline function names and bindings in such a way that they can be re-read, like bind -p > inputrc
bind -l
: Displays the list of all readline funcitons.
bind -x
: Bash-2.04 introduced option -x to bind key sequences to shell commands. Also allows binding a macro to a key sequence.
Readline offers a raft of functions. The Bash reference at gnu.org offers a complete listing of escape sequences and Readline functions.