Play the VIM editor

Source: Internet
Author: User
Tags lowercase set background

Read Catalogue

    • One: Vim Quick Start
    • II: VIM Document editing
    • Three: Find replacements
    • IV: Getting Started with advanced features
Back to CatalogOne: Vim Quick start1.vim Mode Introduction

The following introductory content is from the Wikipedia vim

There are many modes of vim from VI, this unique design is easy to confuse the beginner. Almost all editors have two modes of inserting and executing commands, and most editors use a different approach to vim: the command directory (mouse or keyboard driver), the combination of keys (usually with the control key (CTRL) and the ALT key (ALT)) or the mouse input. Vim and VI, just as the keyboard to switch among these modes. This allows vim to operate without a menu or mouse, and to minimize the operation of key combinations. It can greatly enhance the speed and efficiency of the text entry clerk or programmer.

VIM has 6 basic modes and 5 derivation modes, and here we simply introduce the next 6 basic modes:

    • Normal mode

In normal mode, the editor commands, such as moving the cursor, deleting text, and so on. This is also the default mode after Vim is started. This is exactly the opposite of what many new users expect (most editor default mode is insert mode).

Vim's powerful editor can come from its normal mode commands. Normal mode commands often require an operator end. For example, the normal mode commanddddeletes the current row, but the first "D" can be followed by another move command instead of the second oned, such as a "J" key that moves to the next line to delete the current row and the next row. You can also specify the number of commands to repeat,2dd(repeatddtwo times), anddjthe effect is the same. Users learn a variety of text between the move/jump commands and other common mode of editing commands, and can be used flexibly combined, can be more efficient than those without the pattern of the editor for text editing.

In normal mode, there are many ways to get into insert mode. The more common way is to pressa(append/append) ori(insert/insert) key.

    • Insertion modes (insert mode)

In this mode, most keystrokes insert text into the text buffer. Most new users want this mode to persist throughout the text editor editing process.

In insert mode, you can press theESCkey back to normal mode.

    • Visual modes (visual mode)

This pattern is similar to normal mode. However, the move command expands the highlighted text area. The highlighted area can be a character, a line, or a piece of text. When a non-moving command is executed, the command is executed to the highlighted area. Vim's "text object" can also be used in this mode as the move command.

    • Selection mode (select modes)

This pattern is similar to the behavior of the modeless editor (the way Windows standard text controls are). In this mode, you can use the mouse or cursor keys to highlight the selection of text, but the input of any character, Vim will use this character to replace the selected highlighted text block, and automatically enter the insertion mode.

    • Command line mode

In command-line mode, you can enter text that will be interpreted and executed. For example, execute commands (:keys), search (/and?keys), or filter commands (!keys). After the command is executed, VIM returns to the pattern before the command-line mode, usually in normal mode.

    • Ex modes (ex mode)

This is similar to command-line mode, where:visualyou can execute multiple commands at one time before using the command to leave the ex mode.

This is commonly used in common mode, insert mode, and command line mode, and this course covers only the three common patterns.

2. Switching of three common modes

Vim starts into normal mode, in insert mode or command line mode, just pressEscorCtrl+[(this is no use in the VIM course environment) to enter normal mode. In normal modei, press (insert) ora(attach) keys to enter the insert mode, in normal mode press:enter command line mode. In the command line mode, enter thewqcarriage return and save and exit Vim.

Third, enter the VIM1. Use the VIM command to enter the Vim interface

Vim is appended with the file name that you want to open, or that does not exist (then as a new file). Open the Xfce terminal and enter the following command

$ vim practice_1.txt

Vim editor can also be opened using Vim directly, but no files will be opened.

$ vim

Enter the command line mode and the:e File path same file can be opened.

2. Cursor movement

After entering Vim, press theikey to enter insert mode. In this mode you can enter text information below, please enter the following three lines of information:

PressEscEnter normal mode, use the arrow keys in this mode or,,, thehjklKey can move the cursor.

Key Description
h Left
l Right (lowercase L)
j Under
k On
w Move to the next word
b Move to the previous word

Try using the arrow keys to move the cursor over these letters in normal modeshiyanlou.

Iv. Enter insert mode 1. Enter insert mode

Use the following key in normal mode to enter insert mode, and to start typing from the corresponding position

Command Description
i Edit at current cursor
I Insert at beginning of line
A Inserting at the end of a row
a Insert an edit after the cursor
o Insert a new row after the current row
O Insert a new row before the current line
cw Replace the character from the position of the cursor to the end of a word

Try different ways to get into the insert mode from normal mode, add in front of the last line Shiyanlouwww., Notice that each time you go back to normal mode to switch to insert mode in different ways

V. Save a Document 1. Save the document in command line mode

Enter the:command line mode from normal mode, enterwa carriage return, and save the document. Enter to save the:w 文件名document as a different file name or save it to another path

Vi. exit vim1. Exit vim in command line mode

Enter:command line mode from normal mode, enterwqreturn, save and exit edit

Here are a few other ways to exit:

Command Description
:q! Force quit, do not save
:q Exit
:wq! Force Save and exit
:w <文件路径> Save As
:saveas 文件路径 Save As
:x Save and exit
:wq Save and exit
2. Exiting vim in normal mode

Normal mode inputShift+zzcan be saved out of vim

Vii. deletion of text 1. Delete vim text information in normal mode

Go to normal mode and use the following commands to quickly delete text:

Command Description
x Delete the character that the cursor contains
X Delete the previous character of a cursor
Delete Withx
dd Delete entire row
dw Delete a word (not in Chinese)
d$OrD Delete to end of line
d^ Delete to beginning of line
dG Delete to end of document
d1G Delete to document header

In addition, you can add a number to the command before it, indicating that multiple rows are deleted at once, such as:

2ddRepresents deleting 2 rows at a time

Let's do the following exercises:

$ cp /etc/protocols .$ vim protocols

Viii. Summary

Vim first experience you feel very strange, perhaps at present you do not have vim to produce any favor, do not think it is a good editor, it can only be because you have not mastered the essence of vim, or you type too slow, go back to the Little Overlord learning machine to practice it-_-| | Here can also be revealed, the essence of vim is the command key combination, the operation of the whole keyboard, you can not leave the keyboard input area to complete all the operation, that can be real efficient and cool experience ah, when you master this skill can definitely seconds off other not with VIM programmer several street (at least in the momentum bar, And of course to get rid of the only Emacs programmers, because there is a war between Emacs and Vim, it is interesting to see Emacs and Vim here: God's editor and editor of God)

Back to directory two: Vim document editor one, vim repeat command 1. Repeat the last command

In normal mode.(decimal point) means the last command operation is repeated

Copy test files to a local directory

$ cp /etc/protocols .

Open a file for editing

$ vim protocols

Normal mode inputx, delete the first character, the input.(decimal point) will be deleted again a character, in addition to also can be repeateddddelete operation

2. Execute commands of the same number of times

Enter Normal mode inputN<command>, n indicates the number of repetitions, and the following is the practice:

Open file file for editing

$ vim protocols

Below you can do the following exercises in turn:

    • Input10x, delete 10 consecutive characters
    • Input3dd, 3 lines of text will be deleted

In normal mode, you can alsodwdawdelete a word with or (delete a word), so you can easily associate (n with thednwcorresponding number ) to delete n words

Ii. Quick jump of cursors

In normal mode, the following command allows the cursor to be quickly reversed to the specified position, we discuss the rapid implementation of the inline jump and in-line jump

1. Jump between rows
Command Description
nG(n shift+g) Cursor moves to nth row (if no line number is displayed by default, enter command mode first:set nuto display line numbers)
gg Cursor moves to the first row
G(SHIFT+G) To the last line

Let's practice it:

Open the practice document with VIM

$ vim protocols

Do the following exercises in turn:

    • Quickly jump to line 9th and delete the row
    • Go back to the first line, delete 8 rows down
    • Jump to the end of the document and delete the row

tip: After you have completed the jump, you can use theCtrl+oquick to go back to the previous (before the jump) cursor position , this technique is very practical, such as when you write code, suddenly think of a bug, need to change, this time you jump over to change, just need to pressCtrl+oYou can go back to where you were before. Vim will be waiting for you to explore with a lot of similar tips.

2. In-line jump

In normal mode, use the following command to jump within a line in a word

Command Description
w To the beginning of the next word
e To the end of the next word
b To the beginning of the first word
ge To the end of the previous word
0Or^ to the wardrobe.
$ To end of line
f<字母> Search backwards < letters > and jump to the first matching position (very useful)
F<字母> Search forward < letters > and jump to the first matching position
t<字母> Search backwards < letters > and jump to a letter before the first matching position (not used)
T<字母> Search forward < letter > and jump to a letter after the first matching position (not used)

Do the following exercises in turn:

    • In normal mode, jump to a line, usewjump to the beginning of a word, and then usedwthe word delete
    • In normal mode, useejump to the end of a word and use~the letter that contains the cursor to capitalize or lower case

Third, copy paste and cut 1. Copy and paste text
    • Use replication in normal modey

      • In normal mode,yythe entire line where the cursor is copied (3yyrepresenting the replication of 3 rows)
      • In normal mode,y^copy to the beginning of the line, ory0. Does not contain the character at which the cursor is located.
      • In normal mode,y$copy to end of line. The character where the light is contained.
      • In normal mode,ywcopy a word.
      • In normal mode,y2wcopy two words.
      • In normal mode,yGcopy to the end of the text.
      • In normal mode,y1Gcopy to the beginning of the text.
    • Use paste in normal modep

      • In normal mode,p(lowercase) represents pasting to the cursor (bottom)
      • In normal mode,P(uppercase) means pasting to the front of the cursor (top)

Open the file into normal mode practice the above command, you can now feel freeyyto

$ vim protocols
2. Cut and paste

In fact, thedddeletion of the command is cut, each time youdddelete the contents of the document can be usedpto paste, also this allows us to achieve a very refreshing function-exchange up and down line:

ddp, so simple, that it implements the fast Exchange cursor where the line is with the line below it

Iv. Summary

In this section you should have mastered several frequently used operations, including fast inter-line movement and fast inline movement, as well as cutting and copying and pasting operations, and I hope you will be able to practice more skillfully, and once you have mastered these operations, you can greatly improve your productivity.

Back to directory three: find replacement one, character substitution and revocation (undo action) 1. Replace and revoke (undo) command

Both the Replace and undo commands are for normal mode operations

Command Description
r+< to replace letters > Replace the letter that contains the cursor with the specified letter
R Continuous replacement until you press theEsc
cc Replace the entire row, which deletes the row of the cursor and enters insert mode
cw Replace a word, delete a word, and enter insert mode
CUppercase Replace cursor later to end of line
~ Reverses the case of a cursor in the same letter
uN Undo One or N operations
UUppercase Undo all changes to the current line
Ctrl+r Redo, that is, undo undo operation

To open a file for editing:

$ vim practice_2# 输入以下文本

Then do the following:

    • Input11G, jump to 11 lines
    • Enter FA to jump to the firstacharacter
    • Inputr, and the input b,a character is replaced by the B character (practical)
    • Enter aRreplacement character, enter a new string, press ESC to return to normal mode (useful)
    • Enterccreplace whole line character, enter new string, press ESC to return to normal mode after entering
    • Entercwreplace one English word (word), press ESC to return to normal mode (useful)
    • Input~, the case of the character in which the rollover cursor is located
    • The input isCreplaced with the end of the line, that is, the word will be replaced after the cursor is in place, press ESC to return to normal mode
    • Enteruundo Last action

Second, fast indent 1. Use commands to quickly adjust indent operations

This section learns how to indent quickly in vim and indent operations are effective in normal mode

Open a file for editing

$ vim protocols
    • Normal mode input15G, jump to 15 lines
    • Input entire line in normal mode>>indent right (used to format code super cool)
    • Enter the entire line in normal mode to the<<left fallback
    • Normal mode enter:command line mode toshiftwidthset values to control the number of characters for indentation and fallback
2.shiftwidth command

shiftwidthThe command refers>>to the indentation generated by the previous command (which can be abbreviatedsw). Enter:command line mode to set values in normal mode to control the number of characters inshiftwidthindentation and fallback get current setpoint

:set shiftwidth?

Set indent to 10 characters

:set shiftwidth=10

EnterESCback into normal mode and try again to>>see if the indent changes

3. Adjust the text position

Command line mode enter:ce(center) command to center the contents of the bank


Command line mode enter:ri(right) command to make our text


Command line Mode enter:le(left) command to leave the contents of the bank


Third, find 1. Quick Find

Enter Normal mode/and then type the string you want to find and press ENTER to find it.?is the same as the/function, but?looks up and/down. After entering the lookup, the inputnandNcan continue to findnthe expression to continue the lookup,Nreverse Lookup

2. Quick Find Exercises

Use VIM to open a file for editing

$ vim protocols
    • Normal mode input/icmpfind string ICMP
    • Normal mode Enter tonfind the next ICMP
    • Normal mode input?tcpup lookup string TCP
    • Normal mode inputNto find the last occurrence of TCP
3. Advanced Search
    • Enter\*a word in normal mode to find where the cursor is located
    • Normal mode input\#Ibid, but\*forward (up) to find, #则是向后 (bottom) find
    • Normal modeg\*\*, but partially matches the word
    • Normal modeg\#\#, but partially matches the word

Search fornThe above,Ncontinue to find command can still be used

Back to table four: Advanced features get started one, multi-file editing 1. Use Vim to edit multiple files

There are two forms of editing multiple files, one being a parameter that is used before entering Vim is multiple files. Another is to enter vim and then edit the other files. Create two new files and edit them simultaneously

$ vim 1.txt 2.txt

Default Access1.txtto file editing interface

    • Command line mode input:nedit 2.txt file, you can!:n!Force switch, the input of a file is not saved, just switch to another file
    • Command line mode input:Nedit 1.txt file, can be!forced to:N!switch, the input in the previous file is not saved, just switch to another file
2. Open a new file after entering vim
    • Command line mode enter:e 3.txtopen new file 3.txt
    • Command line mode input:e#back to the previous file
    • command-line mode input:lsto list previously edited documents
    • Command line mode input:b 2.txt(or number) can be directly into the file 2.txt edit
    • Enter:bd 2.txt(or number) in command-line mode to delete a file item from a previously edited list
    • Command line mode input:e! 4.txt, new open file 4.txt, discard the file being edited
    • Enter:fThe file name you are editing in command line mode
    • Input in command line mode:f new.txt, change the file being edited name to New.txt
3. Recover files

If the document is not saved due to a power outage, you can use the recovery method, enter thevim -rdocument, input:ewcover 1.txtto restore

$ vim -r 1.txt
Second, Visual Mode 1. Introduction to Visual Mode commands
    • Enter in normal modev(lowercase), enter the character selection mode, you can move the cursor, where the cursor will be selected. The selection is deselected when the V-meeting is pressed again.
    • In normal mode inputShift+v(lowercase), enter the line selection mode, press V will be the entire row selection, you can move up and down the cursor select more rows, again, press onceShift+vcan be deselected.
    • In normal modeCtrl+v(lowercase), this is the area selection mode, you can select the rectangular region, and then cancel the selection by one timeCtrl+v.
    • EnterdDelete selection area in normal mode
    • EnteryCopy selection area contents in normal mode
2. Visual Mode Command Practice

Copy the practice file to the current directory

$ cp /etc/protocols .

Open the practice file

$ vim protocols
    • In normal mode9G, jump to line 9th, enterShift+v(lowercase v), enter the visual mode for row selection, select 5 rows, press>>indent, indent 5 rows into oneshiftwidth
    • Enter in normal modeCtrl+v(lowercase v), enter visual mode for rectangular area selection, select the first column character and thenxdelete the whole column

Third, Windows Operation 1. Introduction to Windows operation

Vim can open multiple windows in one interface for editing, which are called Vim windows. There are many ways to open a method, for example, you can use the command line mode input to:newopen a new Vim window, and enter the window to edit a new file (Normal modeCtrl+wcan also be entered, butCtrl+win Chrome will be closed tab page with Chrome shortcut keys conflict, So with this shortcut you can practice in IE or other browsers, in addition to the:newcommand, the following list of methods can also be used in command mode or Normal mode to open a new window:

    • Command-line mode enter:sp 1.txtopen new Landscape window to edit 1.txt
    • Command-line mode:vsp 2.txtto open a new vertical window to edit 1.txt
    • Ctrl-w ssplit the current window into two horizontal windows in normal mode
    • Ctrl-w vsplit the current window into two vertical windows in normal mode
    • In normal modeCtrl-w q, it is: Q ends the split window. If you have input in a new window, you need to use the mandatory character! namely: q!
    • Ctrl-w oOpen a window in normal mode and hide all previous windows
    • Ctrl-w jmove to the following window in normal mode
    • Ctrl-w kmove to the upper window in normal mode
    • Ctrl-w hmove to the left window in normal mode
    • Ctrl-w lmove to the right window in normal mode
    • Ctrl-w Jmove the current window below in normal mode
    • Ctrl-w Kmove the current window to the top in normal mode
    • Ctrl-w Hmove the current window to the left in normal mode
    • Ctrl-w Lmove the current window to the right in normal mode
    • Ctrl-w -reduce the height of the window in normal mode
    • Ctrl-w +increase the height of the window in normal mode
2. Windows Operation exercises

Open the practice file

$ vim 1.txt
    • command-line mode input:newopen a new vim window
    • Command-line mode enter:sp 2.txtopen new Landscape window to edit 2.txt
    • Command-line mode enter:vsp 3.txtopen new Landscape window to edit 3.txt
    • If you use a non-Chrome browser, you can useCtrl+wthe jump between windows
    • Enter:q!exit multi-window editing in command-line mode of different windows

Iv. document Encryption 1. Create an encrypted document
$ vim -x file1

Enter your password to confirm the password so that the next time you open, VIM will ask you to enter the password

V. Executing external commands in vim

Enter command-line mode!to execute external shell commands

    • :!lsUsed to display the contents of the current directory
    • :!rm FILENAMEUsed to delete a file named filename
    • :w FILENAMESave the file you are editing in the current VIM as a filename file
Vi. viewing Help in the Help system 1.vim
    • PressF1vimto open your own preset help document in normal mode
    • Enter:h shiftwidthshiftwidthThe open Help file in command line mode
    • Input:verdisplay version and parameters in command line mode
Seven, function set 1.vim function setting

Can be set when editing the file function settings, such as the command line mode input:set nu(display number of lines), set value exit Vim will not be saved. To permanently save the configuration, you need to modify the Vim configuration file. Vim configuration file~/.vimrc, you can open the file for modification, but be careful not to affect the normal use of vim

2. Get the current settings
    • Enter:setor:sedisplay all modified configurations in command line mode
    • command-line mode input:set alldisplays all the SetPoint values
    • Command-line mode:set option?to enter a setting value that displays option
    • Command line mode to enter the:set nooptioncancel current set value
Description of the 3.set function
    • :set autoindent(ai)automatic indentation of input settings in command line mode
    • Command line mode input:set autowrite(aw)set AutoArchive, default not open
    • Enter or in command line:set background=darkmodelight, set the background style
    • Command line mode input:set backup(bk)set automatic backup, default not open
    • Command line mode input: set cindent(cin)set C language style indent

For more detailed parameters, please refer to the VIM manual

Viii. Summary

Through these four chapters of simple learning, you should have mastered the basic operation and use of VIM, but the main purpose of this course is to let you learn in the experimental building other need to use vim of the course without any problems. If you want to simply learn and master the Vim editor, through a variety of tutorials including the course of learning is not able to meet, because to master is related to your personal choice, which requires your constant contact and adhere to the long-term use of vim to complete a variety of editing operations to achieve, At the same time you need to master how to change and write vim configuration files and install various VIM plug-ins to achieve a variety of powerful features to meet your various demanding needs, and finally hope you have a good time in the experimental building

Play the VIM editor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.