Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers 4

up vote 3 down vote accepted

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)

share|improve this answer
    
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
    
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
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
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 7:49
    
Just an example... But PS1 looks very useful. Thanks for the tip. –  EpsilonVector Nov 25 '12 at 7:02
add comment

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
share|improve this answer
    
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 4: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 4: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 7:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.