Python Programming Time

I am a heavy user of Vim. Because I like the feeling that I can control everything without leaving the keyboard with both hands, Vim can make my text operation more accurate and efficient.

For those of you who have never used Vim, you may not know this feeling. Because of the learning cost of using Vim, you have to be very skilled to feel the speed it brings.

Here I make a summary of Vim commands I have used daily, which is divided into 21 points. I suggest that students who want to learn Vim can try more according to the article and search engine. I believe you will gradually like Vim.

This article is more for those who have some basic knowledge, because there are many points that, if written in too much detail, can become quite wordy.

1. The vim model

In normal mode (press Esc or Ctrl+[enter) the file name is displayed in the lower left corner or in empty INSERT mode (press I to enter) the lower left corner is displayed --INSERT-- VISUAL mode (press V to enter) --VISUAL--Copy the code

2. Open the file

#Open a single file
vim file    
#Open multiple files at the same time
vim file1 file2..  

#Open a new file in the VIm window:open [file]#Currently open 1.txt, did some editing but did not save:open! Discard these changes and reopen the unmodified file
#Currently open 1.txt, make some edits and saveTXT :open 2. TXT: exit from 1. TXT, open 2. TXT: exit from :wq and vim 2. TXT again

#Open remote files, such as FTP or share Folderftp://192.168.10.76/abc.txt: e: \ e qadrive \ test \ 1. TXT
#Open the file as read-only, but you can still use :wq! write
vim -R file 

#The :wq! Cannot be used because the modification function is forcibly disabled. write
vim -M file 
Copy the code

3. Insert commands

I inserts I before the current position and A after the current position and O at the end of the current line and o after the current line and o before the current lineCopy the code

4. Search for commands

The easiest search

/text    To find text, press the n key to find the next one, press the n key to find the previous one. ? text    Find text, reverse lookup, press n to find the next, press n to find the previous. Some special characters in vim need to be escaped during look-ups.   . * [] % ^ /? ~$ :set ignorecase    Case-insensitive lookup :set noignorecase    Lookups that do not ignore caseCopy the code

Fast search, do not need to hand typed characters to find

* Go back (down) to find the word where the cursor is# Go ahead (up) to find the word where the cursor isThe above two types of search, n, n continue search command still appliesCopy the code

Precise search: Match word search

If you have hello, HelloWorld, HelloPython in the text

So IF I use /hello, all three words match.

Is there a way to do that? You can use

/hello\>
Copy the code

Precise search: match the beginning and end of a line

#Hello is at the beginning of the line
/^hello

#World is at the end of the line
/world$
Copy the code

5. Replace the command

~ Invert cursor case R< letter > replaces the current character with the written letter R< letter >< letter >... Continuous replacement letter cc to replace the entire line (is to delete the current line, and insert the next line) the cw to replace a word (is to delete a word, it into insert mode), the premise is the cursor the first letter in the word (or b) C (capital C) replacement to the end of each line (and D, D is deleted (shear) to the end of each line, S /old/new/ replace new with old, replace the first match of the current line :s/old/new/g replace new with old, replace all matches of the current line :%s/old/new/ replace new with old, Replace the first match on all lines :%s/old/new/g Replace new with old, and replace all matches in the entire file :10, 20s /^/ /g Add four Spaces before each line from 10 to 20 for indentation. DDP swaps the line where the cursor is and the line immediately below it.Copy the code

Undo and redo

U Undoes Ctrl + R Redo of an entire line, that is, Undo Undo.Copy the code

7. Delete the command

It should be noted that Vim does not actually have a delete command, which you might think is more accurate.

Delete in character units

X Deletes the current character 3x Deletes the current character three times x deletes the previous character of the current character. 3X deletes the current character dl, dl=x dh deletes the previous character, x =dh D deletes the current character to the end of the line. D=d$d$Deletes the current character to the end of the lineD ^ Deletes the current character at the beginning of the lineCopy the code

Delete by word

Dw deletes the current character to the end of the word daw deletes the current character wordCopy the code

Delete by behavior unit

Dd to delete the current line under the DJ to delete a line on the dk to delete a line DGG to delete the current to the first document d1G deleted to delete the current to the first dG document to the current document tail KDGG delete the current line before all lines (not including the current line) jdG to delete the current line after all rows (not including the current line) 10d Deletes the first 10 lines of the current line. $d = 1; $d = 1; $d = 1; $d = 1; &emsp; Deleting a blank line between two lines actually merges the two lines.Copy the code

8. Copy and paste

Y replication is used in normal mode

Yy copies the entire row where the cursor is located (3yy means 3 rows are copied) y^ to the beginning of the row, or y0. Contains no character where the cursor is located. Y $is copied to the end of the line. Contains the character where the cursor is located. Yw copies a word. Y2w copy two words. YG is copied to the end of the text. Y1G is copied to the beginning of the text.Copy the code

In common mode, P paste is used

P(lower case) : pastes the cursor to the right side. P(upper case) : pastes the cursor to the left side.Copy the code

Cut and paste

In normal mode, press V (word for word) or V (line by line) to enter visual mode. Then use the JKLH command to move to select some lines or characters. Then press D to cut NDD and cut n lines after the current line. Use the p command to paste the cut content :1,10d Cut lines 1-10. Use the p command to paste the cut content. :1, 10 m 20 Move lines 1-10 after line 20.Copy the code

10. Exit the Settings

:wq Save and exit ZZ save and exit :q! Force exit and ignore all changes: E! Discard all changes and open the original file. ZZ Save and exit :sav(eas) new. TXT saves as a new file, exits the editing of the original file and does not save it :f(ile) new. TXT opens a new file, does not save it, exits the editing of the original file and does not save itCopy the code

11. Move command

Move in character units

H moves left one character l moves right one character K moves up one character j moves down one character

#F and FFx find the first character x after the cursor 3fd find the third character D after the cursor F same as F, reverse search.Copy the code

Move in behavioral units

#10 refers to any number and can be specified arbitrarily10H Move 10 characters to the left 10L Move 10 characters to the right 10K Move 10 lines up 10J Move 10 lines down
$Move to the end of the line
3 $Move to the end of the next 3 lines
Copy the code

Move by word

W moves one word forward (the cursor is at the beginning of the word) B moves one word backward e, same as w, except that the cursor is at the end of the word.Copy the code

Move by sentence

(Move to the beginning) Move to the end of the sentenceCopy the code

Jump to end of file

Gg moves to the file header. = [[[== 'G' moves to the end of the file. =]]Copy the code

Other movement methods

^ Move to the first non-whitespace character on the line. 0 moves to the first character on the line (can be a space)Copy the code

Use named tag jumps. Personally, this works well because you can jump across files.

With ma, you can mark this as a, and jump to :marks with 'a 'to view all marks with :delm! You can delete all tagsCopy the code

When viewing the error log, the normal step is for Vim to open the file and then use Shift + G to jump to the last line. Here’s an easier way to jump to the last line immediately upon opening the file. Just add a + between the vim and the file.

vim + you.log
Copy the code

By analogy, you can do this when you want to open a file and immediately jump to a specified line

#Open the file and jump to line 20
vim you.log +20
Copy the code

When you use/search to locate a jump or use: line number to make a precise jump, sometimes we want to return to the last position, how to do this?

Simply press Ctrl+ O to return to the last position.

12. Typesetting function

The indentation

:setshiftwidth? View indentation values:setShiftwidth =4 Sets the indentation value to 4The indentation is best written to the configuration file ~/.vimrc
:set tabstop=4
:set softtabstop=4
:set shiftwidth=4
:setExpandtab >> Indent to the right << unindentCopy the code

If you want to indent code, you can also use == to indent before, or n== to indent multiple lines, which requires that you edit files with extensions recognized by VIm, such as.py files.

typography

:ce center: LE left: RI rightCopy the code

13. Comment the command

Multiline comment

Enter the command line mode, press CTRL + V to enter visual Block mode, then press J or K to select multiple lines, mark the lines that need to be commented, press capital I, and then insert the comment, for example, // Press ESC to comment them allCopy the code

Uncomment multiple lines

Enter the command line mode, press CTRL + V to enter the Visual Block mode, press the letter L to horizontally select the number of columns, such as // to select 2 columns, press the letter J, or k to select the annotation symbol and press the D key to cancel the annotationCopy the code

Complex annotation

: 3, 5 s / # ^ / / g annotation line 3-5:3, 5 s / # ^ / / g terminate 3-5 lines: 1, $s / # ^ / / g comments the entire document: 1, $s / # ^ / / g uncomment the entire document: % s / # ^ / / g comments the entire document, This method is faster :%s/^#//g Uncomment the entire documentCopy the code

Adjust your vision

"zz"The: command places the current line in the center of the screen,"zt"The: command puts the current line at the top of the screen"zb": puts the current line at the bottom of the screen. Ctrl + E Scroll down one line Ctrl + Y Scroll up one line Ctrl + D scroll down half screen Ctrl + U Scroll down one screen Ctrl + F Scroll down one screen Ctrl + B Scroll up one screensetNu opens line number :20 jumps to line 20. 20G jumps to line 20Copy the code

15. Regional selection

To select a region, you need to go into visual mode v in characters, up, down, left, right, v in behavior, up, down and then you can do d Cut/Delete Y Copy Ctrl+ V if it's v uppercase, it's V if it's V lowercase, it's normal. If you're in normal mode, you go into v lowercase mode and you can indent multiple lines with that. GgVG select the full textCopy the code

16. Window control

A new window

#Open two files in two Windows
vim -o 1.txt 2.txt


#Assume that 1.txt is now openTXT open a horizontal window, edit 2.txt: VSP 2.txt open a vertical window, edit 2.txt :split Copy the current window to a new window, content synchronization, cursors can be different :split 2.txt open a horizontal window in a new window
#Note: The content is synchronized, but the cursor position is independentCtrl-w S Divides the current window into horizontal Windows Ctrl-W V Divides the current window into vertical Windows Ctrl-w Q Equals: Q Ends the split window. Ctrl-w q! Equivalent: q! End the split window. Ctrl-w O opens a window and hides all previous WindowsCopy the code

Window switch

#Special note: Ctrl W < letter > does not need to be pressed at the same timeCtrl-w H to the left ctrl-w L to the right Ctrl-w J to the lower ctrl-w K to the upper window

#Note: In full screen mode:n Switch to the next window :n Switch to the previous window :bp Switch to the previous window
#Note: Non-full-screen modeBn switches to the next window, the contents of the window in the current position change, the other Windows remain unchanged :bn switches to the previous window, the contents of the window in the current position change, the other Windows remain unchangedCopy the code

Windows mobile

#Special note: Ctrl W < letter > does not need to be pressed at the same timeCtrl-w J Moves the current window to the bottom Ctrl-W K moves the current window to the top Ctrl-W H Moves the current window to the left Ctrl-W L Moves the current window to the right Ctrl-Ww Toggles the Windows in sequenceCopy the code

Adjust the size

#Friendly tip: the keyboard should not be in Chinese stateCtrl-w + Increases window height Ctrl-w - Decreases window heightCopy the code

Out of the window

:close Close the current window :close! Force close current window :q exit, do not save :q! Forced exit, do not save :x save exit :wq Save exit :wq! Forcibly save and exit :w <[path /] file name > Save as :savesa <[path /] file name > Save as ZZ Save and exit. :only Close all Windows, save only the current window (prerequisite: other window content changes must be saved first) :only! Close all Windows except the current window :qall abandons all operations and exits :wall saves all, :wqall saves all and exits.Copy the code

17. Document encryption

Vim -x file_name Then enter the password: Confirm the password: Save the password if you do not change it. :wq, otherwise, the password setting does not take effect.Copy the code

18. Record macro

Press Q to add any letter to start recording, then q to end recording (this means macros in VIm cannot be nested), using @macro names such as QA… Q records a macro named A, which @A uses.

19. Run the command

#Repeat the previous command
. 

#Executing shell commands:! command
#Such as listing files in the current directory:! ls
#Execute the script:! Perl -c script.pl checks the syntax of perl scripts. It is very convenient to exit vim. :! Perl script.pl executes Perl scripts without exiting vim, which is very convenient. Suspend or Ctrl-z suspends vim and returns to shell. Press fg to return to VIm.Copy the code

20. Help commands

On Unix/Linux systems$ vimtutor

#In common modeKeyboard input vim or F1
#In cli mode:help displays the entire help :help XXX displays the help for XXX, such as :help I, :help CTRL-[(that is, CTRL +[help). The help for the :help 'number' Vim option is enclosed in single quotes on Windows: Help TutorCopy the code

21. Configure commands

Display current Settings

:set or :se Displays all Settings that have been changed :set all Displays all Settings :set option? Set nooption Cancel current set value :ver Display all viM information (including version and parameters, etc.)
#Note: In full-screen mode:args Views the list of currently open files. The files currently being edited are enclosed by []Copy the code

Change the setting

Set autoindent(AI) set Autowrite (AW) Set automatic archiving, not enabled by default set backup(bK) set automatic backup, Set background=dark or light; Set background style set cindent(cin) Set C language style indent :set ts=4 Set TAB key to convert to 4 Spaces :set ff= Unix # Modify file DOS file to Unix :set Shiftwidth? View the indentation value :set Shiftwidth =4 Set the indentation value to 4 :set ignorecase&emsp; &emsp; Case-insensitive lookup :set noignorecase&emsp; &emsp; Paste # insert :set ruler? &emsp; &emsp; In. Vimrc, all options set by using the set command can be viewed through this command :scriptnames&emsp; &emsp; View the location of vim script files, such as.vimrc files, syntax files and plugin files. :set list Displays non-print characters, such as TAB, space, end of line, etc. If TAB cannot be displayed, make sure that the.vimrc file is set with set LCS = TAB :>- and make sure that you have a TAB in your file. If the expendTab is enabled, TAB will be expanded to a space. Syntax clear clears defined syntax rules :syntax case match is case sensitive, int and int will be treated as different syntax elements :syntax case ignore, Int and int are treated as the same syntax element and use the same color schemeCopy the code

The above is my summary of using Vim. I hope it will be helpful to you.


Finally, I give you two pictures of Vim keyboard, you can set it as your computer desktop, you may be helpful to learn Vim.

You can follow the public account “Python Programming Time” and reply to “vim” in the background to get a large hd image.