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
    be aware that this approach may overwrite a default combination that is already assigned. You may check which bindings are in use with bind -P – MestreLion Nov 22 '12 at 15:38
  • 1
    also, for this particular case, bind -x '"\C-b":"git branch"' seems much better, as it will issue the command without affecting (or being affected by) your current command line – MestreLion Nov 22 '12 at 16:45
  • '\C' stands for Control, what stands for Alt? – Ulad Kasach Apr 22 '16 at 00:46
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,086
  • That's a very good answer about aliases, but alas I don't want it to be an alias, specifically to take advantage of my right hand thumb. – EpsilonVector Nov 23 '12 at 23:11
  • @EpsilonVector: I fail to see why CTRL+b would be any different, "thumb-wise", than b+ENTER. Both are 2 keypresses. But hey, whatever floats your boat :P – MestreLion Nov 23 '12 at 23:21
  • Right after typing it's not different (you have to move your hand to the right to reach enter/ctrl), but in between typing ctrl is where my right thumb naturally rests. – EpsilonVector Nov 23 '12 at 23:31
  • @EpsilonVector: by the way... was git branch just an example or do you really just want to have a quick way to "know which branch you are" when using the terminal and browsing repositories? Because there are much better solutions for that using PS1 – MestreLion Nov 24 '12 at 07:49
  • Just an example... But PS1 looks very useful. Thanks for the tip. – EpsilonVector Nov 25 '12 at 07:02
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,061
  • I'm sure this is not what the OP wants. He wants to issue the command in his current terminal, as if he copy and pasted it. Besides, writing a 1-line shell script for a .desktop file is silly: you can simply invoke your command directly in the .desktop file – MestreLion Nov 22 '12 at 14:13
  • Besides, for the particular command of git branch such desktop file is useless, since it always defaults to $HOME as its working directory, and there is no way to tell the desktop file where the repository is. – MestreLion Nov 22 '12 at 14:31
  • @MestreLion the reason why I mentioned script in my answer. The script can contain cd /path/where/git/resides command before the git command – deshmukh Nov 23 '12 at 04:31
  • @MestreLion if the question is about a shortcut for a command in the current terminal, bash alias would be adequate – deshmukh Nov 23 '12 at 04:32
  • A script containing a path to a specific git repository is not much better: it would be useful only for that single harcoded repo. – MestreLion Nov 24 '12 at 07:45
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,222
  • 9
  • 81
  • 93