当前位置:网站首页>Brief VIM training strategy

Brief VIM training strategy

2020-11-08 17:53:00 RioTian

vim The learning curve is quite large ( See the learning curve of various text editors ),

therefore , If you start with a bunch of VIM Command classification of , You will lose interest in this editor . The following article is translated from 《Learn Vim Progressively》, This is the best for beginners VIM Upgrade the tutorial , It doesn't list all the commands , It's just a list of the most useful commands . Very good .

—————————— Text begins ——————————

You want to learn as quickly as possible the best text editor in human history VIM Do you ? You have to know how to be in VIM Survived , And learn all kinds of tricks bit by bit .

Vim the Six Billion Dollar editor

Better, Stronger, Faster.

Study vim And it will be your last text editor to use . There is no better text editor than this , It's very difficult to learn , But it's incredibly easy to use .

I suggest the following four steps :

  1. Survive
  2. Feel good
  3. Feel better , stronger , faster
  4. Use VIM The power of

When you finish this article , You're going to be a vim Of superstar.

Before learning , I need to give you some warning :

  • Study vim It was painful at the beginning .
  • It takes time to
  • It needs constant practice , It's like learning an instrument .
  • Don't expect you to be in 3 In the day vim Practice more efficiently than other editors .
  • in fact , You need 2 Weeks of hard work , instead of 3 God .

Catalog

first stage – Survive

  1. install vim
  2. start-up vim
  3. Don't do anything ! Please read first

When you install an editor , You must want to type something in it , And see what the editor looks like . but vim That's not the truth , Please follow the command below :

  • Qi dynamic Vim after ,vim stay Normal In mode .
  • Let's get into Insert Pattern , Please press the key i .( Chen haozhu : You'll see vim There is a... In the lower left corner –insert– word , Express , You can type in as an insert )
  • here , You can enter the text , It's like you use “ Notepad ” equally .
  • If you want to go back Normal Pattern , Please press ESC key .

Now? , You know how to do it in Insert and Normal Mode switched . Here are some commands , I can let you in Normal Survive in mode :

  • iInsert Pattern , Press ESC go back to Normal Pattern .
  • x → Delete a character of the current cursor .
  • :wq → Save + sign out (:w Save , :q sign out ) ( Chen haozhu ::w Can be followed by the file name )
  • dd → Delete current row , And save the deleted lines to the clipboard
  • p → Paste the clipboard

recommend :

  • hjkl ( It is recommended to use its moving cursor , But it doesn't have to be ) → You can also use the cursor keys (←↓↑→). notes : j It's like a down arrow .
  • :help <command> → Display help for related commands . You can also just type in :help Instead of following orders .( Chen haozhu : Exit help requires input :q)

Can you in vim Only the one mentioned above is needed to survive 5 An order , You can edit the text , You have to practice these commands into a subconscious state . So you can start to advance to the second level .

when , When you get to level two , I need to say it again Normal Pattern . In a general editor , When you need copy A paragraph of text , You need to use Ctrl key , such as :Ctrl-C. in other words ,Ctrl Keys are like function keys , When you press the function key Ctrl after ,C It's not about it C 了 , And it's just a command or a fast key , stay VIM Of Normal In mode , All the keys are function keys . You need to know .

Mark :

  • In the following text , If it is Ctrl-λ I can write <C-λ>.
  • With : To start, you need to type <enter> enter , for example — If I were :q That means you have to type in :q<enter>.

Level second – Feel good

The orders above will only keep you alive , Now it's time to learn some more commands , Here are my suggestions :( Chen haozhu : All commands need to be in Normal Use... In mode , If you don't know what kind of model you're in right now , You just press it a few times ESC key )

  1. Various insertion 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 characters from the end of the cursor to the end of a word
  2. Simply move the cursor

    • 0 → The number zero , To the head of the line
    • ^ → The first one in the line is not blank Character position ( So-called blank Characters are spaces ,tab, Line break , Return, etc )
    • $ → At the end of the line
    • g_ → The last one in the line is not blank Character position .
    • /pattern → Search for pattern String ( Chen haozhu : If you find multiple matches , May press n Key to the next )
  3. Copy / Paste

    ( Chen haozhu :p/P Fine ,p After the current position ,P Before the current position )

    • P → Paste
    • yy → Copy the current line on ddP
  4. Undo/Redo

    • u → undo
    • <C-r> → redo
  5. open / preservation / sign out / Change file

    (Buffer)

    • :e <path/to/file> → Open a file
    • :w → Save
    • :saveas <path/to/file> → Save as <path/to/file>
    • :x, ZZ or :wq → Save and exit (:x Means to save only when needed ,ZZ You don't need to enter a colon and enter )
    • :q! → Exit without saving :qa! Forcibly exit all files being edited , Even if other documents are changed .
    • :bn and :bp → You can open a lot of files at the same time , Use these two commands to switch between the next or previous file .( Chen haozhu : I like to use :n To the next file )

Take a moment to familiarize yourself with the above commands , Once you've got them , You can do almost anything other editors can do . But so far , You still feel like using vim Still a little clumsy , But that's okay , You can go up to level three .

Level third – Better , stronger , faster

Congratulations first ! You did a good job . We can start something more interesting . At level three , We're just talking about those and vi Compatible commands .

Better

below , Let's take a look vim How to repeat yourself :

  1. . → ( decimal point ) You can repeat the last command
  2. N → Repeat a command N Time

Here's an example , To find a file, you can try the following command :

  • 2dd → Delete 2 That's ok
  • 3p → Paste text 3 Time
  • 100idesu [ESC] → Will write down “desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu “
  • . → Repeat the last order —— 100 “desu “.
  • 3. → repeat 3 Time “desu” ( Be careful : No 300, You see ,VIM How clever ).
stronger

You need to make your cursor move more efficiently , You must understand the following commands , Don't skip .

  1. NG → To the first N That's ok ( Chen haozhu : Pay attention to the... In the order G It's in capital letters , I usually use : N To the first N That's ok , Such as :137 To the first 137 That's ok )

  2. gg → To the first line .( Chen haozhu : amount to 1G, or :1)

  3. G → To the last row .

  4. Move by word :

    1. w → To the beginning of the next word .
    2. e → To the end of the next word .

    > If you think the word is by default , Then use lower case e and w. By default , A word consists of letters , Numbers and underscores ( Chen haozhu : Program variables )

    > If you think the word is by blank Character separator , Then you need to use uppercase E and W.( Chen haozhu : Program statement )

    Word moves example

below , Let me talk about the strongest cursor movement :

  • % : Match bracket move , Include (, {, [. ( Chen haozhu : You need to move the cursor over the brackets first )
  • * and #: Match the current word of the cursor , Move the cursor to the next ( Or the last one ) Match words (* It's the next one ,# It's the last one )

believe me , These three commands are quite powerful for programmers .

faster

You must remember the movement of the cursor , Because a lot of commands can be linked to these commands that move the cursor . A lot of orders can be done like this :

<start position><command><end position>

for example 0y$ Command means :

  • 0 → First of all
  • y → Copy from here
  • $ → Copy to the last character of the line

You can type in ye, Copy from the current position to the last character of the word .

You can also type in y2/foo To copy 2 individual “foo” String between .

There's a lot of time left, and you don't have to press y To copy , The following command will also be copied :

  • d ( Delete )
  • v ( The choice of Visualization )
  • gU ( Capitalize )
  • gu ( Change to lowercase )
  • wait

( Chen haozhu : Visual selection is an interesting command , You can press first v, Then move the cursor , You'll see the text selected , then , You may be d, Can also be y, It can also be capitalized )

Level fourth – Vim Superpowers

You just need to master the command ahead , You can use it very comfortably VIM 了 . however , Now? , We introduce you to VIM Killer features . The following functions are only used by me vim Why .

Move the cursor on the current line : 0 ^ $ f F t T , ;
  • 0 → To the head of the line
  • ^ → Not the first one in the line blank character
  • $ → end
  • g_ → The last one in the line is not blank Character position .
  • fa → Go to the next one for a At the character of , You can also fs Go to the next one for s The characters of .
  • t, → To the first character before the comma . Commas can be changed into other characters .
  • 3fa → Find the third occurrence in the current line a.
  • F and T → and f and t equally , It's just in the opposite direction .
    Line moves

Another useful command is dt" → Delete everything , Until the double quotation marks —— ".

Area selection <action>a<object> or <action>i<object>

stay visual In mode , These commands are powerful , The command format is

<action>a<object>`  and  `<action>i<object>
  • action It can be any order , Such as d ( Delete ), y ( Copy ), v ( You can choose according to the mode ).
  • object May be : w A word , W A word separated by spaces , s A word , p A paragraph . It can also be a special character :"、 '、 )、 }、 ].

Suppose you have a string (map (+) ("foo")). And the cursor key is at the first o The location of .

  • vi" → Will choose foo.
  • va" → Will choose "foo".
  • vi) → Will choose "foo".
  • va) → Will choose ("foo").
  • v2i) → Will choose map (+) ("foo")
  • v2a) → Will choose (map (+) ("foo"))

Text objects selection

Block operation : <C-v>

Block operation , Typical operation : 0 <C-v> <C-d> I-- [ESC]

  • ^ → To the head of the line
  • <C-v> → Start block operation
  • <C-d> → Move down the ( You can also use hjkl To move the cursor , Or use %, Or something else )
  • I-- [ESC] → I It's insertion , Insert “--”, Press ESC Key to work for each line .

Rectangular blocks

stay Windows Under the vim, You need to use <C-q> instead of <C-v> ,<C-v> It's the copy clipboard .

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

stay Insert In mode , You can enter the beginning of a word , Then press <C-p> or <C-n>, The auto complement function appears ……

``Completion

Macro recording : qa Operation sequence q, @a, @@
  • qa Record your operations in a register a.
  • therefore @a Meeting replay Recorded macro .
  • @@ It's a shortcut key for replay The latest recorded macro .

* Example *

There is only one line in a row and this line has only “1” In the text of , Type the following command :

  • qaYp<C-a>q
    

    • qa Start recording
    • Yp Copy line .
    • <C-a> increase 1.
    • q Stop recording .
  • @a → stay 1 Write it down below 2

  • @@ → stay 2 Write on the front 3

  • Do it now 100@@ New will be created 100 That's ok , And add the data to 103.

Macros

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

front , We see that <C-v> An example of ( stay Windows It should be ), We can use v and V. Once selected , You can do the following :

  • J → Connect all the lines together ( Become a line )
  • < or > → Indent left and right
  • = → Indent automatically ( Chen haozhu : It's quite powerful , I love )

Autoindent

Add something after all the selected lines :

  • <C-v>
  • Select the relevant line ( You can use j or <C-d> or /pattern or % etc. ……)
  • $ At the end of the line
  • A, Input string , Press ESC.

Append to many lines

Split screen : :split and vsplit.

Here are the main commands , You can use VIM With the help of the :help split. You can refer to a previous article on this site VIM Split screen .

  • :split → Create split screen (:vsplit Create vertical split )
  • <C-w><dir> : dir It's the direction , It can be hjkl or ←↓↑→ One of them , It's used to switch split screens .
  • <C-w>_ ( or <C-w>|) : Maximize size ( | Vertical split screen )
  • <C-w>+ ( or <C-w>-) : Increase the size

Split

Conclusion

  • The above is the most commonly used one 90% The order of .

  • I suggest that you learn... Every day 1 To 2 A new order .

  • In two or three weeks , You will feel vim Powerful .

  • occasionally , Study VIM It's like memorizing something .

  • Fortunately, ,vim There are lots of great tools and excellent documentation .

  • function vimtutor Until you get familiar with the basic commands .

  • What you should read carefully in its online help document is :help usr_02.txt.

  • You'll learn things like !, Catalog , register , Plug ins and many other functions .

Study vim It's like learning to play the piano , Once you learn , benefit .

—————————— End of text ——————————

about vi/vim Just a little comment : You don't need a mouse , No need to use a keypad , It is an editor that can complete many complex functions of text editing just by using the big keyboard . Otherwise ,Visual Studio And there won't be vim Plugin .

版权声明
本文为[RioTian]所创,转载请带上原文链接,感谢