Linux editor vim


vim editor

Write code in Linux, use vim editor, vim is an editor, text writing, use vim to write code, it is different from vs under winidows, vs can write code, debug code, compile code, but vim it Just simply write code. vim is a multi-mode editor. It has various modes and three core types:

command mode, insert mode, bottom line mode

How to do it? Practical demonstration.
Open the test.c file with vim. It is successfully opened. At this
insert image description here
time, pressing some buttons at will is useless, because when the file is just opened with the vim editor, it is in command mode, and all user input will be treated as commands and will not be entered as text.
And when you want to write something to it, you need to change the command mode to insert mode, just enter i and
insert image description here
this will appear, so you can write something, write a piece of c language code in it,
insert image description here
save it and exit it after you finish writing it. Compile it, then how to save and exit. At this time, it is still insert mode
to switch from insert to command, press Esc, but to save and exit can not be completed in command mode, you need to enter the bottom line mode, and you can enter the first row by typing a colon on the keyboard: Line mode, you can find that it appears at the bottom and
insert image description here
enter the bottom line mode at this time, but to save and exit, just wq, w: save, q: exit.
After coming out, check whether it is saved.
insert image description here
When switching modes, you cannot insert from the bottom line or the bottom line to insert.
insert image description here
By default, vim is in command mode, so let’s take a look at the command mode.

1. Command mode

Then it has those commands, first look at the cursor positioning, in order to support these operations, I first input in the bottom line mode set nu
to call out the line number of this text.
Cursor positioning:

gg locates to the first line, it directly locates from your current line to the first line
G positions to the last line
n+G positions the cursor to any line
shift + 4 = $: the cursor is positioned to the far right of the current line
^: the cursor is positioned To the beginning of the current line
w: the cursor moves to the left according to the word, the word moves by word, not the word by character
b: moves to the right by word, the word moves by word, not the word by character
h, j, k, l: Move left, down, up, and right. Why are there these? Don’t you have up, down, left, and right keys now? It’s because vim appeared earlier than the keyboard with up, down, left, and right keys.

insert image description here
At this time, the amount of code is too small. I want to copy the code in the fifth line in batches. What should I do?
Cursor to the line to be copied, and then press yy to copy the specified content, and then press p to paste it out, and the assigned content is realized. This is just
insert image description here
one copy, if I want to make more than one, yy+np(n means I want to paste it out The number of lines)
insert image description here
This is just to achieve a copy, if you want to achieve a lot of content nyy+p, n is how many pieces of content you want to copy

yy: copy the line where the cursor is located, nyy copy the line where the cursor is located and count n lines down
np: paste (n repeated lines) to the next line where the cursor is located

And if you regret copying, you can undo it by pressing u

u: undo

How can there be copying without cutting, cutting is to press dd
to cut it to the last line, cut: dd, paste is always

And if you don't paste it out, then delete it.

ndd: cut, delete

insert image description here

~: Convert the case of the content where the cursor is located (small -> large, large -> small), it can only be changed if it is an English character
insert image description here

Keep pressing shift+, you can realize one-line conversion

r: replace the cursor character with the character you want, nr: replace the cursor character and the next n characters with the character you want, batch replacement
insert image description here

It is batch modification. But if I want to replace it with the desired content as a whole, it is no longer repeated characters, and I don’t need to delete these content, just change it directly, and this phenomenon shift+r = G
will appear when using its bottom layer.
insert image description here
At this point you can rewrite
insert image description here
shift+r:替换模式,对内容进行整体替换,它是第四种模式,如果要退出那么按Esc即可

x: delete the cursor character, nf: delete n characters starting from the cursor character, delete printf
insert image description here
, if you regret after deleting, press u to undo, if you regret this undo, then pressctrl+r:撤销之前的撤销

2. Some operations of vim

Delete the test.c just now, and then directly open a file that has not been created with vim. I wrote something, but I didn’t save it, so I just exited. At this point, there is nothing. Then
insert image description here
I
insert image description here
open a file that does not exist with vim. file, and saving it is equivalent to creating a new file
insert image description here
. When vim opens a file, save and exit it will create this file. OK This file is opened and multiple files under vim
are created. But if I still want to form a file, how to do it, enter the bottom line mode, enter , this file may exist or not exist. In this way, a file is formed , and vim can realize split-screen operation. It can be found that the cursor is in code1.c at this time, so how do I want to switch to the code.c file? Press in command mode to switch files. Operate on which window the cursor is on.

vs + 文件名
insert image description here

insert image description here
ctrl + ww

3. Bottom row mode

When you open a file with vim and write some content, but you did not save, but enter q, at this time he will not let you quit.
insert image description here
If you want to force quit, bring an exclamation point! That's it
insert image description here
! Indicates mandatory execution, and you can compile this code without exiting this file

! gcc code.c
! ls
insert image description here
can even execute this a.out file
insert image description here
insert image description here
Execute external commands without exiting the bottom line, and in the bottom line mode, you can call out the line number of the file
set nuand cancel the line numberset nonu

4.vim simple configuration

vim can perform various editing operations, but every time vim opens a file, enter i to enter the insert mode to edit, but every time it is edited, he will not have any prompts, and there is no indentation after each new line, which is Also uncomfortable, this is because there is no configuration, so how to configure?
vim should have a lot of configuration items, these configuration items are in the hidden file of vimrc in the home directory, when these configurations are used in vim in the future home directory, vim will use these configuration options and its configuration is in the corresponding home directory of the user
, It will have a hidden file .vimrc
insert image description here
but there is no need to create it ourselves at this time. touch .vimrc
insert image description here
Configure set nu in .vimrc
insert image description here
When I use vim to open a file, it will show me the line number by default, indicating that the configuration has taken effect.
insert image description here
When configuring options, if you don’t know what to configure, you can go directly to the search.
Configure vim in the .vimrc hidden file. Make a configuration file and write configuration options in it. It is configured in your own home directory and will not affect other users.
I have a configuration here
. Just copy this string of links to the command line to complete the configuration

curl -sLf https://gitee.com/HGtz2222/VimForCpp/raw/master/install.sh
-o ./install.sh && bash ./install.sh

insert image description here
After configuration, you need to enter or restart xshell to take effect.
After configuration, it will have such a file.
insert image description here
If you don’t want to see it, you can make it a hidden file.
insert image description here

Of course, this is configured under ordinary users.
At this time, if you open the code.c file, it will also have a syntax reminder
insert image description here
, which is much better than nothing. The indentation of this configuration is two spaces. If you are not used to it, you can change it
in vim.vimrc, open the configuration file, enter / in the bottom line mode, and then you can search /2
insert image description here
. Change all 2 here to 4, then save and exit. Then enter it after coming out source .bashrcto make it effective

Guess you like

Origin blog.csdn.net/m0_67768006/article/details/131258408