Vim concise tutorial [coolshell]

Source: Internet
Author: User

Vim's learning curve is quite large (see the learning curve of various text editors). Therefore, if you see a lot of VIM command classification at the beginning, you will lose interest in this editor. The following article is translated from "Learn Vim progressively". I think this is the best Vim Upgrade tutorial for beginners. I did not list all the commands, but just listed the most useful commands. Very good.

---------- The text starts ----------

Do you want to learn vim, the best text editor in history, as quickly as possible? You must first learn how to survive vim and then learn various tricks.

Vim the six billion dollar Editor

Better, stronger, faster.

Learn vim and it will become your last text editor. There is no better text editor than this one, which is very difficult to learn, but incredibly easy to use.

I suggest the following four steps:

  1. Alive
  2. Feeling good
  3. Better, stronger, and faster
  4. Use Vim's super power

After you finish this article, you will become a Vim superstar.

Before getting started, I need to give you some warning:

  • Learning Vim is painful at the beginning.
  • Time required
  • You need to practice constantly, just like learning an instrument.
  • Don't expect you to practice Vim more efficiently than other editors in three days.
  • In fact, you need two weeks of hard work instead of three days.
Level 1-survival
  1. Install Vim
  2. Start Vim
  3. Do nothing!Read

After installing an editor, you will definitely want to enter something in it and then see what the editor looks like. But Vim is not like this. Follow the command below:

  • After Vim is enabled, VIMNormalMode.
  • Let's enterInsertMode. Press I. (Chen Hao Note: you will see the "-insert-" in the lower-left corner of VIM, indicating that you can insert it)
  • Now you can enter the text, just like using notepad.
  • If you want to returnNormalMode. PressESCKey.

Now, you know howInsertAndNormalSwitched. Below are some commands for youNormalMode:

  • iInsertMode, pressESCBackNormalMode.
  • x→ Delete a character of the current cursor.
  • :wq→ Save disk + exit (:wStorage,:qExit) (Chen Hao note: the file name can be followed after W)
  • dd→ Delete the current row and save the row to the clipboard.
  • p→ Paste clipboard

Recommendation:

  • hjkl(For strong examples, we recommend that you move the cursor, but it is not required.) → you can also use the cursor key. Note:jJust like the down arrow.
  • :help <command>→ Display the help of related commands. You can also enter:helpInstead of commands. (Chen Hao Note: To exit the help, enter Q)

You only need the Five Commands mentioned above to survive vim, And you can edit the text. You must train these commands into a subconscious state. So you can start to level 2.

When you enter Level 2NormalMode. In a general editor, you need to useCtrlKey, for example:Ctrl-C. That is to say, the ctrl key is like a function key. After you press the ctrl key, C is no longer a C, and it is a command or a shortcut key,In normal mode of VIM, all keys are function keys.. You need to know this.

MARK:

  • In the following textCtrl-λI will write<C-λ>.
  • To:You need to enter<enter>Press enter, for example,-if I write:qThat is, you need to enter:q<enter>.
Level 2-good feeling

The above commands can only survive you. Now it is time to learn more commands. The following is my suggestion: (Chen Hao note: all commands must be used in normal mode. If you do not know the current mode, press the ESC key several times)

  1. Various insert Modes
    • a→ Insert after the cursor
    • o→ Insert a new row after the current row
    • O→ Insert a new row before the current row
    • cw→ Replace the character from the cursor position to the end of a word
  2. Simple move cursor
    • 0→ Zero number, to the row Header
    • ^→ The first line of the line is not the position of the blank character (the so-called blank character is space, tab, line feed, carriage return, etc)
    • $→ To the end of the row
    • g_→ The last line is not the position of the blank character.
    • /pattern→ Searchpattern(Chen Hao Note: if multiple matches are found, press n to the next one)
  3. Copy/paste(Chen Hao Note: Both P and P can be used. P indicates that after the current position, P indicates that before the current position)
    • P→ Paste
    • yy→ Copy the current rowddP
  4. Undo/Redo
    • u→ Undo
    • <C-r>→ Redo
  5. Open/Save/exit/change the file(Buffer)
    • :e <path/to/file>→ Open a file
    • :w→ Inventory
    • :saveas <path/to/file>→ Save<path/to/file>
    • :x,ZZOr:wq→ Save and exit (:xSave only when needed. ZZ does not need to enter a colon and press Enter)
    • :q!→ Exit and do not save:qa!Forcibly exit all the files being edited, even if other files are changed.
    • :bnAnd:bp→ You can open many files at the same time and use these two commands to switch to the next or previous file. (Chen Hao Note: I like to use: N to the next file)

Take a moment to get familiar with the above commands. Once you have mastered them, you can almost do what other editors can do. So far, you still think that using Vim is a little clumsy, but it doesn't matter. You can go to Level 3.

Level 3-better, stronger, and faster

Congratulations! You did a good job. We can start something more interesting. In Level 3, we only talk about commands that are compatible with VI.

Better

Next, let's take a look at how Vim repeats itself:

  1. .→ (Decimal point) The last command can be repeated
  2. N <command> → repeat a command n times

The following is an example. To open a file, try the following command:

  • 2dd→ Delete 2 rows
  • 3p→ Paste the text three times
  • 100idesu [ESC]→ Write "desu desu desu"
  • .→ Repeat the previous command -- 100 "desu".
  • 3.→ Repeat "desu" three times (Note: It's not 300. You see, how clever Vim is ).
Stronger

To make your cursor move more efficiently, you must understand the following commands,Never skip.

  1. NG→ To line N (Chen Hao Note: note that G in the command is capitalized, And I generally use: n to line N, for example, lines 137 to 137th)
  2. gg→ To the first line. (Chen Hao Note: equivalent to 1g, or: 1)
  3. G→ To the last line.
  4. Move by word:
    1. w→ Start with the next word.
    2. e→ To the end of the next word.

    > If you think the word is in the default format, use the lower case e and W. By default, a word consists of letters, numbers, and underscores (Chen Hao Note: program variables)

    > If you think words are separated by blank characters, you need to use uppercase E and W. (Chen Hao Note: program statements)

Next, let me talk about the strongest cursor movement:

  • %: The matching brackets are moved, including(,{,[. (Chen Hao Note: you need to move the cursor first to brackets)
  • *And#: Match the current word of the cursor, move the cursor to the next (or the previous) match the word (* is the next, # Is the previous)

Believe me, the above three commands are quite powerful for programmers.

Faster

You must remember the movement of the cursor, because many commands can be linked with the commands that move the cursor. Many commands can be used as follows:

<start position><command><end position>

For example0y$Command means:

  • 0→ First come to the line Header
  • y→ Copy from here
  • $→ Copy to the last character of the row

You can enteryeFrom the current position to the last character of the word.

You can also entery2/fooTo copy the strings between two "foo.

There is still a lot of time and you may have to press y to copy the file. The following command will also be copied:

  • d(Delete)
  • v(Visualized selection)
  • gU(Change to uppercase)
  • gu(Small write)
  • And so on.
(Chen Hao Note: Visual selection is a very interesting command. You can press v first and move the cursor to see the selected text. Then, you may d, Y or uppercase) Level 4-Vim super power

You only need to master the previous commands, and you can use Vim comfortably. But now we will introduce you to the vim killer feature. The following are the reasons why I only use vim.

Move the cursor over the current row: 0  ^  $  f  F  t  T  ,  ;
  • 0→ To the line Header
  • ^→ The first non-blank character to the line
  • $→ End of line
  • g_→ The last line is not the position of the blank character.
  • fa→ To the next character with a, you can also use FS to the next character with S.
  • t,→ The first character before the comma. A comma can be another character.
  • 3fa→ Search for the third a in the current row.
  • FAndT→ AndfAndtSame, but in the opposite direction.

Another useful command is:dt"→ Delete all content until double quotation marks --"。

Region selection <action>a<object>Or <action>i<object>

In visual mode, these commands are powerful in the format

<action>a<object>And<action>i<object>

  • Action can be any command, suchd(Delete ),y(Copy ),v(You can select the mode as needed ).
  • The object may be:wOne word,WA word separated by spaces,sOne sentence,pA paragraph. It can also be a special character:"、 ‘、 )、 }、 ]。

Suppose you have a string(map (+) ("foo")). The cursor key is in the first.

  • vi"→ Selectfoo.
  • va"→ Select"foo".
  • vi)→ Select"foo".
  • va)→ Select("foo").
  • v2i)→ Selectmap (+) ("foo")
  • v2a)→ Select(map (+) ("foo"))

Block operation: <C-v>

Block operations, typical operations:0 <C-v> <C-d> I-- [ESC]

  • ^→ To the line Header
  • <C-v>→ Start block Operation
  • <C-d>→ Move down (you can also use hjkl to move the cursor, use %, or something else)
  • I-- [ESC]→ I is insert, insert"--", Press ESC to take effect for each row.

For Vim in Windows, you need to use<C-q>Instead<C-v>,<C-v>Is to copy the clipboard.

Automatic prompt: <C-n>And <C-p>

In insert mode, you can enter the beginning of a word, and then press<C-P> or <c-N> ......

Macro recording: qaOperation Sequence q, @a, @@
  • qaRecord your operations in the registera。
  • Therefore@aMacro that will replay the recording.
  • @@Is a shortcut key used to replay the macro of the latest recording.

Example

In the text with only one line and only one line, type the following command:

  • qaYp<C-a>q
    • qaStart recording
    • YpCopy rows.
    • <C-a>Add 1.
    • qStop recording.
  • @a→ Write down 2 below 1
  • @@→ Write 3 in front of 2
  • Now do[email protected]@A new 100 row is created and the data is increased to 103.

Visual selection: v, V, <C-v>

We can see that<C-v>(In Windows, it should be <c-q>). We can usevAndV. Once selected, you can do the following:

  • J→ Connect all rows (into one row)
  • <Or>→ Left and right indent
  • =→ Auto indent (Chen Hao note: this function is quite powerful and I like it too much)

Add something after all selected rows:

  • <C-v>
  • Select related rows (you can usejOr<C-d>Or/patternOr%......)
  • $To the end of the line
  • A, Input string, pressESC。

Split screen: :splitAnd vsplit.

The following are the main Commands. You can use Vim's help.:help split. You can refer to the previous article Vim split screen on this site.

  • :split→ Create a split screen (:vsplitCreate a Vertical Split screen)
  • <C-w><dir>: Dir is the direction, which can behjklOr one of them, which is used to switch the split screen.
  • <C-w>_(Or<C-w>|): Maximize the size (<c-W> | Vertical Split screen)
  • <C-w>+(Or<C-w>-): Increase the size.

Conclusion
  • The above is the author's most commonly used 90% command.
  • I suggest you learn one or two new commands every day.
  • In two to three weeks, you will feel the power of vim.
  • Sometimes learning Vim is like memorizing something.
  • Fortunately, VIM has many good tools and excellent documents.
  • Run vimtutor until you are familiar with the basic commands.
  • Which of the following must be carefully read in the online help documentation?:help usr_02.txt.
  • You will learn!,Directory, register, plug-in, and many other functions.

Learning Vim is just like learning to play the piano.

---------- Text ended ----------

For VI/vim, let's just comment: this is an editor that you don't need to use a mouse or a keypad. You only need to use a keyboard to edit many complex functions. Otherwise, Visual Studio won't have the vim plug-in.

(Full text)

Vim concise tutorial [coolshell]

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: info-contact@alibabacloud.com 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.