Tuesday, October 14, 2008

Reclaim Ctrl+C and Ctrl+V for familiar Copy and Paste in gnome-terminal

Most people work with graphical desktops. Most desktop (GUI) applications use the Ctrl+C and Ctrl+V for Copy and Paste actions respectively. One gets use to these really fast. However, when working with bash shell running inside a gnome-terminal those key bindings mean something different i.e. Ctrl+C sends the kill signal to the foreground process and Ctrl+V is used for quoted insert functionality (i.e. to enter control keys literally). This is the legacy of the command line oriented Unix terminals based on tty (stty - program that controls the settings of tty devices).

In this entry I describe how to make Ctrl+C and Ctrl+V do Copy and Paste in gnome-terminals. Here is how to do it:

Open a gnome-terminal window and type the following commands:

> stty intr ^K # free Ctrl+C for copy
> stty lnext ^- # free Ctrl+V for paste
> stty -g
> stty -g > ~/.stty # store the settings in home directory
Add the following to .bashrc
case $- in
*i*)
stty `cat ~/.stty` # reload the stored stty settings
bind -u quoted-insert # unbind the quoted-insert function of bash - free Ctrl+V for paste
esac
Now using the gconf-editor, edit the gnome-terminal's key bindings (@ /apps/gnome-terminal/keybindings key).

Close and reopen the terminal window. And now Ctrl+C will copy and Ctrl+V will paste.

2 comments:

Sujit Nair said...

Out of curiosity, what do we need to do to revert the changes. I mean, reinstate the original/default actions for Ctrl+C and Ctrl+V. Can you put across the settings changes for these, 'coz I would really love to try to modify the actions, but would like to be aware of the changes to revert to default too. Amateur in Unix, sorry if its a very basic question!

Sandip Chitale said...

Basically

1. removing the file named ~/.stty

2. removing the :

case $- in
*i*)
stty `cat ~/.stty` # reload the stored stty settings
bind -u quoted-insert # unbind the quoted-insert function of bash - free Ctrl+V for paste
esac

block from .bashrc

3. resoting the gnome-terminal keybindings to original value will revert the changes.