Linux bash shell Learning (4): editing commands

Source: Internet
Author: User

This article is the second chapter of "learning the bash shell" 3rd edition, command-line editing, Reading Notes. But we will not limit this.

In general, I just try to press it directly. If an error occurs, use the cursor and arrow on the left or right to modify the image, or use the upper/lower cursor to directly use the previous command or previous command to modify the image. That's all. I use VI and almost do not use Emacs. The author of this book said that the command can be compiled just like using vi or Emacs. In the future, I have installed Emacs on cygwin.

History commands are recorded in ~ /. Bash_history, which is determined by histfile. You can use echo $ histfile to view the information. When we log on to login or open a terminal for Shell human-computer interaction, bash extracts the previous historical commands from. bash_history. In cgywin, when we exit the window, it will be saved. Not every Bash is saved in real time, but it cannot be determined for other Linux systems. We can use the up/down cursor key to get History commands, and use the history command to list all of them (including commands that have not been written into histfile ).

Emacs Mode

By default, bash uses the Emacs mode for compilation. You can use set-O Emacs | Vi to specify the method we use. The following describes the compilation method:

I. Operations on Characters

: This book mainly introduces operations based on keybroad. It is an old method. For example, if there is no optical tag key, or in some remote operations, such as serial port connection, the optical tag key cannot be used. These situations are rare now, so we do not need to remember them. The following is only a copy. After modification, you do not need to move the cursor to the end. You can press the Enter key to execute the command.

  • CTRL-B, move the cursor backward (left), equivalent to the left cursor
  • CTRL-F, cursor forward (right shift), equivalent to right cursor
  • DEL: Delete the next character (in fact, it is to delete the current character, move the cursor forward)
  • CTRL-D, delete the previous character (in fact, delete the current character, move the cursor behind), this needs to be careful, because if nothing happens, press the CTRL-D, equivalent to logout or close this terminal.

2. Operations on word

: I have never used word-based operations. They are all characters used by the land. In fact, it still works does not matter. In Emacs mode, subtitles and numbers are used as a word. If "-" is used in the middle, two words are considered.

  • ESC-B, move the cursor behind a word
  • ESC-F, move the cursor forward a word
  • ESC-DEL, delete the current or the following word, actually I didn't work in cgywin
  • ESC-CTRL-H, removing the front word, or the front part of the word cursor
  • ESC-D, removing the word behind, or the part behind the word cursor
  • CTRL-W, removing the front word, or the front part of the word cursor
  • CTRL-Y, reply to the previously deleted part.

3. Operations on rows

:

  • CTRL-A, move the cursor to the front, equivalent to Home, so there is no need to remember so much
  • CTRL-E, move the cursor to the End, equivalent to End, so there is no need to remember so much
  • CTRL-K that removes the current cursor from the last content and can be restored with a CTRL-Y. In fact, the CTRL-Y will remember the part of the last operation deleted and mark the parse in the current light.

4. Operations on historical commands

: This is very common. In fact, we can simply use the up/down cursor key. The following is a record:

  • CTRL-P, previous command, same as the optical tag key
  • CTRL-N, the next command, with the bottom cursor key
  • CTRL-R, query, if not found, will issue beep
  • ESC-<, the first command of history. I tried it in cgywin. It is invalid.
  • ESC->, the last command of history. I tried it in cgywin and it was not stable.

5. Complete Text operations

: We do not want to enter a long command or a long file name. We only need to enter the first letter. without ambiguity, the complete word is automatically displayed, I only use TAB. This method can be used to simplify input, including paths, commands, and functions. commands and functions have higher priority than paths (file names. In addition to the prefix, we can directly press the tab to give the matched content, or we may give the matched content after a long time. For example, we have two files abc123 and abcdef, And we press tab, abc can be obtained, and exact matching cannot be performed later. If we do not know the specific name, we can press tab, then the system will give abc123, abcdef for our reference.

  • ESC -?, It can display commands that may be used for perfection and act like continuous tabs.
  • ESC-/, try to complete the file name
  • CTRL-X/, displaying possible full file names
  • ESC -~, Try the possible full username (in fact, You need to press three keys, because ~ Shift is required)
  • CTRL-X ~, Display possible full usernames. I tried in cgywin, invalid
  • ESC -~, I'm not sure what it means to try vairable.
  • CTRL -~, Complete vairable list, not sure what it means
  • ESC-@, try to complete the hostname
  • CTRL-@ to list the complete hostname
  • ESC -!, Command complete
  • CTRL-X !, This indicates first press CTRL-X, then press! (Shift-1), listing all possible commands
  • ESC-TAB, trying to complete the Command Based on the history list

Vi. Other operations

: I think this operation is hard to remember. I only want to record it here, so that we can understand how many editing capabilities bash can provide.

  • CTRL-J, equivalent to RETURN, that is, carriage RETURN, which is LINEFEED in ASCII, which is allowed in UNIX to replace RETURN
  • CTRL-L, similar to clear, clears the screen and places it at the top of the screen
  • CTRL-M, equivalent to return, that is, carriage return
  • CTRL-O, equivalent to return, then display the next command in history
  • CTRL-T, swap two characters
  • CTRL-U, removing start to cursor
  • CTRL-V, added quote, but I'm not valid in cgywin
  • CTRL-[, equivalent to ESC
  • ESC-C, capital the letter, and move the cursor to the next word
  • ESC-U, which converts the letter at the bottom of the cursor to uppercase and moves the cursor to the next word
  • ESC-L, change the letters of word that are placed after the cursor to lowercase, and move the cursor to the next word
  • ESC-., add the last word of the previous command to the cursor
  • ESC-_, same as ESC -.

 Vi Mode

The vi mode is not the default mode and needs to be entered through set-o vi. Vi is a good text compiler, for example, used for C programming, but in bash, it may have more keys than Emacs. It can be divided into input mode and control mode.

Input mode

Including:

  • DEL: delete character, move the cursor behind
  • CTRL-W: delete previous word
  • CTRL-V: quote the next character, invalid in cgywin
  • ESC: Enter Control Mode

InControl Mode

. In vi, word can be divided into two methods: non-blank, which is a word divided by space, and word composed of letters and numbers, by default, we refer to the following one.

Control Mode-mobile

  • H, move one character to the left
  • L, one character to the right
  • B. Move one word to the left.
  • W, move one word to the right
  • W, move to the next non-blank word
  • B. Move it to the previous non-blank word
  • E. Move to the end of the current word.
  • E, move to the end of the current non-blank word
  • 0, move the beginning of the row
  • ^, Move to the first non-blank word in the row
  • $, Move to the end of the row

In the last three methods, we can use numbers to represent repeated operations, for example, 7 h, which is equivalent to 7 h in a row.

Control Mode -- re-enter input:

After entering the control mode, we want to exit and re-enter the input mode. The following methods can be used:

  • I, indicating the current insert
  • A, indicating adding
  • I, indicating joining at the beginning of the row
  • A, indicating adding at the end of the row
  • R, indicates rewriting (r indicates replacing a character, still in control mode)

Control Mode -- delete:

The vi mode provides powerful deletion functions. Even if I am very familiar with vi, I still seldom use it. The record is as follows:

  • Dh: delete a character, move the cursor behind
  • Dl: delete a character and move the cursor forward
  • Db: Delete the front part of the word where the current cursor is located. If the cursor is not located in the word, delete the previous word
  • Dw: delete the part behind the word where the current cursor is located. If the cursor is not located in the word, delete the last word
  • DB: Delete the front part of non-blank word where the current cursor is located. If the cursor is not located in non-blank word, delete the first non-blank word of the cursor.
  • DW: delete the part after non-blank word where the current cursor is located. If the cursor is not located in non-blank word, delete the last non-blank word.
  • D $: Delete the cursor to the last part
  • D0: Delete the start part to the cursor

If we use c instead of d, run the DELETE command to enter the input mode ., You can also use the following abbreviations:

  • D: equivalent to d $. Delete the cursor to the last part.
  • Dd: equivalent to 0d $, that is, deleting the entire row
  • C: equivalent to c $, that is, deleting the following part and entering the input mode
  • Cc: equivalent to 0c $, that is, deleting the entire row and entering the input mode
  • X: equivalent to dl. delete a character and move the cursor forward.
  • X: equivalent to dh. delete a character and move the cursor behind.

All the good editors provide the un-delete method. In VI mode, you can use U to reply to previous operations and trace them all the time. In the DELETE command, if we use y instead of D, we will record the part to be deleted by the original D command. It is not actually deleted at the time, and P is used (after the cursor) or the P (before the cursor) command can paste the record content.

Control Mode -- history command:

As follows:

  • K or-, the previous command, equivalent to the up cursor, you can use 3 K to repeat three times
  • J or +, followed by the command, equivalent to the downward cursor, you can use 3j to repeat three times
  • G, move to line given by repeat count, meaning unknown
  • /String: searches backward for historical commands containing strings, while ^ string is used to query historical commands starting with string. You can use/to continue searching.
  • ? String, which is used to search for historical commands containing strings? Continue searching
  • N. Continue searching in the original direction.
  • N. Continue searching in the opposite direction.

We can see that H, J, K, and l are actually the oldest optical marking keys. This type of keyboard has been seen a long time ago. I don't remember how the first keyboard looked like. I only remember that when the first monitor was under a great thunder, there was a white smoke and it was scrapped. Fortunately, it wasn't just sitting in front of the computer.

Control Mode -- search for characters:

The following is the search method. X indicates a character. The following commands can add n times of repeated operations to the front, such as 4fa, and search for 4th a letters backwards.

  • FX: searches for the next character X and moves the cursor over the X.
  • FX: searches for the previous character X and moves the cursor over the X.
  • TX, find the next character X, move the cursor to the previous character of X
  • TX, search for the previous character X, and move the cursor to the next character of the X
  • ;, Re-operate the last SEARCH Command
  • , Re-operate the last search command, but the direction of the search is the opposite

In addition, we can also search for the column, that is, the nth character, that is, N |. If only | is specified, it is the first character.

Control Mode-complete operation

:

  • /, Equivalent to the tab in Emacs mode. You can complete the following commands, functions, and file names.
  • * Complete file name. If you have multiple options, list them (listed in the command line)
  • =, Complete file name. If you have multiple options, list them.

Control Mode-other operations

  • ~, Replace the case of the current letter and move the cursor behind
  • -, Give the last word of the previous command and enter the input mode. In cgywin
  • CTRL-L, clear screen
  • #, The command is automatically added to the beginning, and the command is not executed, but recorded in history.

 Fc processing History commands

FC is a very interesting command. I have never used this command before. I use history, or use the up and down cursor arrows to view or obtain historical commands.

  • Fc-l: displays historical commands. You can add parameters later. If a number is used, it indicates the nth command. If a string is used, it indicates the command starting with the string. If there are two parameters, the command in the display range is displayed, for example, fc-l 5 8, the command in step 5 is displayed. If there is only one parameter, the command is displayed from the beginning, for example, fc-l 5 shows the History commands from 5th to the last.
  • To display historical commands, you can also directly compile historical commands, such as compiling command 45th, fc 45, the latest command starting with c, fc c, this command enters the vi compilation mode, where we can easily compile. If we provide two parameters, we can compile a name group,After exiting vi Mode, execute these commands in sequence

    .

 Others

The book also introduces some processing methods for extended historical commands. I think it's okay to look at it. Who remembers so many things, just like Chinese typing, what natural codes have you learned before, but what really remembers is pinyin. In addition, my experiment in cgywin does not have any effect. The key to this chapter is that we can easily compile bash.

Bash's edit interface becomes Readline. We can use ~ /. Modify inputrc to revise Readline and set our own key-pressing method. Such files are called Startup files. File, associate a (Group) key with a function of macro or Readline. For example, if we want to add a CTRL-T, place the cursor behind the row, and add it to. inputrc:

# Wei test

"/C-t": end-of-line

"/C-t" indicates the key name,/C indicates the Control key prefix, and end-of-line indicates the readline function, which has the function such as Operation 60. Add # To the first line to indicate comments. This also provides a shortcut, such as "/C-t": "Hello", when I use a CTRL-T, it is equivalent to typing Hello.

  • /C-: CTRL
  • /M-: Escape key (Meta (Escape) key prefix)
  • /E: escape charater, which is no different from/M-in the experiment.
  • //: Indicates/;/", indicating",/'indicates'

We can add some conditional controls, such as only for some edit mode, some teminal or some applications. The condition control uses $ if, $ else, $ endif. For example, the preceding example only appears in emacs mode.

$ If mode = emacs

"/C-t": end-of-line

$ Endif

For some shortcut keys, you can useBind-P

. In. the modification in the inputrc file must be valid for the next login, but sometimes we do not want to make a permanent revision, but only want to temporarily take effect. You can use bind '"/C-t ": end-of-line method. The setting after bind is the same as that in the startup file, but ''must be added ''. Bind is only valid for this logon. Clear the bundle and use bind-u. All bindings of this function are clear. You can use bind-P to confirm. Bind-r. The shortcut key is invalid. Bind-x is used to bind shell commands, such as bind-x '"/C-l": ls '. Delete a specific binding, just as we use bind to add a binding. Bind-p is interesting. It is displayed in. inputrc format. It can be imported to. inputrc or copied to the bind command. If we need to customize many such commands and do not want to put them in. inputrc, we can write them into a file and import them through bind-f.

We can also set some readline parameters through the set method. Many parameters can be set. For details, refer to Learning the bash Shell. Here I will list what I'm interested in:

Set bell-style none | visible | audible. The default value is audible. This setting is used to process beep. For example, if no matching tab exists, beep is triggered. If beep is always present in the office, it's annoying. You can disable none. The visible method is no different from audible in cgywin.

Summary

In fact, even if we often use linux, it is still difficult to remember so many things, and the current keyboard is quite convenient, maybe there are some restrictions in some serial port teminal. Generally, the following is enough:

  • Left and right cursor keys, Home and End move cursor to line header and End
  • Enable or disable the cursor key to view historical commands.
  • The tab key to automatically add the complete words as much as possible
  • Delete Del or backspace
  • CTRL-C, discard the entire line, do not execute

 

Related links:
My articles on Linux operations


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.