Linux: Use of vim editor

Table of Contents

1. What is vim editor?

Basic concepts of vim

2. Basic operations of vim

3. vim normal mode command set

4. Vim end line mode command set

5. Vim configuration


1. What is vim editor?

Vim is a widely used text editor on Linux systems. It has powerful functions and a high degree of customization.

vi/vim
To put it simply, they are both multi-mode editors. The difference is that
vim
yes
vi
An upgraded version, it is not only compatible with
vi
All instructions, and there are some new features in it. For example, syntax highlighting and visual operations can be run not only in the terminal, but also in x window
,
mac os
, windows.

Basic concepts of vim

untie
vim
three modes of
(
In fact, there are many modes. Currently, I have mastered this
3
Just plant
),
They are command mode (
command mode
), Insert mode
) and bottom row mode (
last line mode)

normal
/
ordinary
/
command mode
(Normal mode)

Control the movement of the screen cursor, deletion of characters, words or lines, move and copy a section and enter
Insert mode
down, or to
last line mode

Insert mode
(Insert mode)

only at
Insert mode
to enter text, press ”
ESC
” key to return to the command line mode. This mode is the most frequently used editing mode we will use later.

last line mode
(last line mode)

Save or exit the file, you can also perform operations such as file replacement, string search, and line number listing. In command mode,
shift + :
to enter this mode. To see all your modes: open vim
, bottom row mode input directly

:help vim-modes

2. Basic operations of vim

Enter
vim,
Enter the symbol at the system prompt
vim
and file name, enter
vim
Full screen editing screen
:

$ vim test.c

But there is one thing to pay special attention to, that is, when you enter
vim
After that, it was in
[
normal mode
]
, you need to switch to
[
Insert mode
]
to be able to enter text.

[
normal mode
]
switch to
[
Insert mode
]

input
a

input
i

input
o

[
Insert mode
]
switch to
[
normal mode
]

Currently in
[
Insert mode
]
, you can only keep entering text. If you find that you have entered the wrong word
,
If you want to use the cursor keys to move back and delete the word, you can first click “ESC”
” key to go to
[
normal mode
]
Delete the text again. Of course, you can also delete it directly.

[
normal mode
]
switch to
[
last line mode
]


shift + ;

,
In fact, just enter ”
:

quit
vim
and save files
,
exist
[
normal mode
]
Next, click ”
:
“Press the colon key to enter”
Last line mode

,
For example
:

:w
(save current file)

: wq (
Enter ”
wq

,
Save and exit
vim)

: q! (
input
q!,
Force exit without saving
vim)

3. vim normal mode command set

Insert mode

according to”
i
“Switch to insert mode”
insert mode
“,according to
“i”
After entering insert mode, file input starts from the current position of the cursor;

according to”
a
“After entering insert mode, text input starts from the position next to the current cursor position;

according to”
o
“After entering insert mode, a new line is inserted, and text is entered starting from the beginning of the line.

Switch from insert mode to command mode

according to”
ESC
“key

Move cursor

vim
You can directly use the cursor on the keyboard to move up, down, left, and right, but the formal
vim
It uses lowercase English letters ”
h
”, “
j
”, “
k
”, “l
”, respectively control the cursor to move one space left, down, up, and right.

according to”
G
“: Move to the end of the article

according to”
$
》: Move to the line where the cursor is located

end of line

according to”
^
》: Move to the line where the cursor is located

Beginning of line

according to”
w
“: The cursor jumps to the beginning of the next word

according to”
e
“: The cursor jumps to the end of the next word

according to”
b
“: The cursor returns to the beginning of the previous word

according to”
#l
“: The cursor moves to the line
#
location, such as:
5l,56l

according to[
gg
]: Enter to the beginning of text

according to[
shift
+
g
]: Enter the end of text

according to”
ctrl

+

b
》:Screen to

back

Move one page

according to”
ctrl

+

f
》:Screen to

forward

Move one page

according to”
ctrl

+

u
》:Screen to

back

Move half page

according to”
ctrl

+

d
》:Screen to

forward

Move half page

Delete text


x
“: Each time you click, delete a character at the cursor position.


#x
“:For example,”
6x
“Indicates deleting the position where the cursor is.

Behind (including yourself)
“6
characters


X
“:Capital letter
X
, each time you press it, delete the

Front

a character


#X
“:For example,”
20X
“Indicates deleting the position where the cursor is.

Front
“20
characters


dd
“: Delete the line where the cursor is located


#dd
“: Delete starting from the line where the cursor is located
#
OK

copy


yw
》: Copy the characters from the cursor position to the end of the word into the buffer.


#yw
“:copy
#
words to buffer


yy
》: Copy the line where the cursor is to the buffer.


#yy
“:For example,”
6yy
” means copying from the line where the cursor is located

Count down
“6
line text.


p
“: Paste the characters in the buffer to the cursor position. NOTE: All with
“y”
Relevant copy commands must be associated with
“p”
Only with cooperation can the copy and paste function be completed.

replace


r
》: Replace the character where the cursor is.


R
“: Replace the character where the cursor is until you press ”
ESC
” key.

Undo operation


u
“: If you execute a command by mistake, you can press ”
u
”, return to the previous operation. Press multiple times
“u”
Multiple replies can be performed.


ctrl+r

:
Reversal of Undo

Change


cw
“: Change the word where the cursor is to the end of the word


c#w
“:For example,”
c3w
” means change
3
characters

jump to specified line


ctrl

+

g
“List the line number of the line where the cursor is located.


#G
“:For example,”
15G
” means moving the cursor to the article’s
15
Beginning of line.

4. vimlast line mode command set

Before using the last line mode, please remember to press ”
ESC
” key to confirm that you are in the normal mode, and then press the “:” colon to enter the last line mode.

List line numbers

"set nu": After entering "set nu", the line number will be listed in front of each line in the file

Jump to a certain line in the file

「#」: The 「#」 sign represents a number. Enter a number after the colon and press the Enter key to jump to that line. For example, enter the number 15.
Press Enter again and it will jump to line 15 of the article

Find characters

「/Keyword」: Press the 「/」key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can keep pressing
"n" will search further until the keyword you want is found.
"?Keyword": Press the "?" key first, and then enter the characters you want to find. If the keyword you are looking for for the first time is not what you want, you can continue
Press "n" to search forward to the keyword you want

Thinking: Question: / and ? Find the difference between there and?

In Vim, / and ? are two different commands for finding text.

/ are commands used to find text forward. When you enter / followed by the text you want to find, and then press the Enter key, Vim will search for matching text from the cursor position toward the end of the file.

? is the command used to find text backwards. When you enter ? followed by the text you want to find, and then press the Enter key, Vim will search for matching text from the cursor position toward the beginning of the file.

Therefore, the difference between / and ? is that they search text in different directions, one searches forward and the other searches backward.

save document

「w」: Enter the letter "w" in the colon to save the file

leave
vim

 "q": Press "q" to exit. If you cannot leave vim, you can force "!" to leave vim after "q".
"wq": It is generally recommended to use it with "w" when leaving, so that the file can be saved when exiting. 

5. Vim configuration

Location of configuration files

The Vim editor can be customized by editing the ~/.vimrc file

in directory
/etc/
Below, there is a file called
vimrc
file, which is public in the system
vim
Configuration file, valid for all users.

In each user’s home directory, they can create their own private configuration file and name it:
“.vimrc”
. For example,
/root
directory, usually there is already a .vimrc
document
,
If it does not exist, create it.

Switch user to execute by yourself
su
, enter your main working directory
,
implement
cd ~

Open in your own directory
.vimrc
file, execute
vim.vimrc

We can also configure .vimrc for each user

This can customize everyone’s exclusive vim

Common configuration options
,
For testing

Set syntax highlighting
: syntax on

Show line number
:setnu

Set the number of spaces for indentation to
4: set shiftwidth=4

Line highlighting: set cursorline

Now we use vim to open a file, and we can see that the corresponding changes have occurred

There are more personalized configurations where you can find some information to achieve the functions you want.

Use plug-ins

The native configuration may not be fully functional. In order to make Vim better-looking and more powerful, you can use the plug-in manager to install and manage various plug-ins.

You can directly search VimForCpp in Gitee and enter according to the tutorial.

Or click the link below:
VimForCpp: Quickly turn vim into a c++ IDE

To make the configuration take effect immediately

implement:

source ~/.bashrc

Then open .vimrc and you can see that the configuration file has changed

When you open a file, you can see that personalized configuration has occurred, as shown in the figure, which is an automatic completion function.

There are more functions that can be implemented according to the documentation.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. CS entry skill treeLinux entryUse vi to simply edit text. 38136 people are learning the system.