Article Directory
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
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
this will appear, so you can write something, write a piece of c language code in it,
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
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.
When switching modes, you cannot insert from the bottom line or the bottom line to insert.
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.
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
one copy, if I want to make more than one, yy+np
(n means I want to paste it out The number of lines)
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
~: 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
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
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.
At this point you can rewrite
shift+r:替换模式,对内容进行整体替换,它是第四种模式,如果要退出那么按Esc即可
x: delete the cursor character, nf: delete n characters starting from the cursor character, delete printf
, 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
I
open a file that does not exist with vim. file, and saving it is equivalent to creating a new file
. 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 + 文件名
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.
If you want to force quit, bring an exclamation point! That's it
! Indicates mandatory execution, and you can compile this code without exiting this file
! gcc code.c
! ls
can even execute this a.out file
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 nu
and 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
but there is no need to create it ourselves at this time. touch .vimrc
Configure set nu in .vimrc
When I use vim to open a file, it will show me the line number by default, indicating that the configuration has taken effect.
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
After configuration, you need to enter or restart xshell to take effect.
After configuration, it will have such a file.
If you don’t want to see it, you can make it a hidden file.
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
, 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
. Change all 2 here to 4, then save and exit. Then enter it after coming out source .bashrc
to make it effective