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?
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?
You can use
bind -x
to bind a command to a shortcut in Bash. For example, to bindgit status
command to Crtl+p shortcut you can use:Put it into
~/.bashrc
file to remember it. From man page:1. Automatically open a new terminal window, showing the output of your command
Since you need to keep the terminal open after the command ran, putting:
under a shortcut key combination, will do the job. It will open a new
gnome-terminal
window and run the command inside it.Add it to a shortcut
Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
to Ctrl+Enter
Note
Although your suggested shortcut works, you might pick another key combination then Ctrl+Enter, since it at least clashes with the
LibreOffice
shortcut to jump to a new page.Explanation
To have a new
gnome-terminal
window opened from command line, and run a command in it, you can use:However, although the command will run successfully, the terminal window will close immediately before you can read the output.
The section:
is then to keep the terminal open (until you hit Enter) after the command runs.
Other commands
This way, you can run any (complex) command in the terminal, without having it closed immediately:
will output (if
wmctrl
is installed):After pressing Enter the terminal will close.
2. Running a command in the currently active gnome-terminal window
Of course you could place a simple one-liner under a shortcut key (assuming you have
xdotool
installed):This would however type the command blindly, in any application, and typing the command is not the cleanest option.
The small script below therefore:
gnome-terminal
window (by its pid)gnome-terminal
window, presses Return.If not, the script does nothing, So it won't paste into other applications.
Since the script takes the targeted command as an argument, you can put multiple commands under multiple shortcuts.
The script
How to use
The script needs
xdotool
sudo apt-get install xdotool
Create a dirctory
~/bin
if it doesn't exist yet, either log out out/in or runsource ~/.profile
gterm_keys
(no extension) in~/bin
, an make it executableNow You can run any command, from a shortcut key, in the frontmost
gnome-terminal
window by adding the command:to a shortcut key, as described in
[1]
In case zsh is used instead of bash, the following line in
~/.zshrc
bindsgit status
to ALT+ENTER.To get ALT+ENTER in Bash, I use this line: