How do you assign commands to keys in Terminal?
Is there a solution to assign special key combinations to words in terminal use. For example the less command is very usefull and i use i a lot to pipe the output of another process through it.
The idea would be to set up special key combinations that are only active in terminal use assigned to write different commands? So pressing Ctrl + L in terminal window could write
| less
or Ctrl + G could stand for
| grep
Note: i just mean adding the letters to commandline not execute the finally. A similar way what's tabcompletion but more specific.
Solution 1:
Yes, you can use the bind command
bind '"\ey"':"\"less \C-m\""
That maps Alt-e to the less command and execute it (with the \C-m aka Ctrl-m )
Probably for the | you need to escape it.
bind '"\ey"':"\"\|less \C-m\""
If you want to just append it to the command line remove the *\C-m\""
Be careful as there are already some bindings defined:
Ctrl + A Go to the beginning of the line you are currently typing on
Ctrl + E Go to the end of the line you are currently typing on
Ctrl + L Clears the Screen, similar to the clear command
Ctrl + U Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H Same as backspace
Ctrl + R Let’s you search through previously used commands
Ctrl + C Kill whatever you are running
Ctrl + D Exit the current shell
Ctrl + Z Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W Delete the word before the cursor
Ctrl + K Clear the line after the cursor
Ctrl + T Swap the last two characters before the cursor
Esc + T Swap the last two words before the cursor
Alt + F Move cursor forward one word on the current line
Alt + B Move cursor backward one word on the current line
Tab Auto-complete files and folder names
The Control key, as you can see above, is made with C-m , so you can start the less command with Ctrl-g, as follow:
bind '"\C-g"':"\"\|less *\C-m\""
To get the key codes for the Alt (only for the ALT), you can use the read command from the shell:
@~$ read
^[y
^[y is equal to \ey
For more info, this question was answered also in stackoverflow:
https://stackoverflow.com/questions/4119991/bash-call-script-with-customized-keyboard-shortcuts