10

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.

1 Answer 1

10

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

7
  • thanks, how can i delete a binding or show all bindings?
    – NES
    Jan 4, 2011 at 14:06
  • something is wrong with your first 2 examples. it always prints also the wildcard after less after i assigned with your binding syntax.
    – NES
    Jan 4, 2011 at 14:10
  • bind -p and bind -P will show you all the bindings. I made a mistake with the star... I'm updating the answer
    – tmow
    Jan 4, 2011 at 14:38
  • thanks a lot. perhaps do you have an idea how to reach the same goal in fish? i get it so far to work but the problem is that fish seems to automatically execute the command instead of just adding the string to commandline by default. here is the manpage linux.die.net/man/1/bind Unfortunately the example there is not very helpful for my case.
    – NES
    Jan 4, 2011 at 14:52
  • This is already a built-in function I think fishshell.com/user_doc/index.html#editor suing the Alt-p shortcut
    – tmow
    Jan 4, 2011 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .