How to change command line shortcuts not listed in Preferences in GNOME Terminal [duplicate]

I would like to inspect / change / remap the keybindings in my Ubuntu server terminal. (I'm not using any GUI. I'm referring the bare-bones terminal, TTY1).

My core question is this:

  • Where is the configuration file for terminal keybindings?

Context: (I'm writing this context to help readers understand where I'm coming from.)

  • I would like to understand my "low level" terminal keybindings so that I can better configure and use Byobu.

  • I would also appreciate if an answer could:

    • explain the process order / stack of how the terminal gets started (because this will help me understand which processes are 'stealing' keybindings)

    • clarify if I'm using any terminology incorrectly


Solution 1:

Short Answer

To answer the question directly:

Where is the configuration file for terminal keybindings?

In my case (Ubuntu 16.04 with bash), Serge's answer is useful. (Though I'm not quite sure that it is complete in covering all key bindings that might conflict, which is why I'm writing this longer answer.) Bash configures keybindings in these files:

  • default key-bindings: /etc/inputrc
  • custom key-bindings: ~/.inputrc

Long Answer

In this section, I would like to be a bit more comprehensive. While bash and readline affect key bindings, they are other processes involved. For example, how does ALT-2 get bound to TTY #2?

Overview

As an overview, here are the pieces involved on my Ubuntu Server 16.04 machine. I start with systemd, Ubuntu's current init system.

  • systemd starts systemd_logind.
  • systemd_logind starts TTY1 with login.
  • login starts bash.
  • bash uses the readline library.
  • bash runs ~/.bashrc which ultimately starts byobu
  • byobu, on my system, uses tmux as a back-end

(Note: systemd is used in Ubuntu 15.04 and 16.04. Upstart was the init system for Ubuntu 6.10 to 15.10, according to https://en.wikipedia.org/wiki/Ubuntu_version_history.)

Practically speaking, the following components trap some keypresses:

  • systemd-logind (traps ALT-1, ALT-2 etc).
  • login
  • bash using the readline library
  • tmux
  • byobu
  • the TTY (TODO: I don't know if this is a part of systemd or login?)

systemd-logind

For Ubuntu 16.04, systemd-logind is where user logins start. According to the Ubuntu man page for systemd-logind .

systemd-logind is a system service that manages user logins. It is responsible for:

  • Keeping track of users and sessions, their processes and their idle state
  • Providing PolicyKit-based access for users to operations such as system shutdown or sleep
  • Implementing a shutdown/sleep inhibition logic for applications
  • Handling of power/sleep hardware keys
  • Multi-seat management
  • Session switch management
  • Device access management for users
  • Automatic spawning of text logins (gettys) on virtual console activation and user runtime directory management

See also the man page for logind.conf. I didn't see any mention there of terminal keybindings.

TTY / keyboard configuration

Related links about TTY / keyboard configuration:

  • https://wiki.debian.org/Keyboard
  • https://help.ubuntu.com/community/Custom%20keyboard%20layout%20definitions
  • Changing TTY keyboard layout on a server?

Tools for TTY / keyboard layouts:

  • https://github.com/simos/keyboardlayouteditor

bash (with the readline library)

The gnu.org bash reference has nice documentation about bash and readline functions.

These configuration files affect key bindings:

  • default key-bindings: /etc/inputrc
  • custom key-bindings: ~/.inputrc

tmux

tmux key bindings are described here: https://wiki.archlinux.org/index.php/Tmux#Key_bindings

A user-specific configuration file should be located at ~/.tmux.conf, while a global configuration file should be located at /etc/tmux.conf.

byobu

Byobu has default key bindings which can be user defined in /usr/share/byobu/keybindings/.

Work In Progress

Note: this answer is a work-in-progress. There may be mistakes. (I find it easier to state what seems to be true based on my reading and reasoning, and correct it if it is wrong.) I will update this and remove this notice when it is closer to completion.

Solution 2:

As per bash manual ( accessible via command line man bash ) , the custom keybindings can be configured via ~/.inputrc file. Custom keybindings are said to be set like this:

  When using the form keyname:function-name or macro, keyname is the name
   of a key spelled out in English.  For example:

          Control-u: universal-argument
          Meta-Rubout: backward-kill-word
          Control-o: "> output"

When the terminal starts and passes control to your shell(which is bash typically ), it reads the config files, including ~/.inputrc. The readline library is what in fact enables this behavior, and it also allows using vi or emacs editing mode. Other shells may not have support for readline, for example ksh, so you would really need to use vi/emacs editing more In this case, the shortcuts are the same as for vi and emacs text editors.

The byobu is not really related to ~/.inputrc and bash's readline: uses plenty of it's own custom keybindings and settings, which mostly come from the backend that it's using - screen or tmux. I suggest you read man pages and tutorials to get understanding of how these work.