Ubuntu – How to create a keyboard shortcut for a terminal command

command linegnome-terminalshortcut-keys

I often work with git and loved iTerm2 for the ability to set keyboard shortcuts for commands. For example, I set CTRL+ENTER to git status.

Is this possible with gnome-terminal or can you recommend another option to achieve this?

Best Answer

You can use bind -x to bind a command to a shortcut in Bash. For example, to bind git status command to Crtl+p shortcut you can use:

 bind -x '"\C-p":git status'

Put it into ~/.bashrc file to remember it. From man page:

-x keyseq:shell-command

Cause shell-command to be executed whenever keyseq is entered. When shell-command is executed, the shell sets the READLINE_LINE variable to the contents of the Readline line buffer and the READLINE_POINT variable to the current location of the insertion point. If the executed command changes the value of READLINE_LINE or READLINE_POINT, those new values will be reflected in the editing state.