📜 ⬆️ ⬇️

Hone the skills of the console

After I had a new monitor in the workplace, I began a new iteration of improving my “mouseless” ( mouse-less, approx. ) Experience. You know what that means, don't you? This means that every time you take up the mouse, 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 significant.

There is also the next level of “thoughtless” experience, when you try to avoid hard-to-reach keys, for example, Delete , Backspace , Escape or even Enter .

If you hold your hands in a standard position for 10-finger blind typing, it is more convenient to press Ctrl-m instead of reaching with your little finger to enter.
')
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 a start, a comparative table:
TitleTypical keyConvenient alternative
Start lineHomeCtrl-a
End of lineEndCtrl-e
One character forwardRight ArrowCtrl-f
One character backLeft ArrowCtrl-b
Delete the character under the cursorDeleteCtrl-d
Delete the character in front of the cursorBackspaceCtrl-h
Previous team in historyUp ArrowCtrl-p
Next team in historyDown ArrowCtrl-n
Enter commandEnterCtrl-j or Ctrl-m
As a bonus, here is another list of convenient combinations:
TitleKeyboard shortcut
Cut the previous wordCtrl-w
Cut all to end of lineCtrl-k
Cut all to the beginning of the lineCtrl-u
Paste previously cutCtrl-y
Undo the changeCtrl-_ (Ctrl+Shift+-)
Clear screenCtrl-l
Do not forget to try these combinations in other applications. I think you will be surprised how widely they are supported.

Why does it work


First of all, there is a widely used readline library. Try to remove it from your system, and you will find out how many applications depend on it ( THIS WAS A BAD JOKE. DO NOT DO THIS ). From the official home page :

The GNU Readline library provides a set of functions that allow the user to edit input commands right 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 "buns". 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 the Wimmers, however there is one major drawback: there is no indicator of the current mode.

readline provides most of the 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 Ctrl-m combination with the xev utility, you will see

  <code> 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
 </ code> 

that XLookupString returns 0x0d bytes for this combination. A 0x0d byte is nothing more than a carriage return control character, also known as \r . This control character tells the terminal to accept your command, working in the same way as 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 to this is no less interesting - Caret notation : a method of writing control characters using printed characters. The rule is very simple: just inject the seventh bit in the code of the control character, and get the corresponding printed one. On the one hand, everything is simple; on the other hand, it is impossible to reassign. But don't believe me, check it out here .

That's all. I hope the material was helpful. Improve!

PS The mode indicator in the readline is still present, starting with version 6.3. To do this, add set show-mode-in-prompt On to ~ / .inputrc. Thank you xaizek
PSS The ascii code table can be viewed with the man ascii command. Again, thanks xaizek

Source: https://habr.com/ru/post/266933/


All Articles