10

I would like to change the default shortcut to switch back and forth to the tty. By default it is ctrl + alt + F#. I have tried making a custom shortcut using Ubuntu's keyboard settings but it only worked while using the graphical interface when i switch back to tty it doesn't work. So I need to figure out how to change the default binding of the ctrl + alt + F# shortcut to make a single key toggle back and forth between two TTYs.

5
  • 2
    Are you sure this isn't a duplicate of askubuntu.com/questions/77982/… Aug 15, 2012 at 10:21
  • What do you mean by "single key toggle back and forth between two TTYs?"
    – ish
    Aug 15, 2012 at 10:43
  • @jacob somewhat but maybe not exactly :)
    – Rinzwind
    Aug 15, 2012 at 11:18
  • Ah yes I see, this is requesting a single key that toggles rather than a different set of keys. Aug 15, 2012 at 14:01
  • I have a "60 percent" keyboard. The F1-F12 keys are combined with the top row number keys. I press <Fn>+<number> and it runs the F-number. However, I cannot run a <ctrl><alt><F1-F12> to switch TTL.1--TTL.7. The keyboard simply runs <ctrl><alt><1-7> not the switch TTL function. and holding down the <Fn> key as an additional shifter key has no affect. This message is to explain to folks the need for a good solution to the problems of using a "60 percent" keyboard with linux.
    – user12711
    Feb 11 at 14:17

3 Answers 3

11
+50

You can use bind to bind a key to some function. Here is what I did:

bind '"\ea": ". ~/newScript.sh\n" '

Here newScript.sh is the file which changes the tty and \ea means that whenever Alt+A is pressed, the script executes.

The contents of newScript.sh are:

#! /bin/bash
ttyNum="$(tty)"
ttyNum=$(echo ${ttyNum##*y})
ttyNum=$(( (ttyNum +1) % 7))
chvt $ttyNum

Note that if you want to store the key bindings permanently, you can store them in your `~/.inputrc' file. For more information, you can visit this link: http://www.techrepublic.com/article/find-and-bind-key-sequences-in-bash/5683375

Update: You can store the bind line in your ~/.bashrc file so that you don't have to do it again.

2
  • Note that this will not work if you are not logged in or if you are not running bash. Solution using remap.inc (see my answer below) will work whether you are logged in or not.
    – January
    Sep 10, 2013 at 11:20
  • The link says most keyboards come with an extra row of f1..f12 keys. But a popular new breed called "60 Percent Keyboards" only has 5 rows of keys, and no separate keypad, and no separate f1-f12 key-row above. Instead, to type numbers or f1-f12 function keys, it's all done by the old fashioned "top row" 5th row on typewriters, and "60 Percent Keyboards". These are popular with gamers,and with anyone who wants to key their hands on homerow. So, not having separate f1-f12 keys may be becoming more and more popular nowadays. And ctrl-alt-f1..f12 key press combination DOES NOT WORK.
    – user12711
    Feb 11 at 20:57
7

There is a command for that chvt.

chvt command stands for CHange Virtual Terminal. It takes one option which is a number which tells it the virtual terminal to switch to.

For example, if you are logged in at the first virtual terminal and you want to switch to the 7th where the xserver usually displays the GUI, then run chvt as follows :

chvt 7

And to return to the 1st virtual terminal, type :

chvt 1

So all you need to do is add that command to 7 shortcuts and you can switch.

2
  • 1
    How would you do that for when you press the key while in, say, tty1? Aug 21, 2012 at 5:58
  • @john: bind it. green7 already explained that.
    – Rinzwind
    Aug 21, 2012 at 7:04
7

EDIT: silly me! Just look into the file /etc/console-setup/remap.inc

You will find everything you need in that file. In any case, explanations are given below.

Unfortunately, "bind" will only work with a shell session.

What you need to do is to change the console mappings. You can do that using the program "loadkeys" (man loadkeys). First, dump the current mappings to see what the format of the file should look like:

dumpkeys > default_keys.txt

Note that you need to do that from a text console and not X, so press Ctrl-Alt-F1 first to switch to one!

Here is an example line from that file:

alt keycode  59 = Console_1

That means that the key that generates code 59, pressed with the modifier "alt" (defined somewhere else in that file) will generate an "Console_1" event -- switching to the virtual console 1.

How to find out which key generates which code? Use the program showkey for that. I start the program and press "F1":

komp $ showkey
keycode  59 press
keycode  59 release

The only way to exit this program is to close your eyes and contemplate the nature of Open Source for ten seconds. Or just to refrain touching the keyboard for ten seconds, that works as well. Use this method to find out what line in the keymap file you would need change the key mappings.

Say, you want to switch to the console 1 using "ctrl 1". You will find out that the keycode for 1 is 2. Save the following line to a file called "myownkeys.txt":

control keycode 2 = Console_1

Switch to a text console (if you haven't done it yet) and run

sudo loadkeys myownkeys.txt

Presto, Ctrl-1 should move you now to console 1. Add more lines to your files for your shortcuts. If you want to make it permanent, you can [EDIT] modify the file /etc/console-setup/remap.inc

By the way, apart from Console_n you also have "Decr_console" and "Incr_Console" -- switch to console on the left and console on the right, respectively. By default they are bound to alt-left and alt-right.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.