Hone your console skills
- Transfer
- Tutorial
After I had a new monitor in the workplace, I started a new iteration of improving my “ mouse-less ” experience. You know what that means, do you? This means that every time you grab the mouse while removing your hand from the keyboard, you spend a little time and energy. If you need to type a lot of text (and I write a lot of code), this becomes essential.
There is also the next level "amyous" experience when you try to avoid the hard keys, for example
If you hold your hands in the standard position for 10-finger touch typing, it’s more convenient to press
In this article I will talk about convenient keyboard shortcuts that can be used in any terminal, although they work in many other places.
For starters, a comparison table:
As a bonus, here is another list of convenient combinations:
Do not forget to try these combinations in other applications. I think you will be surprised at how widely supported they are.
First of all, there is a widely used library
This means that the application can simply use this library for user input, and it will take care of all the "goodies." From the official documentation you can find out that it supports emacs and vi modes, where the first one goes by default. Vi mode is more familiar to wimmers, however, there is one significant drawback: there is no indicator of the current mode.
Do you remember the ASCII character table ? There at the beginning of the table there are 32 old unnecessary control characters. And if you look at the combination
which
Another interesting question is how does the system determine the relationship between the
That's all. I hope the material was helpful. Improve yourself!
PS The mode indicator is
P.SS The table of ascii codes can be viewed with the command
There is also the next level "amyous" experience when you try to avoid the hard keys, for example
Delete
, Backspace
, Escape
or Enter
. If you hold your hands in the standard position for 10-finger touch typing, it’s more convenient to press
Ctrl-m
instead of reaching for the little finger with your little finger.In this article I will talk about convenient keyboard shortcuts that can be used in any terminal, although they work in many other places.
For starters, a comparison table:
Title | Typical key | Convenient alternative |
---|---|---|
Start line | Home | Ctrl-a |
End of line | End | Ctrl-e |
One character forward | Right Arrow | Ctrl-f |
One character back | Left Arrow | Ctrl-b |
Delete character under cursor | Delete | Ctrl-d |
Delete character before cursor | Backspace | Ctrl-h |
Previous team in history | Up Arrow | Ctrl-p |
Next team in history | Down Arrow | Ctrl-n |
Command input | Enter | Ctrl-j or Ctrl-m |
Title | Keyboard shortcut |
---|---|
Cut previous word | Ctrl-w |
Cut all to end of line | Ctrl-k |
Cut all to the beginning of the line | Ctrl-u |
Paste cut before | Ctrl-y |
Roll back change | Ctrl-_ (Ctrl+Shift+-) |
Clear screen | Ctrl-l |
Why does it work
First of all, there is a widely used library
readline
. Try removing it from your system and you will find out how many applications depend on it ( THIS WAS A BAD JOK. DO NOT DO THIS ). From the official homepage :The GNU Readline library provides a set of functions that allow the user to edit input commands directly at the input location.
This means that the application can simply use this library for user input, and it will take care of all the "goodies." From the official documentation you can find out that it supports emacs and vi modes, where the first one goes by default. Vi mode is more familiar to wimmers, however, there is one significant drawback: there is no indicator of the current mode.
readline
provides most combinations, but not all. Among them are Ctrl-h
, Ctrl-m
, Ctrl-j
. But why do they work? I think you will like the answer. Do you remember the ASCII character table ? There at the beginning of the table there are 32 old unnecessary control characters. And if you look at the combination
Ctrl-m
with the utilityxev
you will seeKeyRelease event, serial 34, synthetic NO, window 0x1800001,
root 0x103, subw 0x0, time 17907461, (780,924), root:(3342,946),
state 0x4, keycode 58 (keysym 0x6d, m), same_screen YES,
" XLookupString gives 1 bytes: (0d) "
XFilterEvent returns: False
which
XLookupString
returns 0x0d bytes for this combination. And 0x0d bytes is nothing more than a carriage return control character , also known as \r
. This control character tells the terminal to accept your command, working just like it did Enter
. So, control characters are not so unnecessary (well, at least some of them). Another interesting question is how does the system determine the relationship between the
Ctrl-m
combination and the carriage return symbol. The answer is no less interesting - Caret notation: A method for writing control characters using printed characters. The rule is very simple: just inventory the seventh bit in the control character code and get the corresponding print. On the one hand, everything is simple; on the other, cannot be reassigned. But do not believe me, check yourself here . That's all. I hope the material was helpful. Improve yourself!
PS The mode indicator is
readline
still present, starting with version 6.3. To do this, add set show-mode-in-prompt On
to ~ / .inputrc. Thanks xaizek P.SS The table of ascii codes can be viewed with the command
man ascii
. Again, thanks xaizek