preface
A new friend came to our group last month, just when I was on vacation! When I came back from my vacation, this new friend became my deskmate, and then we worked on projects together. Just yesterday when we were configuring the environment, modifying the various configuration files; My friend was surprised to see me using all the vim shortcuts: “Oh my God, how do you remember so well? Who can remember that? Teach me?” .
In fact, it is very simple: write more, practice more, for example: a former colleague, write code only using vim edit mode, I learned from this.
About liunx under vim instructions, I just graduated when I drew a mind map (so long I thought not deleted, cloud disk was cleared), but can according to the following knowledge points, one by one to see and local operation again. 🤣 😏 😜
01 Vim configuration file
Let’s take a look at our local vim configuration file information, here is the MAC side:
Vim ~ /. (1) liunix end vimrc | MAC vim/usr/share/vim/vimrc into the configuration file
If you don’t know where the vimrc file is, you can use :scriptnames to view it, for example:
① vim hello.lua(2) :scriptnamesThe following list appears:1: /usr/share/vim/vimrc -- This is a local configuration file2: /usr/share/vim/vim80/defaults.vim
3: /usr/share/vim/vim80/plugin/getscriptPlugin.vim.13: /usr/share/vim/vim80/plugin/vimballPlugin.vim
14: /usr/share/vim/vim80/plugin/zipPlugin.vim
Copy the code
Let’s look at the default configuration:
" Configuration file for vim
set modelines=0 " CVE-2007-2438
" Normally we use vim-extensions. If you want true vi-compatibility
" remove change the following statements
set nocompatible " Use Vim defaults instead of 100% vi compatibility
set backspace=2 " more powerful backspacing
" Don't write backup file if vim is being called by "crontab -e"
au BufWrite /private/tmp/crontab.* set nowritebackup nobackup
" Don't write backup file if vim is being called by "chpass"
au BufWrite /private/etc/pw. *set nowritebackup nobackup
let skip_defaults_vim=1
set nu "Line no.
set tabstop=4 "A TAB is four Spaces long
set ai "Set automatic indentation
syntax on "Highlighting
" Source a global configuration file if available
if filereadable("/etc/tmp/vim/config/vimrc.local"By default, I do not modify the configuration, and load the required configuration in a separate filesource /etc/tmp/vim/config/vimrc.local
endif
"The SetTitle function is automatically called when creating files such as.H. C. hpp.cpp.mk.sh
autocmd BufNewFile *.[ch],*.hpp,*.cpp,Makefile,*.mk. *.sh exec ":call SetTitle()"
"Add comments"
""Define the function SetTitle to automatically insert the file header func SetTitle()if &filetype= ='sh'
call setline(1."# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =") -- the delimitercall setline(2."#")
call setline(3."# file Name:".expand("%:t")) -- file namecall setline(4."# copyright @author: I'm Amu"The name of the person who created the filecall setline(5."# created Time:".strftime("%c")) -- Create datecall setline(6."# mail: "2511221051@qq.com) -- create emailcall setline(7."# desc:") -- File function descriptioncall setline(8."#")
call setline(9."# = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =")
call setline(10."#! /bin/sh") -- file typecall setline(11."")
call setline(12."")
endifEndfunc ### has many other commandsCopy the code
02 Overview of vim’s basic concepts
Vim has three modes: command mode, Insert mode and last line mode. The three modes can be switched at will, as shown in the figure below:
02.1 Vim command line mode
When we use Vim to edit a file, we default to the command line mode. In this mode, you can use the arrow keys on the keyboard: up, down, left, right, or K, J, H, l to move the cursor. Of course, we can also use shortcut keys to copy, paste, delete, replace the contents of the file and a series of operations. Some common commands:
X delete the character before the current cursor x Delete the character before the current cursor: Switch to the bottom-line command mode and enter dd at the bottom line to move the cursor to a certain position and delete a whole line of data. O Start a new line below the current line and change to the input mode. Shift +g Jumps to the end of the fileCopy the code
The following figure shows a schematic of vim in command line mode.
02.2 Vim input mode
In vim input mode, you can write files with write permission. It’s pretty much the same as what we normally do in an editor. Then we usually enter the input mode directly by command mode: I, I, a, a, O, O and so on insert command can enter; When we finish editing the file, press esc to exit exit mode and return to command mode:
shortcuts | Functional description |
---|---|
i | In the cursorOn the left side of the Enter text, and the text to the right of the cursor moves to the right |
I | Where the cursor isThe beginning of a line with Enter text, which is equivalent to executing the I command at the beginning of the line |
a | In the cursorOn the right side The input text |
A | Where the cursor isThe tail Entering text is equivalent to running the a command at the end of the cursor line |
o | On the line where the cursor isThe next line To add a new line, the cursor stays at the beginning of the new line |
O | On the line where the cursor isOn a line To add a new line, the cursor stays at the beginning of the new line |
The following diagram shows vim in input mode:
02.3 Vim Bottom line command mode
The bottom line command mode of Vim: In command mode, press :(notice the English colon). At this time, a: symbol will appear in the lower left of vim window, and then you have entered the bottom line command mode.
Note: the command is automatically returned to the command line mode after execution.
Function: The bottom-line command mode can save, replace, query, and delete the specified content in a file.
The most commonly used command shortcuts are shown below:
shortcuts | Functional description |
---|---|
:q | Exit the VI editor |
:q! | Exit the VI editor without saving the file |
:w | Save the file but do not exit the VI editor |
:wq | Save the file and exit the Vim editor |
:start,endd | Delete from the start line to the end line. The last d identifier is deleted |
:%d | Clear the contents of the file (jump to the first line dG execution can also delete) |
:! command | Leave the Vim editor and run command in command mode to display the result |
:%d | Clear the contents of the file (jump to the first line dG execution can also delete) |
The following figure shows the operation status of Vim after it enters the bottom-line command mode:
Vim common commands + shortcut keys
PS: You can pay attention to GIF graphics, each operation animation will have a command; Pay attention to watchCopy the code
03.1 Vim Open file command
① Normally open a file with the cursor on the first line by default
➜ ~ vimFilename -- filename indicates the filenameCopy the code
By default, the cursor is at the end of a file
➜ ~ vim+ package.json -- the name or path of the file to open package.jsonCopy the code
The default line on which the cursor is positioned when a file is normally opened
➜ ~ vim+num package.json -- num identifies the line where the cursor is positionedCopy the code
In 03.2 vim command mode, the cursor moves
① Character movement command
① h Move the cursor one space to the left ②jMove the cursor 3 Spaces downkMove the cursor up one space (4)lMove the cursor one space to the right. Note that these move commands can also be used to show how much we moved last time. For example:2j-- Indicates downward movement2"Copy the code
② Word movement command
1.wMove the cursor to the right at the beginning of the next wordbMove the cursor forward to ③ at the beginning of the previous wordeJumps the cursor to the end of the current or next wordCopy the code
③ First and last line move command
$move the cursor to the end of the current line0Move the cursor to the beginning of the current line0O ③ gg Move the cursor to the first position of the file ④ G Move the cursor to the end of the file ⑤ num+G Move the cursor to the line of the file -- num indicates the line of the file ⑥ :num In the bottom-line command mode, directly give the trip number. It's also possible to jump -- num hops to rowsCopy the code
④ Exit command
(1) :wqSave the file and exitviEditor ② :wSave the file, but do not exitviEditor ③ :q exitsviEditor ④ :q! Exit without saving the fileviEditor ⑤ ZZ saves the file and exitsviThe editorCopy the code
⑤ Page turning command
① CTRL + D moves the cursor down half the screen, usually each time12(2) CTRL +uScroll the cursor up half the screen, usually each time12(3) CTRL +fScroll the cursor down the screen, usually each time24(4) CTRL +bMove the cursor up and over the screen, usually each time24lineCopy the code
⑥ Cut and paste key commands
① d delete the text in the specified location and temporarily store it in the cache; You can usepAccess the cache; This is often used: d, :num,numd, dd(delete the whole row),.. 2.yCopies the specified text to the temporary cache; You can useputOperator access; Often used like this:yYy (copy the whole line) ③pPuts the contents of the specified cache below the cursor position. The entire line of text is placed below the line, or (4) behind the cursor if it is notPPuts the contents of the specified buffer above the current cursor position; The full line of text is placed below the line, or behind the cursor if it is notCopy the code
⑦ Text modification key command
1.xDelete the character specified by the cursor position ② dd Deletes the line where the cursor is located ③uUndo recent changes, for all changes ④ U Undo all changes made when forward ⑤ r Replaces a character at the cursor position, but does not enterinsertMode; For example,2R: put behind2⑥ R replaces the character starting from the cursor position and changes at the same timeviGo to text input mode ⑦. Repeat the previous modification -- note that this command is: English symbol dotsCopy the code
⑧ Search and replace commands (in the bottom line command mode)
(1) : /stringStart from the beginning of the file to the end of the search; Press n to get to the next one,NThe previous record can be found by pressing the up and down key after pressing /.stringSame as above, %s/word1/word2/g Replace word1 with word2 from the beginning to the end of the file ④ :num,num1s/word1/word2/g Replace word1 with word2 ⑤ from line num to line num1 Step 6 :s/str1/str2/g step 6 :s/str1/str2/gCopy the code
⑨ Save part of the current file as another file
Note that this is in the bottom line command mode :num,$w test.luaSave the current file from line NUM to the last line to test.luaIn theCopy the code
⑩ Populate the current file with the contents of other files
# # # note is in the bottom line command mode: r/usr/local/var/weixin/test.logReads the contents of the specified file, inserted at the end of the current lineCopy the code
⑩ Delete a text word command
D $delete all words from the current cursor to the end of the line ④ d^ Delete all words from the current cursor to the first of the line ⑤ DNJ Delete n lines from the current cursor N indicates the number of lines to be deleted. ⑥ DNK Deletes n lines upward from the current cursor. N indicates the number of lines to be deletedstring/d Delete the current filestring%s/^\n$//g = %s/^\n$//gcThe command will have the same effect, but it will becomeinsertmodelCopy the code
⑩ Text content movement commands
① >> Move the row to the right by some distance ② << Move the row to the left by some distance ③ num<< Move the following rows (including num) to the left by some distance ④ :num,num1>> Move the rows from NUM to num1 by some distance to the rightCopy the code
Batch add comment command in ticker vim
Method 1: Block selection mode (common mode)1, CTRL + V to enter block selection mode, then move the cursor to select the line you want to comment,2, and then press capital I to enter the insert mode at the beginning of the line to enter the comment symbol; For example: # or //(double slash)3When you have finished typing, press ESC twice and Vim will automatically annotate all the lines you have selected. You may need to wait a few seconds to unannotate them in batches:1CTRL + V to go to block selection mode and select the comment symbol at the beginning of the line you want to delete2:start,edns/^/ note /g -- start :start line number; Edns /^ edns/ /g -- start :start line number; End: end line number: comment: # or //Copy the code
12) Select the command
① CTRL + V to enter block selection mode, move the cursor to the word you want to select ② according to hj k lTo select the module you need ③ you can perform various operations on the selected dataCopy the code
In passing display and cancel the line number
① :set nu Displays the line number. You can also enter set number. ② :set nonu Cancellations the line numberCopy the code
14) Specifies the automatic indentation of the file contents
(1) :setAutomatic indentation is enabled by AI.setNoai turns off automatic indentationCopy the code
The found text is highlighted
(1) :setHlsearch to enable text highlightingset nohlsearchTurn off text highlightingCopy the code
15) Persons files are edited together
➜ ~vimfilename1 filename2 filename3 ... Open multiple files, the first one is opened by default.prevSwitch to the previous file ② :NSwitch to the previous file ③ :nextSwitch to the next file ④ :n Switch to the next file ⑤ :lastSwitch to the last file ⑥ :firstSwitch to the first file ⑦ :qa: Quit allCopy the code
⑯ Persons Feature multiple Windows
## Pay attention to ① :spFilename horizontal opens a new window showing the new file, if only enter:sp, the same file is displayed in both Windowsw+ s Horizontal split window ③ CTRL +w+ v Vertical split window ④ CTRL +w + jMove the cursor to the lower window ⑤ CTRL +w + kMove the cursor to the upper window ⑥ CTRL +w+ q leave the current window ###Copy the code
conclusion
These are some of the vim commands THAT I’ve used a lot over the years. Keep in mind that they are common! Remember to use it more often