5

Is there a way to bind hotkeys to textual commands in the terminal? For example: ctrl+b == git branch <ret>

4 Answers4

9

You can use the bash bind command as seen in the linked Stackoverflow answer.

In your case, the command would be:

bind '"\C-b":"git branch\n"'

To make it stick from session to session then add it to your ~/.inputrc

Bash bind reference. (also available as man bash)

Tron
  • 568
2

Instead of a hotkey, how about a bash alias?

For example:

alias b='git branch'

now b is an alias for git branch, and you can simply type b <ret> in your terminal.

It is in a way much better than a hotkey, since you can still add other options to your command before hitting enter. For example:

b --all
b --remotes
b -m newbranch

... and so on.

To make this alias permanent, add it to the end of your ~/.bashrc or ~/.bash_aliases. The latter is preferred, but only works if your ~/.bashrc sources it. The default one for Ubuntu 12.04 does.

You can check if a given command is already assigned to an alias, program, function or builtin using type <command>:

$ type b
b is aliased to `git branch'

$ type cp
cp is /bin/cp

$ type cd
cd is a shell builtin

$ type quote
quote is a function
quote () 
{ 
    echo \'${1//\'/\'\\\'\'}\'
}

$ type c
bash: type: c: not found

Last but not least, it is also worth checking availability for commands that does not exist in your system, but may exist in Ubuntu's repositories:

$ blender
The program 'blender' is currently not installed.  You can install it by typing:
sudo apt-get install blender

This way your alias don't shadow any (current or future) command

MestreLion
  • 20,726
0

Sure.

The best way would be to:

  • Write a script with the command you want to execute
  • Create a .desktop file for this .sh file
  • Assign a shortcut key to this .desktop file
deshmukh
  • 4,121
0

A program like xmacro may be of help:

 xmacrorec can be used to record mouse and keyboard events on any X11 display.
 .
 xmacroplay can be used to playback recorded events or send any other
 mouse/keyboard events you choose. It is very handy for scripting an
 X display - for example controlling a presentation in mgp or ultrapoint
 from a script, network connection...
 .
 xmacroplay-keys is a script to help use the above.

See this question and its answer for more details.

roadmr
  • 34,802