We optimize the work process in the console

    Everyone is used to editing text in text editors, notepads, web forms, etc. In the process of typing, we use the usual arrows, the “End” and “Home” buttons, the more experienced ones hold down the “Ctrl” and the arrows move according to the words (which, by the way, does not always work). And when switching to the console, we are guided by the same rules, without even knowing that bash offers very convenient tools and key combinations that greatly simplify the work and minimize the number of operations to complete the task. In addition, bash has convenient tools for working with history, lots of different permutations and other interesting functions. The most frequently used by me and any experienced administrator I will describe in this article.

    In order for everything described by me to work, you do not need to install anything before, you do not need to write scripts, configs, or other additions: all these tools work exactly the same in most modern bash interpreters that run on any UNIX system.
    My pedagogical experience suggests that practice is the best way to understand, love and get used to new functions, so I didn’t draw bare tables, but made a story, and I recommend to open a console and try everything that you haven’t used before reading an article, hands.

    Operations on the cursor

    Out of habit, the Home button is used to move to the beginning of the line, and the End button to move to the end. But how comfortable is it to reach for these buttons when typing a command? Bash suggests staying with the alphanumeric keyboard and using the combination “Ctrl + A” instead of “Home” and “Ctrl + E” instead of “End”. Sometimes, having typed a long command, you recall that one more thing had to be done before it: in this case, the optimal way would be to move “Ctrl + A” to the beginning of the line, append the “#” symbol (thereby commenting out the command, but leaving it in the history ), and press Enter, after which you can execute the desired command, find the commented out command in the history, remove the comment symbol and execute it.
    To clear the text before or after the cursor, the following key combinations are used: “Ctrl + K” - delete text from the current cursor position to the end of the line, and “Ctrl + U” - to the beginning of the line. And finally, by “Ctrl + C” you can delete the entire line at once.
    You can delete words immediately: “Ctrl + W” deletes the word to the left of the cursor, “Alt + D” - to the right. But the deleted words are also placed in the buffer, the last deleted word can be inserted starting from the current cursor position using the combination “Ctrl + Y”.
    Often in a hurry you can type 2 characters in the wrong sequence: in this case, it is convenient to use the key combination “Ctrl + T”, which is designed to replace the character before the cursor and the character under the cursor.

    History

    Usually the knowledge of the bash history subsystems is limited to the up and down arrows. In the work it is convenient to use the following features:
    • Exclamation mark. Calls some command from the history, and the history itself is displayed in numbered form on the screen by the history command. For example: you can execute the fifth command from the history by typing "! 5", and the previous one by "! -2". The previous command is called by "!!", and the first command in the list (in reverse order), starting with "ro" - with the combination of "! Ro".
    • Variables and substitutions.Very often you need to insert the last argument of the previous one into a new command. A vivid example: on the command ls we looked at the contents of a distant directory, and you need to go into it. Do not write the full path again? Here you can use the substitution "! $" Inherited from the C shell, or the native variable "$ _". Both will be replaced by the last argument of the previous command. The only negative is that it cannot be viewed and edited. In this case, it is advisable to use the combination “Alt +.”, Which will insert the desired argument at the current cursor position, and you can do anything with it. It is also worth noting that if the last argument of the previous command is a wildcard, then instead of substituting "! $" All the text corresponding to this mask will be used, and not the last element.
      A typo in the previous line can be fixed without delving into it: the replacement operation "^^" allows you to replace the text of the previous command with a new one and execute the new command. For example:

      [vorb @ localhost ~] $ ls -l / dev / dsa
      ls: cannot access / dev / dsa: There is no such file or directory
      [vorb @ localhost ~] $ ^ dsa ^ sda
      ls -l / dev / sda
      brw-rw ---- 1 root disk 8, 0 Apr 5 03:18 / dev / sda


      Here the erroneous dsa entry of the previous command is replaced with the correct sda, and the new command was completed successfully.

    Navigating the file system with

    the “cd” command, like the auto-addition of the “TAB” button, you will not surprise anyone. But often you have to run around the entire file system with the need to then go back. The bash mechanism based on the stack model is useful here, which is supported by the pushd and popd commands: pushd jumps to the target directory and pushes the absolute path of the previous directory onto the stack, while popd, on the contrary, selects the previous path from the stack and goes to it. Example:

    [vorb @ localhost ~] $ pushd / var / cache / urpmi / rpms /
    / var / cache / urpmi / rpms ~
    [vorb @ localhost rpms] $ pushd /etc/urpmi/mediacfg.d/Cooker-2010.0-i586 /
    /etc/urpmi/mediacfg.d/Cooker-2010.0-i586 / var / cache / urpmi / rpms ~
    [vorb @ localhost Cooker-2010.0-i586] $ popd
    / var / cache / urpmi / rpms ~
    [vorb @ localhost rpms] $ popd
    ~
    [vorb @ localhost ~] $

    In this example, I first went to the directory with the urpmi cache, then to the directory with the urpmi config, then went back to the directory with cache, and home. One of the most common tasks solved using this mechanism is to go to the directory with configs, then to the directory with the cache, with the logs, or something else, and be able to quickly go back.
    By the way, the “cd” command also has its own tricks: “cd” without parameters will change the directory to the home, “cd ~ user” - to the user's home directory.

    In principle, this is all that I wanted to talk about in the framework of this article. Of course, this is far from all, and if there is a desire to develop knowledge in this direction, I recommend the book “UNIX: Tools” by Jerry Peak, Tim O'Reilly and Mike Lukidis. It describes not only bash, but also C shell, and ksh, and how much I read it - I always find something new for myself.
    Good luck!

    Also popular now: