# Switching Virtual Terminals



## Geezer (Dec 14, 2021)

Switching virtual terminals can be done with the keyboard easily with <Alt><F_#_> and for graphical virtual terminals with <Ctl><Alt><F_#_>.

Is there a way to switch virtual terminal, both text and graphical, with a command?


----------



## Alain De Vos (Dec 14, 2021)

Probably not an answer,





						chvt.c - Busybox source code (0.46) - Bootlin
					

Elixir Cross Referencer - Explore source code in your browser - Particularly useful for the Linux kernel and other low-level projects in C/C++ (bootloaders, C libraries...)



					elixir.bootlin.com
				




But this is,





						FreshPorts -- sysutils/chvt: Change foreground virtual terminal
					

The command chvt N makes /dev/ttyv(N-1) the foreground terminal. The key combination Ctrl-Alt-FN (with N in the range 1-12) has a similar effect.  WWW: https://lists.debian.org/debian-bsd/2009/11/msg00006.html




					www.freshports.org


----------



## covacat (Dec 14, 2021)

vidcontrol -s ?


----------



## Geezer (Dec 14, 2021)

covacat said:


> vidcontrol -s ?



This works very well.

Can use this from a text console to another text console, or even to a graphical console. But cannot use this from a graphical console. So this is the equivalent of <Alt><F_#_>.

Is there an equivalent of <Ctl><Alt><F_#_> ?


----------



## covacat (Dec 14, 2021)

from xterm try to use vidcontrol -s #num < /dev/ttyv*
not sure if it is possible


----------



## Geezer (Dec 14, 2021)

covacat said:


> from xterm try to use vidcontrol -s #num < /dev/ttyv*
> not sure if it is possible



Using:

`vidcontrol -s`*X*`</dev/ttyv`*Y*

Yes, if I am additionally logged into the text console (Y + 1).

So if I am in graphical console 9, and wish to switch to graphical console 10, I would have to log in to one of the text consoles first.

Almost there.


----------



## kpedersen (Dec 14, 2021)

Geezer said:


> Switching virtual terminals can be done with the keyboard easily with <Alt><F_#_> and for graphical virtual terminals with <Ctl><Alt><F_#_>.


Can I just confirm what you mean by graphical terminal? Are you referring to XTerm via X11? Surely that keybinding depends on what window manager you use?


----------



## Zare (Dec 14, 2021)

He probably meant a tty with an active X session.


----------



## Geezer (Dec 15, 2021)

kpedersen said:


> Can I just confirm what you mean by graphical terminal? Are you referring to XTerm via X11? Surely that keybinding depends on what window manager you use?




Yes, an xterm session (or similar) in X. Keybindings are irrelevant because I want to use a command.


----------



## kpedersen (Dec 15, 2021)

Geezer said:


> Yes, an xterm session (or similar) in X. Keybindings are irrelevant because I want to use a command.


It is a bit naff but can you not send a key event to the WM via xsendkeys (i.e here)? Either modify this program or wrap it in a script.

Assuming then that your graphical terminals and on separate desktops. A specific key could be mapped to change to the respective one. Very possible in Fvwm, OpenBox, DWM, etc.


----------



## covacat (Dec 15, 2021)

the combination is parsed by the X server itself not the WM
you can try sending to the root window but i doubt it will work (i dont think that events sent that way will reach the required code path)


----------



## blind0ne (Dec 15, 2021)

Is it possible to leave terminal window opened on real computer, go home and connect through ssh, and attach to this existing terminal?


----------



## covacat (Dec 15, 2021)

blind0ne said:


> Is it possible to leave terminal window opened on real computer, go home and connect through ssh, and attach to this existing terminal?


with screen or tmux, yes


----------



## blind0ne (Dec 15, 2021)

covacat said:


> with screen or tmux, yes


how to do it?


----------



## Geezer (Dec 15, 2021)

kpedersen said:


> It is a bit naff but can you not send a key event to the WM via xsendkeys (i.e here)? Either modify this program or wrap it in a script.
> 
> Assuming then that your graphical terminals and on separate desktops. A specific key could be mapped to change to the respective one. Very possible in Fvwm, OpenBox, DWM, etc.





covacat said:


> the combination is parsed by the X server itself not the WM
> you can try sending to the root window but i doubt it will work (i dont think that events sent that way will reach the required code path)



Really, whether it works or not (which so far it does not), sending key events around to be acted upon, is a bit of a kludge, whereas what I want is the bit where it is acted upon.


----------



## kpedersen (Dec 15, 2021)

covacat said:


> the combination is parsed by the X server itself not the WM
> you can try sending to the root window but i doubt it will work (i dont think that events sent that way will reach the required code path)


It should do. You send it to the root window and the WM hooks the event and handles it.
You often need to make sure to label the event as fake.

For OpenCDE, I integrated this tool to send events directly to the WM (not as keypresses). You should be able to get it to change virtual desktops using X11 events / atoms.



			wmctrl - A command line tool to interact with an EWMH/NetWM compatible X Window Manager.


----------



## covacat (Dec 15, 2021)

kpedersen said:


> It should do. You send it to the root window and the WM hooks the event and handles it.
> You often need to make sure to label the event as fake.
> 
> For OpenCDE, I integrated this tool to send events directly to the WM (not as keypresses). You should be able to get it to change virtual desktops using X11 events / atoms.
> ...


he does not want to change virtual desktops
he wants one of the console vt's (the classic textmode console) like switching from xterm to ttyv1 (ctrl+alt+f1)


----------



## kpedersen (Dec 16, 2021)

Geezer said:


> So if I am in graphical console 9, and wish to switch to graphical console 10, I would have to log in to one of the text consoles first.


Would autologin to those text consoles be an option? There is an example here: https://forums.freebsd.org/threads/autologin-and-auto-run-x-server.6782/#post-43547

Be warned that it has changed a little. Your best bet is to read through the existing gettytab file where there is already a commented out autologin example for root. Just modify that one.


----------



## grahamperrin@ (Dec 18, 2021)

Geezer said:


> `vidcontrol -s`*X*`</dev/ttyv`*Y*



What exactly should I enter to switch to ttyv1?

Found: <https://cgit.freebsd.org/ports/commit/?id=53d1284c696e71eb2d6066141b60b3466ebccaab>



> … switch to the second virtual terminal (/dev/ttyv1):
> `# vidcontrol </dev/ttyv0 -s 2`



It works, but I don't understand _why_ it works


----------



## covacat (Dec 18, 2021)

-s1 but you have to be at one of the other ttyv's
from X/xterm you may try < some other /dev/ttyvn where you are already logged or something
i don't have a freebsd desktop so I can't test


----------



## grahamperrin@ (Dec 18, 2021)

covacat said:


> -s1 but you have to be at one of the other ttyv's



`-s1` seems to take me to ttyv0.

I found a working command (a postscript to my previous post); tested in Konsole.


----------

