(i) Vim last-line mode
- Vim last-line mode: Built-in command-line interface
- Address delimitation
- Start_pos[,end_pos]
- #: A specific # line, for example, 5 or line 5th
- .: Indicates when the forward
- $: Represents the last line, for example, $-1 means from the current line to the 2nd line
- #,#: Specifies the row range, the left is the starting row, and the right side is the end row, for example 10,100 indicates a total of 91 rows from line 10th to line 100th
- #,+#: Specifies the row range, the left is the starting row, the right side is the offset, for example 3,+7 means starting from line 3rd and the following 7 lines total 8 rows
- %: Full text, equals 1,$
- /pattern/: From the cursor position, the first line that is matched to the PATTERN, such as/pattern/,$, from the matching line to the last row
- Can be used together with editing commands for editing operations
- d/Delete, y/copy, c/Modify
- There is no space in the middle, the edit command follows the address delimitation directly
- W filename/content Save As
- R filename/to merge another file in
- Find
- /pattern: Starting at the cursor, looking back at all the strings that were matched to the PATTERN, the cursor stops at the first matching string
- ? Pattern: Starting at the cursor, looking forward to all the strings that have been pattern matched, the cursor stops at the first matching string
- N: Next find result, same as command direction
- N: Previous find result, opposite command direction
- Find and replace
- S/used to find/replace content/modifier
- What to look for: regular expressions
- What to replace: You cannot use regular expressions, but you can reference
- If "What to look for" uses a grouping symbol in the pattern, you can use the latter reference \1,\2 in "What to replace" ...
- or use the & symbol to directly refer to all the text that matches the content used to find
- If the content itself has a/symbol, you need to add \ to escape
- You can also change/change to other infrequently used characters so that the/symbol of the content itself does not need to be escaped, such as using the @ symbol, or the # symbol, etc.
- Modifier
- I: Ignore case (ignores case in find pattern)
- G: Global substitution means that a row is replaced if it matches multiple times, and the first occurrence in each row is replaced by default
- Example
- 1,20s/this/this/g
- %[email protected]\<t\ ([[: Alpha:]]\+\) \>@t\[email protected]: Converts words beginning with all lowercase t to words beginning with uppercase T in full text
- %[email Protected]\<t[[:alpha:]]\+\>@&[email protected]: Adds ER to the word that starts all lowercase t in the full text
- %[email protected]^#@@: Remove the # number in front of each line
Practice:
1. Copy the/etc/grub2.cfg file to the/tmp directory and use the Find replacement command to delete the whitespace characters in front of all lines in the/tmp/grub2.cfg file
Answer:
Cp/etc/grub2.cfg/tmp
Vim/tmp/grub2.cfg
:
%[email Protected]^[[:space:]]\[email protected]@
2, copy the/etc/rc.d/init.d/functions file to the/tmp directory, and use the Find Replace command to add # to the beginning of each line in the/tmp/functions file that starts with a white space character
Answer:%[email protected]^[[:space:]]\+[^[:space:]]@#&@
3, add # to the beginning of the first three lines of the/tmp/grub2.cfg file
Answer: 1,3s/*/#&/
4, replace all enabled=0 in the/etc/yum.repos.d/centos-base.repo file with enabled=1, all gpgcheck=0 replaced by gpgcheck=1
Answer:%[email protected]\ (enabled\|gpgcheck\) [email protected]\[email protected]
- VIM Multi-file mode
- vim FILE1 FILE2 FILE3 ...
- Switch between files: first/first file, last/last file, next/next file, previous/previous file
- Save and exit all files: Wqall
- save so file: Wall
- exit All files: Qall
- Multi-window
- multi-file
- -o: Horizontal separation, ctrl+w, press the up and down arrows after release, you can switch files; vim-o FILE1 FILE2 FILE3
- -o: Vertical separation; ctrl+w, release and press left and right arrows to toggle file
- Single file
- ctrl+w, release and press S, split horizontal single file
- Ctrl + W, release and press V, vertical separate single file
- Customizing the working characteristics of vim
- Command settings in the last-line mode, only valid for the current VIM process
If you want to permanently work, you need to modify the configuration file, just write the last command in the config file.
- Global:/ETC/VIMRC
- User personal: ~/.VIMRC, if the file does not exist, you need to create it yourself
- Line number
- Display: Set number or set Nu
- Cancel: Set Nonumber or set Nonu
- Parentheses match highlighting (seemingly vim has been highlighted now, the command is invalid >_<)
- Display: Set Showmatch or set SM
- Cancel: Set Noshowmatch or set NOSM
- Auto Indent
- Enable: Set AI
- Disabled: Set Noai
- Highlight search Results
- Enabled: Set Hlsearch
- Disabled: Set Nohlsearch
- Syntax highlighting
- Enabled: Syntax on
- Disabled: Syntax off
- Ignore character case
- Enable: Set IC
- Disabled: Set Noic
- In the last-line mode, you can use the help command to get assistance, or help subject to get assistance with a topic, such as helping set
Job: If you set the TAB key to indent 4 characters, instead of 8
(ii) The arithmetic operations of Bash script programming
- Arithmetic operators: +,-,*,/,**,%
- Arithmetic operation format
- Let var= arithmetic operation expression
- $[arithmetic operation expression]
- $ ((arithmetic operation expression))
- Expr command
- There should be spaces between each operand and operator
- To get results, you need to use a command reference
- Note: The multiplication symbol requires the use of escape characters in some scenarios
Exercise: Write a script that adds three users and asks for the sum of the UID of the three users
Introduction to the Vim editor and bash arithmetic operations