39

I am using Kubuntu 10.04. I would like to change some of the standard shortcut keys for bash (terminal).

Here are the shortcuts I would like to set up:

  • Ctrl-C to copy the selected text in the terminal to the clipboard.

  • Ctrl-V to paste from the clipboard into the terminal.

  • Ctrl-Z to undo the editing on the current line in the terminal.

  • Ctrl-Shift-C (or even better, Super-C) to terminate the command.

  • Ctrl-Shift-Z (or Super-Z) to be the background command.

  • Ctrl-Shift-V (or Super-V) to be the literal insert command (or whatever Ctrl-z did before).

How do I make these changes?

BTW, I like the way the terminal works on OS X. The use of the command key makes all the copy/paste commands very consistent (unlike many other things in OS X).

P.S. I read the bash man page and it didn't help. It gave me ideas, but I need specific examples for the above combinations. Thanks.

Linger
  • 3,332
  • 10
  • 38
  • 47
Paul
  • 633

10 Answers10

20

Those aren't features of bash, they're features of the terminal driver. As such, they're specified by stty(1).

$ stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;
swtch = M-^?; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;
lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff
-iuclc ixany imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
echoctl echoke
14

This is almost completely solved now. Part of the solution was first brought to my attention by MTK358 over at https://www.linuxquestions.org/questions/linux-software-2/change-bash-shortcut-keys-such-as-ctrl-c-818170/. He pointed out the (rather obvious, much to my embarrassment) fact that the copy and paste shortcut keys can easily be changed by the menu settings of konsole. Once I saw this, it took about 1 minute to set up Ctrl-C and Ctrl-V to copy and paste with the terminal.

The other part of the solution should have been clear from the stty man page, but it wasn't (to me). I ended up just experimenting based on various clues, such as those posted above.

I decided which non-numeric key I wanted to replace Ctrl-C as the terminate command. (I used "k"). I never did find any info about how to convey a shift modifier to stty, so I gave up on that. With that decided, all I had to do was enter the following on the command line:

stty intr ^k

I verified that everything worked as desired.

Now I just had to place the single command above (stty intr ^k) in a startup script. I'm not sure which one is the "proper" one. I'd like this change to be system wide and permanent.

Any suggestions as to which script I should place the command in?

And finally, as to the preachy replies, they are unnecessary and unhelpful.

Paul
  • 633
9

I run Ubuntu and changed the shortcut for Copy from the default Ctrl+Shift+C to the more normal and simple Ctrl+C. This however made me run into problem not being able to send the interrupt command ^C.

Thanks to @Paul answer I found running the command

tty intr \^k

Will allow me to send the interrupt command using Ctrl+K. However this command needed to be run every time I open the terminal which is a hassle (just like he mentioned himself).

With a little bit of further research (googling ) I found how to execute a command each time the terminal is opened (in Ubuntu anyway).

Thus, finally running the following command (once) (in Ubuntu 18.04 and likely more) will give the expected behavior everytime.

echo $'# Make Ctrl+K interrupt command in terminal\nstty intr \^k\n' >> ~/.bashrc

Finally, it's worth to mention that you must not use the K button. You may exchange the k letter in all commands above to any letter key of your choice.

Olindholm
  • 191
6

Some of thse can be set using the normal KDE keyboard shortcut mechanism. In Konsole's "Settings" menu, click on "Configure Shortcuts", and you'll get a dialog which you can use to change the key bindings for the functions which are handled by KDE. That will allow you to remap Ctrl+C to copy and Ctrl+V to paste - but keep in mind that once you do that, those key sequences will no longer be passed through to the terminal, so you won't be able to use Ctrl+C to interrupt a program, for example.

Other functions are controlled by the readline library, which you can configure by editing the file ~/.inputrc. To get Ctrl+Z to revert any edits made on the current line, you'd want to add

C-z: revert-line

but that key sequence is probably already trapped by the terminal, so you might have to use stty to unbind it before it'll work. First look for ^Z in the output of stty -a (as shown in Ignacio's answer) and then, for example, if it shows up in susp = ^Z, run

stty susp ^-

to unbind that key mapping.

The other changes you're asking about would have to be done with stty as Ignacio says, since those are terminal functions, but I'm not sure exactly how. The issue is that I don't know whether the terminal recognizes Shift, or if it does, how to convey that information to stty.

David Z
  • 6,785
3

Bash uses emacs or vi mode on the command line. It has no concept of "selection" or "clipboard". It uses things like a "kill buffer" and "point", "mark", "word", "line", "kill" and "yank", etc.

Trying to shoehorn GUI-style functionality into it will be frustrating at best and won't work fully. You will have to try to combine functions of stty, readline and your terminal (presumably Terminal.app for you). You will need to remap functions such as intr, susp, lnext, etc., so you can still use their features.

I strongly recommend against such radical modifications. It just wasn't designed to do what you want.

When in Rome...

2

I reckon this is an old post, but the following can be handy.

I am using the Mate Terminal on Ubuntu 18.04. At the GUI interface of the Mate Terminal it is possible to configure the keyboard shortcuts Crtl+C for copy, and Ctrl-V for paste. When doing that, the Mate Terminal automatically swaps the intr function to the shortcut shift-crtl-c, so it works just out of the box.

Another hint is that we can use the mouse for smart copy/paste operations. When right-clicking in any application, not only for terminal, it will copy the selected text automatically, and the paste function can be done with the middle button. Below you can find more information on that.

https://www.howtogeek.com/440558/how-to-copy-and-paste-text-at-linuxs-bash-shell/

aotherix
  • 121
2

Source: https://superuser.com/a/1726410/1701302

For BASH

Add to your ~/.bashrc the follow command:

bind '"\C-f": "\C-a date \C-j"' # CTRL+F

For ZSH

Add to your ~/.zshrc the follow command:

bindkey -s "\C-f" "\C-a date \C-j" # CTRL+F
Pedro
  • 21
1

For BASH: Add edit your ~/.bashrc or ~/.bash_aliases

For ZSH: Edit your ~/.zshrc

And add the follow command:

# For bash
bind '"\C-f": "php -v"' ## only type text, NO RUN
bind '"\C-f": "\C-a php -v \C-j"' ## run php -v command

For ZSH

bindkey -s "\C-f" "php -v" ## only type text, NO RUN bindkey -s "\C-f" "\C-a php -v \C-j" ## run php -v command

When you press CTRL+F will appear php -v inline.

For a valid command, use like this:

# For BASH
bind '"\C-f": "\C-a date \C-j"'

For ZSH

bindkey -s "\C-f" "\C-a date \C-j"

When you press CTRL+F will appear the current time inline like:

ter 14 jun 2022 09:43:19 -03

Resume:

## For BASH
# Add to your ~/.bashrc or ~/.bash_aliases the follow command:
bind '"\C-f": "\C-a YOUR_COMMAND \C-j"'

Example:

bind '"\C-f": "\C-a echo $(date) \C-j"' # CTRL+F run YOUR_COMMAND

For ZSH

Add to your ~/.zshrc the follow command:

bindkey -s "\C-f" "\C-a YOUR_COMMAND \C-j"

Example:

bindkey -s "\C-f" "\C-a date \C-j" # CTRL+F

1

Here's the steps summarized:

  1. Go to terminal settings, change Copy from ctrl+shift+C to simply ctrl+c
  2. Done.
  3. Use ctrl+shift+C in the terminal if you need to abort a program.

And that without the need to hassle with bind, bindkey, tty intr \^k, your .bashrc or anything else other than your terminal's shortcut preferences window.

Full explanation:

You can use showkey -a to display the keys you're entering.

After turning off the terminal's own shortcuts in the terminal's settings, ctrl+c is ending up the same as ctrl+shift+C, both as ^C.

In other words: For the program in the terminal it doesn't matter if you press shift or not.

Additionally, shortcuts processed by the terminal will not be send to the program in the terminal.

This means in conclusion, that you can simply bind the terminal's copy/paste keys to the non-shift variants for the terminal app side, and use the commands for programs running in the terminal with shift added.

Tested on Fedora 40.

0

Thanks to this answer: https://stackoverflow.com/questions/18900803/persistent-stty-settings, I put stty intr ^D in my .bashrc (or .profile) and it worked on a new konsole window. I also set the Copy shortcut to Ctrl-C in the Konsole Shortcuts Settings. Ctrl-Z can be set in .inputrc as @David Z stated.

alchemy
  • 290