Eighth chapter, vim text editor chapter content
- Three main modes of using VI and VIM
- Move the cursor into insert mode
- Change, delete, copy text
- Undo Change
- Search Documents
- Vim Register
- Visualization and Multi-window
- Vim Help
Why use the VIM editor
1. All Unix like systems will be built into the vi text editor, and other text editors will not necessarily exist
2. Many of the individual software's editing interfaces will actively call VI
3. Vim has the ability to edit the program, can be active in font color to identify the correctness of the grammar, convenient programming
4. The program is simple, the editing speed is quite fast
Brief introduction
- Vi:visual Interface, Text editor
- Text: ASCII, Unicode
- Type of text editing:
Line Editor: SED
Full Screen Editor: Nano, VI
Nano:ctrl+o Save Enter confirm Ctrl+x exit
Vim-vi improved
Gedit a simple graphical editor
Gvim a graphical version of the VIM editor
Open File
- # vim [OPTION] ... FILE ...
+#: After opening the file, leave the cursor at the beginning of line #, + default end of line
+/pattern: Immediately after opening the file, leave the cursor at the beginning of the first line that is matched to the PATTERN
–B file binary mode open files
–d file1 file2 ... Compare multiple Files
Ctrl+w let go right arrow key; Ctrl+w left ARROW key.
-m file read-only open files
- Ex file or vim–e directly into ex mode (Extended command mode)
- If the file exists, the file is opened and the content is displayed
- If the file does not exist, create it when you save it for the first time after editing
Vim: A pattern editor
- Keystroke behavior is a "pattern" that relies on vim
- Three main modes:
Command (Normal) mode: default mode, move cursor, cut/paste text (Red Hat official called Command mode) (this mode is mainly to view the file, of course, can also modify the file)
Insert (insert) or edit mode: Modify text
Extended commands (Extended command) mode: Save, exit, etc.
- ESC key to exit the current mode
- ESC key always returns to command mode
Mode conversion
- Command mode---insert mode
I:insert, enter at the cursor position
I: Input at the beginning of the line where the current cursor is located
A:append, enter after cursor location
A: Enter at the end of the line at the current cursor
O: Opens a new line below the line where the current cursor is located
O: Opens a new line above the line where the current cursor is located
- Insert mode--------> Command mode
Esc
- Command mode--------> Extended command mode
:
- Extended Command mode--------> Command mode
Esc,enter
Close File
: Q exit (no changes to file)
: q! Force exit, discard the modifications made
: Wq Save Exit
: X Save exit
: wq! Forced to save the exit (the file only read permissions, the administrator can force the save exit, the other user does not) the directory is located in the directories to 777, other users can, but the mechanism is to delete the original file, has built a new file, content is changed content.
ZZ: Save exit
ZQ: Do not save exit
Extended mode
- Press ":" To enter ex mode
- Create a command prompt: On the left side of the screen at the bottom
- Command:
W Write (Save) disk File
Wq Write and exit
X Write and exit
Q exit
Q! Do not save the log out, even if the changes are lost
R filename reads the contents of the file into the current file (: W filename Saves the current state to the new file)
W FileName writes the current file contents to another file
!command Execute command (Execute command temporarily, knock enter and go back to vim)
R!command read-in command output
Command mode cursor jump
H: Left L: Right J: down K: Upper
#COMMAND: Jumps the number of characters specified by #
W: The first word of the next word (3+w jump 3 words)
E: The ending of the current or next word
B: The first word of the current or previous word
#COMMAND: Specifies the number of words to jump by # at one time
H: Top M: Page Middle row L: Bottom of page
^: jumps to the first non-whitespace character at the beginning of a line
0: Jump to the beginning of the line
$: Jump to end of line
#G, extension mode: #: Jump to line specified by #
G: Last line
1G, GG: First line
): The next sentence (: The previous sentence of which. Separate is a sentence
}: Next paragraph {: Paragraphs where empty lines are separated
Command mode Flip screen operation
- CTRL+F: Flip a screen to the end of the file
- CTRL+B: Flip a screen to the file header
- Ctrl+d: Turn half screen at the end of the file
- Ctrl+u: Turn half screen to file header
Command mode operation
X: Delete the character at the cursor
#x: Delete the # characters at the beginning of the cursor
XP: Swap the position of the character where the cursor is located and the character after it (ab to BA)
x: Clip Delete p: paste
~: Convert Case
J: Remove line breaks after the current line
- Replace command (R, replace)
R: Replace the character at the cursor (r, enter new character to replace the current character, replace one character)
R: Switch to replace mode (fourth mode: Replace mode, always replace the current character)
D: Delete command, can be combined with the cursor jump character, to achieve range deletion
d$: Delete to end of line
d^: Delete to non-empty header
D0: Delete to the beginning of the line
DW: Delete a word
De: Delete forward
DB: Delete later
#COMMAND
- DD: Delete the line where the cursor is located
#dd: Multiline Delete (5DD delete 5 lines)
- D: Always delete from the current cursor position to the end of the line, leaving blank lines, equivalent to d$
- Copy command (y, yank):
Y: Copy, behaves similar to D command
y$
Y0
y^
Ye
yw
Yb
#COMMAND
#yy: Copying Multiple lines
Paste underneath the line where the cursor is located
- Y: Copy Entire row
- Paste command (P, paste):
P: If the buffer is an entire row, paste the current cursor below the row, or paste it at the end of the current cursor.
P: If the buffer is an entire row, the current cursor is pasted above the row, otherwise, it is pasted to the front of the current cursor position.
- changing commands (c, change)
C: Switch to insert mode after modification
- Command mode---insert mode
C $: Delete all content after the cursor
c^
C0
Cb
Ce
cw
#COMMAND
- CC: Delete the current line and enter new content, equivalent to S
#cc:
- C: Delete the current cursor to the end of the line and switch to insert mode
Command mode: Manipulating text
Command mode
- 100iwang [ESC] Paste "Wang" 100 times
- <start Position><command><end position>
- Command:
Y copy, d Delete, GU to uppercase, GU to lowercase
For example, the 0y$ command means:
0→ to the wardrobe first.
y→ start copying from here
$→ copy to the last character of the bank
Ye copy from the current position to the last character of this word
Extended Command mode: Address delimitation
: Start_pos,end_pos
# specific # lines, such as 2 for line 2nd
#,# from the left # represents the starting line to the right # indicates the end line
#,+# the starting line from the left # representation, plus the number of rows on the right #
: 2,+3 = 2 to 5 rows
. When moving forward
$ last line
., $-1 current line to penultimate line
% full text, equivalent to 1,$
Start from the first line that is matched to the pat1 pattern, until the end of the line that is first matched to the PAT2 (note that the cursor position is one line ahead of the search line, or there will be fewer lines)
#,/pat/
/pat/,$
- How to use: followed by an edit command
D
Y
W File: Save a range of rows to a specified file
R file: Inserts all content from the specified file at the specified location
Extended Command mode: Find
/pattern: Looks at the end of the file from the current cursor location
? PATTERN: Finds the file header from the current cursor location
N: Same direction as command
N: Opposite direction of command
Extended Command mode: Find and replace
- S: Complete the Find and replace operation in extended mode
Format: s/What to look for/replace with content/modifiers
What to look for: Available modes
Replace with: cannot use mode, but can use \1, \2, ... You can also use the "&" reference to find the entire contents of the previous lookup
Modifier:
I: Ignore case
G: global substitution; By default, each row replaces only the first occurrence of the
%s/root/root/g
GC: Global substitution, asking before each replacement
- Find separators in substitutions/can be replaced with other characters, such as
[Email protected]/[email protected]/[email protected]: To prevent the replacement of content has a path, that need to escape the trouble.
s#/boot#/#i
: + Up and down keys to invoke the command you just executed
Command mode: Undo Changes
- U Undo Recent Changes
- #u撤销之前多次更改
- U undo cursor falls on this line after all the changes of this row
- Press Ctrl-r to redo the final undo change (cancel the undo just now)
- . Repeat the previous action (repeat the action just now)
- N. Repeat the previous action n times
Vim Register
- There are 26 named registers and 1 unnamed registers, often with different clipboard content, can be shared between different sessions
- Register name A, b,..., z, format: "Registers are placed between numbers and commands
Example: 3 "tyy means copying 3 rows to the T register
"TP means to paste the contents of the T register
- Unspecified, no named registers will be used
- There are 10 digital registers, denoted by 0,1,...,9, 0 stores the most recently copied content, and 1 stores the most recently deleted content. When new text changes and deletions, 1 dumps to 2, 2 dumps to 3, and so on. Digital registers cannot be shared between different sessions
Editing a binary file
- Open a file in binary mode
Vim–b Binaryfile
- Extended command mode, using the XXD command to convert to a readable hexadecimal
:%!xxd
- Editing a binary file
- In extended command mode, use the XXD command to convert back to binary
:%!xxd–r
See what is the 16 binary of a?
Echo A>f1;hexdump-c F1
Visualization mode
- Allow selection of text blocks
V Character-oriented
V Line-oriented
CTRL-V Block-oriented
shift+ > Indent Right (select and indent first)
- Visualization keys can be used in conjunction with the move key:
W)} arrows, etc.
- Highlighted text can be deleted, copied, changed, filtered, searched, replaced, etc.
Multi-file Mode
- Vim FILE1 FILE2 FILE3 ...
: Next Next
:p Rev A previous
: First One
: Last One
: Wall Save All
: Qall Quit all
: Wqall
Use more than one "window"
Vim-o|-o FILE1 FILE2 ...
-O: Horizontal split
-O: Vertical split
Switch between windows: Ctrl+w, Arrow (ctrl+w let go, then press left and RIGHT ARROW keys)
- Single File Window segmentation:
Ctrl+w,s:split, Horizontal split
Ctrl+w,v:vertical, Vertical split
Ctrl+w,q: Canceling adjacent windows
Ctrl+w,o: Cancel All windows
: Wqall exit
Customizing the working characteristics of vim
- Configuration file: Permanently valid
Global:/ETC/VIMRC
Personal: ~/.VIMRC (recommended, put the following command into the file last, permanent)
- Extended mode: Current VIM process is valid
(1) Line number
Display: Set number, abbreviated as set Nu
Cancel display: Set Nonumber, abbreviated to set Nonu
(2) Case of ignoring characters
Enable: Set IC
Do not ignore: set Noic
(3) Auto Indent
Enable: Set AI
Disabled: Set Noai
(4) Smart indent
Enabled: Smartindent shorthand set Si
Disabled: Set Nosi
(5) Highlight Search
Enabled: Set Hlsearch
Disabled: Set Nohlsearch
(6) syntax highlighting
Enabled: Syntax on
Disabled: Syntax off
(7) Display tab and line break ^i and display
Enabled: Set list
Disabled: Set Nolist
(8) file format
Enable Windows format: Set Fileformat=dos
Enable UNIX format: set Fileformat=unix
Shorthand: Set Ff=dos|unix
(9) Set text width
Enabled: Set textwidth=65 (VIM only)
Disabled: Set WRAPMARGIN=15 (right-to-left)
(10) Set the identification line of the line where the cursor is located
Enabled: Set Cursorline, abbreviated CUL
Disabled: Set no Cursorline
(11) Copy retention format
Enabled: Set paste
Disabled: Set Nopaste
Learn More
: Help Option-list
: Set Or:set All
: Help
: Help Topic
Use:q to exit Help
- Vimtutor (Command-line command)
Eighth chapter, VIM editor