Hone your console skills

Original author: Sergiy Kukunin
  • 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 Delete, Backspace, Escapeor Enter.

If you hold your hands in the standard position for 10-finger touch typing, it’s more convenient to press Ctrl-minstead of reaching for the little finger with your little finger.

image

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:
TitleTypical keyConvenient alternative
Start lineHomeCtrl-a
End of lineEndCtrl-e
One character forwardRight ArrowCtrl-f
One character backLeft ArrowCtrl-b
Delete character under cursorDeleteCtrl-d
Delete character before cursorBackspaceCtrl-h
Previous team in historyUp ArrowCtrl-p
Next team in historyDown ArrowCtrl-n
Command inputEnterCtrl-j or Ctrl-m
As a bonus, here is another list of convenient combinations:
TitleKeyboard shortcut
Cut previous wordCtrl-w
Cut all to end of lineCtrl-k
Cut all to the beginning of the lineCtrl-u
Paste cut beforeCtrl-y
Roll back changeCtrl-_ (Ctrl+Shift+-)
Clear screenCtrl-l
Do not forget to try these combinations in other applications. I think you will be surprised at how widely supported they are.

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.

readlineprovides 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-mwith the utilityxevyou will see

KeyRelease 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 XLookupStringreturns 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-mcombination 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 readlinestill present, starting with version 6.3. To do this, add set show-mode-in-prompt Onto ~ / .inputrc. Thanks xaizek
P.SS The table of ascii codes can be viewed with the command man ascii. Again, thanks xaizek

Also popular now: