Vim Learning
Vim's normal mode press CTRL+Z will put the currently open file into the background to run, if you need to restore to the foreground, you need to use the command mode of the shell to see the number of all tasks in the current environment with the jobs command, in the FG%n back to the foreground, n is the corresponding background run number.
DW is to delete a word (or DAW) and DNW delete n words.
NX is the deletion of that character, NDD is deleted n rows.
In command mode: Set NU is the display line number.
NG (n shift+g) moves the cursor to the nth row. GG moves to the first row, and G (SHIFT+G) moves to the last line.
Command |
Description |
W |
To the beginning of the next word |
E |
To the end of the next word |
B |
To the beginning of the previous word |
Ge |
To the end of the previous word |
0 or ^ |
to the Wardrobe |
$ |
To the end of the line |
f< Letter > |
Search backwards < letters > and jump to the first matching position (very practical) |
f< Letter > |
Search forward < letters > and jump to the first matching position |
t< Letter > |
Search backwards < letters > and jump to a letter before the first matching position (not used) |
t< Letter > |
Search forward < letters > and jump to a letter after the first match position (not used) |
Ps: Search for characters and jumps can only be valid within the current line, cannot jump to the next line.
· Use y replication in normal mode
o Normal mode, the entire row in which the YY copy cursor is located (3yy means to copy 3 rows)
O in normal mode, y^ is copied to the beginning of the line, or y0. Contains no characters at the cursor.
O in normal mode, y$ is copied to the end of the line. The character at which the light is contained.
O in normal mode, yw copies a word.
O in normal mode, y2w copies two words.
O in normal mode, YG is copied to the end of the text.
O in normal mode, y1g is copied to the beginning of the text.
· Use p paste in normal mode
o Normal mode, p (lowercase) is pasted after the cursor (bottom)
o Normal mode, P (uppercase) is pasted to the front of the cursor (top)
Replace and undo (undo) command
Both the Replace and undo commands are for normal mode operations
Command |
Description |
r+< to replace letters > |
Replaces the letter that contains the cursor with the specified letter |
R |
Continuous replacement until ESC is pressed |
Cc |
Replace the entire row, that is, delete the row of the cursor and enter insert mode |
cw |
Replace a word, that is, to delete a word and enter insert mode |
C (uppercase) |
Replace cursor to end of line |
~ |
Reverse the case of the letter in which the cursor is |
U{n} |
Undo One or N times |
U (upper case) |
Undo all changes to the current row |
Ctrl+r |
Redo, that is, undo undo operation. |
Enter >> or << directly in normal mode, which is equivalent to adding a tab (also called a jump or tab) to the place before the cursor, used to adjust the formatting in the code file.
We can adjust the tab width, as long as in the command mode input: Set shiftwidth=10, a tab distance to set the width of 10 characters, but only to change the width of >>, and can not change the tab width in edit mode, A tab in edit mode usually defaults to 8 characters wide.
About finding
In normal mode, enter/then type the string you want to find after you press ENTER. Same AS/function, except. is to look up and/or down. After entering the lookup, enter N and N to continue looking for N to continue looking, n reverse lookup.
In normal mode, enter \* to find the word at the cursor location
Normal mode input \ #同上, but \* is forward (up) to find, #则是向后 (next) to find
In normal mode, enter g\* with \*, but the word can be partially met
In normal mode enter g\ #同 \#, but the word can be partially met
Recover documents and encrypted documents
If the document is not saved because of a power outage, you can use the recovery method, Vim-r Enter the document, enter: Ewcover 1.txt to restore
$ vim-x File1
If the file does not have a password before, you will need to enter your password and then enter a confirmation password so that VIM will ask you to enter the password the next time you open it.
If you need to unlock the file password, enter the correct password to enter the file, in the command mode input: Set key= so there is no password. There is also a simpler direct: X has been pressed to enter the good, that is, to give a blank password.
Visual mode
In normal mode, enter V (lowercase), enter the character selection mode, you can move the cursor, the place where the cursor passes will be selected. The selection will be canceled once the V is pressed again.
In normal mode, enter shift+v (lowercase), enter the row selection mode, press v after the whole row will be selected, you can move the cursor up and down to select more lines, the same, and then press the SHIFT+V can be deselected.
In normal mode, enter Ctrl + V (lowercase), which is the zone selection mode, you can select the rectangular area, and then press CTRL + V to deselect.
Enter D to delete selection contents in normal mode
In normal mode, enter Y to copy the selection of the region
Shell Programming
Variable related
The env is to view environment variables, and set is to view all current variables, including user-defined variables. The most used and most viewed of the custom variable is actually the SP1 variable, and we can look at it with a set, and the env is not visible because it is not an environment variable. The SP1 variable saves the user command prompt.
The locale command is to view the current system language, mainly to see Lang and lc_all variables.
LANG: A variable that defines the system subject line
Lc_all: Defining a whole language variable
For these two variables can be roughly understood as Lang is the current language family (temporary), and Lc_all is the overall language, the current language family will revert to the Lc_all value after the restart.
$, representing the result of the last command execution, 0 is true (normal execution), Non-zero is False (execution failed).
$n, n is a positive integer, and $ is representative of the command itself, $1-$9 represents parameter 1 to parameter 9, respectively, for example:./HELLO.SHABC 22,./hello.sh is the command itself (executing a hello.sh script) for $, followed by ABC is the first parameter $ 1,11 and 12 are the second and third parameters, that is, $ and $. In the actual shell program, you can use the $n value directly. If the argument is greater than 10, a brace is required, such as ${10}.
$*, representing all parameters, looks at all parameters as a whole. As in the above example, the value of $* is ABC 11 22.
$@, which also represents all parameters, but unlike $*, is different when used, that is, one use.
$#, which represents the number of parameters.
Example:
#/bin/bash #this file is plus.sh Sum= (($ + $)) Echo $Sum Echo $* Echo $@ Echo $# |
Execute this script:./plus.sh11 22
Results:
33
11 22
11 22
2
Example 2:
#!/bin/bash #this file is for.sh For i in "$*" Todo Echo $i Done For j in "$@" Todo Echo $j Done |
Execute script:./FOR.SH1 2 3 4
Results:
1 2 3 4
1
2
3
4
Read command (very practical command):
Parameters:-P "hint info"------output some hints before reading a value
-T seconds-------Specify the maximum time limit to wait for user input
-N numeric-------Specifies the maximum number of characters to read
-S--------Hide user input, similar to the effect of entering a password when a user logs on
Test statement
1, according to the file type to judge
-B To determine if the file exists, and whether it is a block device file
-C to determine if the file exists, and whether it is a character device file
-D to determine if the file exists and is a directory file (is the directory file is true)
-E to determine if a file exists (exists as true)
-F to determine if the file exists and is a normal file
-L to determine if a file exists and is a linked file
-P to determine if the file exists and is a pipe file
-S to determine if the file exists and is Non-null (Non-null true)
-S to determine if the file exists and is a socket file
2, according to the file authority to judge
-R to determine if a file exists and has Read permissions
-W to determine if a file exists and has write permissions
-X to determine if the file exists and has execute permissions
-U to determine if a file exists and has SUID permissions
-G to determine if the file exists and has Sgid permissions
-K to determine if a file exists and has sbit permissions
Comparison between 3 and two files
File 1-nt file 2 to determine if file 1 was modified more than the new file 2
File 1-ot file 2 to determine if file 1 modifies the time than file 2 of the old
File 1-ef file 2 Determines whether file 1 is consistent with the inode number of document 2, and can be interpreted as whether two files are the same file. Usually used to judge hard links.
Comparison between 4 and two integers
Integer 1-EQ integer 2 to determine whether two numbers are equal
Integer 1-ne integer 2 to determine if two numbers are not equal
Integer 1-GT integer 2 to determine whether the integer 1 is greater than the integer 2
Integer 1-LT integer 2 to determine whether the integer 1 is less than the integer 2
Integer 1-ge integer 2 to determine whether the integer 1 is greater than or equal to the integer 2
Integer 1-le integer 2 to determine whether the integer 1 is less than or equal to the integer 2
EQ is the abbreviation of equal NE is the abbreviation of not equal GT is great than abbreviation LT is less than abbreviation GE is great thanor equal to abbreviation le is less than to abbreviation
5, the judgement of the string
-Z string to determine if the string is empty
-N string to determine if the string is Non-null
String 1 = = String 2 To determine whether two strings are equal
String 1 =! String 2 to determine if two strings are unequal
6. Multiple condition judgment
Judge 1-a judgment 2 logic and two all set up the result is true
Judge 1-o Judge 2 logic or, there is a true, that the result is true
。 Judgment logic is not, will judge the result to take the opposite
Two forms of judgment
L TEST-E/root/install.log
l [-e/root/install.log] recommends the use of this
If statement
If [Conditional judgment type];then
Statement of execution of conditions
Else
A statement that is not executed immediately
Fi
Or
If [conditional-judged]
Then
Statements executed when conditions are established
Else
Statements executed when the condition is not tenable
Fi
Case statement
Basic format:
Case $ variable name in
"Value 1")
If the value of the variable equals the value 1, execute program 1
;;
"Value 2")
If the value of the variable equals the value 2, execute program 2
;;
*)
If the value of the variable is not the value above, then execute this program code
;;
Esac
For statement
Grammar one,
For variable in value 1 value 2 value 3 ....
Todo
Program
Done
Grammar two,
for ((initial value; cyclic control condition; variable change)
Todo
Program
Done
Syntax two for loops like C language
While loops and until loops
While [conditional-judged]
Todo
Program
Done
As soon as the conditional judgment form is established, the cycle begins.
Usually in the program code, there should be a control statement that exits the loop.
The Until loop and the while loop, in contrast, start the loop as soon as the conditional judgment is not established. Once the loop condition is set, the loop terminates.
Linux $ () and ${}, $ (()) Detailed I. About $ ()
In fact, the same function of $ () and ' (Reverse tick) ' is used to make command substitution, which is used to reorganize the command line. It must contain a command statement.
In actual use, a $ () appears in command line, which executes the command statement in $ () first. Like what:
Echo Today is $ (date)! We know that the date command prints the current time, then executes the date command to print the entire sentence result to the standard output via the echo command, and the result is: This is Sun Oct 1820:24:31 CST 2015!
Of course the whole sentence can be replaced by: Echotoday is ' date '! The result is exactly the same.
$ () vs ' ' comparison
Advantages: $ () can be very good with the "(single quotes) to distinguish between the actual use of intuitive readability good.
Disadvantage: Portability is not "good," is basically available in all other shells.
ii. about ${}
First look at the example:
First define a variable a=he, now want to output Hello, we can echo $Allo, this is you will find the output is empty, because the shell will recognize your variable as $allo rather than $a+llo, and $allo is an unassigned variable, of course, output empty. Now let's change the echo ${a}llo and get hello to the correct result. Obviously ${} is the delimiter of the variable, indicating the length limit of the variable name to enhance readability. In fact, we usually write the $a just ${a}, in the shell script, should try to avoid using shorthand, which can greatly reduce the error.
${} If it's just a delimiter, that's too much to underestimate, here are some of its special and commonly used usage examples:
Suppose we define a variable that is:
File=/dir1/dir2/dir3/my.file.txt
We can substitute ${} for different values:
${file#*/} : take off the first/and its left string: Dir1/dir2/dir3/my.file.txt
${file##*/} : Take off the last one/and the string to the left: My.file.txt
${file#*.} : takes off the first . and its left string: file.txt
${file##*.} : take off the last . and its left string: TXT
${file%/*} : take off the last piece/ and its right-hand string:/dir1/dir2/dir3
${file%%/*} : take off the first/and its right string: (null)
${ file%.*} : take away the last . and its right string:/dir1/dir2/dir3/my.file
${file%%.*} : take off the first . and its right string: