The Vim text editor is one of the most commonly used tools under the Linux/unix system, which makes it easy to create, modify, edit, or program files that are similar to Notepad or notepad++ under the Windows system. Therefore, mastering the use of the tool is necessary for the use of Linux systems. This article records the basic use of the Vim editor to strengthen memory or prepare for review later. ( The combination of cat and <<eof also enables some of the editor's functions to generate and edit documents: Cat >log.txt <<eof)
The main references are:
1. "Brother Bird's Linux private cuisine"
2. "Linux is the way to learn"
3. Quick Vim https://learnxinyminutes.com/docs/zh-cn/vim-cn/
4. Novice Tutorial Http://www.runoob.com/linux/linux-vim.html
5. Other Internet information, Google,baidu and other search engines
One, the Vim editor three modes basically vi/vim altogether divides into three kinds of modes, respectively is
Command mode,
input modes (Insert mode)And
Baseline Command mode (last line mode)。
(1) Command mode:
The user has just started Vi/vim and entered command mode.
In this state, the keystrokes are recognized by VIM as commands rather than as input characters. For example, when we press the
I, and does not enter a character,
Iwas taken as a command.
Here are a few common commands:
- I switch to input mode to enter characters.
- x deletes the character at which the current cursor is located.
- : Switch to Baseline command mode to enter commands at the bottom of the line.
- U undo
- Ctrl+r Reply Revocation
To edit text: Start vim, enter command mode, press
I, switch to input mode.
Command mode has only some of the most basic commands, so you still have to rely on the bottom line command mode to enter more commands.
(2) input mode:
Press I in command mode to enter the input mode.
In input mode, you can use the following keys:
- Character keys and shift combinations, enter characters
- Enter, enter, line break
- Back SPACE, backspace key, delete the previous character of the cursor
- DEL, delete key, delete cursor after one character
- Arrow keys, moving the cursor in text
- Home/end, move cursor to beginning/end of line
- Page Up/page down, up/down page
- Insert, toggle cursor to input/Replace mode, the cursor will become a vertical bar/underline
- ESC, exit input mode, switch to command mode
(3) Bottom-line command mode:
Press: (colon) in command mode to enter the baseline command mode.
The bottom line command mode can enter commands for single or multiple characters, and there are many commands available.
In the baseline command mode, the Basic command has (the colon has been omitted):
- Q Exit Program
- q! forced exit
- W Save File
- WQ FileName: Save as specified file name and exit
Press the ESC key to exit the bottom command mode at any time.
To put it simply, we can think of these three patterns as the icons below to indicate:
Ii. Vim's basic command operation VIM Navigation basics
Vim <filename> # opens <filename> in vim</filename></filename>
:q # quit Vim
:w # save the current file
:wq # save the file and exit Vim
: q! Exit Vim and do not save the file
#! * forces * to execute :q, so exit Vim without saving
:x # save file and exit Vim, short for :wq
U # to cancel
CTRL + R # redo
H # moves one character to the left
J # moves down one row
K # moves up one row
L # moves one character to the right
# moves within rows
0 # moves to the beginning of the row
$# moves to the end of the line
^ # moves to the first non-white space character in the line
# look in the text
All the words after the word # cursor are highlighted
? All the words before the word # cursor are highlighted
Find n # and move the cursor to the next occurrence of the word
N # moves the cursor over the last occurrence of the word
:%s/foo/bar/g # change all 'foo' on each line of the file to 'bar'
:s/foo/bar/g # change all 'foo' on the current line to 'bar'
# skip to the character
F < character > # jumps forward to < character >
T < character > # jumps forward to the left of < character >
#, for instance,
F < # jumps forward to <
T < # jumps forward to the left of <
Move by word
The default word consists of letters, Numbers, and underscores
W # moves to the next word
B # moves to the beginning of the previous word
E # moves to the next suffix
Other commands to move
Gg # moves to the top of the file
G # moves to the end of the file
:NUM # move to NUM row (NUM is any number)
H # moves to the top of the screen
Move M # to the middle of the screen
L # moves to the end of the screen
Mode:
Vim is based on the concept of patterns .
Command mode-Vim is in this mode for navigating and manipulating command insertion mode-used to modify the visual mode in your files-for highlighting text and manipulating them Ex mode-to jump to the bottom of the ': ' Prompt to enter commands on the line
I # switches Vim to insert mode before the cursor position
After the cursor position, switch Vim to insert mode
V # switches Vim to visual mode
: # switch Vim to ex mode
<esc> # return to command mode no matter what mode you are currently in</esc>
Copy and paste text
Y # copies the selected content
Yy # copy the current line
D # deletes the selected content
Dd # deletes the current row
P # paste the copied text after the current cursor position
P # paste the copied text in front of the current cursor position
X # deletes the character at the current cursor position
Vim's ' Grammar '
Vim can be thought of as a set of commands in the ' verb-modifier-noun ' format:
Verb-your action modifier-how do you perform your action noun-the object your action is made for
A few important examples of ' verbs ', ' modifiers ', and ' nouns ':
# 'verb'
D # delete
C # modified
Y # copy
V # visual selection
# 'modifier'
I # internal
Around a #
NUM # number (NUM is any number)
F # finds the text and sits on it
T # finds the text and stops in front of it
Find the string from the cursor
? Find the string in front of the cursor
# 'noun'
W # word
S # sentence
P # paragraph
B # block
Example 'statement' or command
D2w # delete 2 words
Cis # modifies the content within the paragraph
Yip # copy the content of the paragraph (copy the paragraph you are in)
Ct < # until the brackets open
Modify your current position until the next parenthesis opens
D $# is deleted until the end of the line
Some shortcut keys and tips
<!--</span--> TODO: Add more! -->
> # indents the selection one level
< # unindents the selection one level
:earlier 15m # restores the document to the state it was 15 minutes ago
:later 15m # reverse the above command
DDP # adjacent rows swap places, dd before p
. # repeat the previous action
Macro
A macro is basically a recordable action. When you start recording a macro, it records every action and command you use until you stop recording. When a macro is called, it applies the exact same sequence of actions and commands to the selected text again.
Qa # starts recording a macro called 'a'
Q # stop recording
@a # replay macro
Configure ~/.VIMRC
The. VIMRC can be used to configure Vim at startup.
Here is an example ~/.VIMRC file:
"Sample ~ /. Vimrc
"2015.10
"Vim iMproved
The set nocompatible
"Detects file types by file names so that you can do things like smart auto indentation.
Filetype indent the plugin on
"Turn on syntax highlighting
Syntax on
"Better command line completion
The set wildmenu
"Except for case-independent lookups when using uppercase letters
The set ignorecase
The set smartcase
"When a new line is opened, if the file specific indentation rule is not opened,
"Keeps the indentation the same as your current line
The set autoindent
"Show the line number on the left
The set number
"Indentation options, modified to suit personal preference
"The number of visible Spaces per TAB
The set tabstop = 4
"The number of Spaces in TAB when editing
The set softtabstop = 4
"The number of Spaces to indent when using indentation operations (>> and <<)
Set shiftwidth = 4
"Converts TAB to space
The set expandtab
"Enables intelligent TAB and space switching for indentation and alignment
The set smarttab
2018-04-27 22:36:47 in Nanjing Xian Linya Dongcheng
Basic use of Vim text editor