Concise VIM Tutorial

Original link: https://yousazoe.top/archives/a10f3033.html

introduction

You want to teach yourself vim (the best text editor known to human kind) in the fastest way possible. This is my way of doing it. You start by learning the minimal to survive, then you integrate all the tricks slowly.

Do you want to learn the best text editor VIM in human history at the fastest speed? You have to know how to survive in VIM first, and then learn the tricks bit by bit.

Vim the Six Billion Dollar editor

Better, Stronger, Faster.

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

I suggest the following four steps:

  1. Survive
  2. Feel comfortable
  3. Feel Better, Stronger, Faster
  4. Use superpowers of vim

Before I start learning, I need to give you some warnings:

  • Learning vim is painful at first
  • needs time
  • Requires constant practice, just like you learn an instrument
  • Don’t expect you to make vim more efficient than other editors in 3 days
  • In fact, you need 2 weeks of hard work, not 3 days

1st Level-Survive

  1. install vim
  2. start vim
  3. Do nothing! please read first

When you have an editor installed, you definitely want to type something into it and see what the editor looks like. But vim is not like this, please follow the command below:

  • After starting Vim, vim is in Normal mode
  • Let’s enter Insert mode, press the key i . (Chen Hao’s Note: You will see a word –insert- in the lower left corner of vim, which means that you can enter it in an insert mode)
  • At this point, you can enter text, just like you would with Notepad
  • If you want to return to Normal mode, press ESC

Now, you know how to switch between Insert and Normal modes. Here are some commands that will allow you to survive Normal mode:

  • iInsert mode, press ESC to return to Normal mode
  • x → delete a character under the current cursor
  • :wq → save + exit ( :w save, :q quit) (Chen Hao note: :w can be followed by a file name)
  • dd → delete the current line and save the deleted line to the clipboard
  • p → paste clipboard

Recommended :

  • hjkl (strongly recommended to move the cursor, but not required) → you can also use the cursor keys (←↓↑→). Note: j is like the down arrow.
  • :help <command> → Display help for the associated command. You can also just type :help without the command. (Chen Hao Note: To exit the help, you need to enter :q )

All you need to survive in vim are those 5 commands above, you can edit text, and you must practice these commands into a subconscious state. So you can start to progress to the second level.

When it is, when you get to the second level, you need to say something about Normal mode. In general editors, when you need to copy a piece of text, you need to use the Ctrl key, such as: Ctrl-C . That is to say, the Ctrl key is like a function key. When you press the function key Ctrl , C is no longer C, and it is a command or a shortcut key. In the Normal mode of VIM, all the The keys are function keys. This you need to know.

  • In the text below, if it is Ctrl-λ , I will write it as <C-λ>
  • For commands that start with : you need to type <enter> , for example – if I write :q then you would type :q<enter>

2nd Level – Feel comfortable

Those commands above will only keep you alive, now it’s time to learn some more commands, here are my suggestions: (Chen Hao Note: All commands need to be used in Normal mode, if you don’t know where you are now What kind of mode, you just madly press the ESC key a few times)

  1. Various insert modes
  • a → insert after cursor
  • o → insert a new line after the current line
  • O → Insert a new line before the current line
  • cw → replace characters from the cursor position to the end of a word
  1. simple move cursor
  • 0 → digit zero, to the beginning of the line
  • ^ → to the first position of the line that is not a blank character (the so-called blank character is space, tab, line feed, carriage return, etc.)
  • $ → to end of line
  • g_ → to the last position of the line that is not a blank character
  • /pattern → search for the string of pattern
  1. Copy/Paste (Chen Hao Note: p / P can be used, p means after the current position, P means before the current position)
  • P → paste
  • yy → copy current line to ddP
  1. Undo/Redo
  • u → undo
  • <Cr> → redo
  1. Open/Save/Exit/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 quit ( :x means save only when needed, ZZ doesn’t require a colon and enter)
  • :q! → quit without saving `:qa!” Force quits all files being edited, even if other files have changed
  • :bn and :bp → you can open many files at the same time, use these two commands to switch to the next or previous file (Chen Hao Note: I like to use :n to go to the next file)

Take a moment to familiarize yourself with the above commands, once you get the hang of them, you can do almost anything other editors can do. But by now, you still feel that using vim is a bit clumsy, but that’s okay, you can progress to the third level.

3rd Level – Better. Stronger. Faster.

Congratulations! You did a great job. We can start something more interesting. At level 3, we only talk about commands that are compatible with vi.

Better

Next, let’s see how vim repeats itself:

  1. . → (decimal point) to repeat the last command
  2. N<command> → repeat a command N times

The following is an example, to find a file you can try the following command:

  • 2dd → ​​delete 2 lines
  • 3p → paste text 3 times
  • 100idesu [ESC] → will write “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 desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu “
  • . → Repeat the previous command – 100 “desu “.
  • 3. → Repeat “desu” 3 times (note: not 300, you see how smart VIM is).

Stronger

If you want to make your cursor movement more efficient, you must know the following commands, don’t skip them.

  1. N G → to the Nth line (Chen Hao Note: Note that the G in the command is capitalized, and I generally use :N to the Nth line, such as :137 to 137th line)
  2. gg → go to the first line. (Chen Hao Note: Equivalent to 1G , or :1 )
  3. G → to the last line
  4. Move by word:
    1. w → go to the beginning of the next word
    2. e → to the end of the next word
    • If you think the word is made by default, then use lowercase 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, then you need to use uppercase E and W. (Chen Hao Note: Program Statement)

Next, let me talk about the strongest cursor movement:

  • % : move the matching brackets, including ( , { , [ . (Chen Hao Note: You need to move the cursor to the brackets first)
  • * and # : match the word the cursor is currently on, move the cursor to the next (or previous) matching word (* is the next, # is the previous)

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

Faster

You must remember the cursor movement, because many commands can be linked to these commands to move the cursor. Many commands can be done as follows:

<start position><command><end position>

For example 0y$ command means:

  • 0 → first to the beginning of the line
  • y → copy from here
  • $ → copy to the last character of the line

You can type ye to copy from the current position to the last character of the word.

You can also type y2/foo to copy the string between 2 “foo”s.

There is still a lot of time that you don’t have to press y to copy, and the following commands will also be copied:

  • d (delete)
  • v (visualization option)
  • gU (uppercase)
  • gu (lowercase)
  • and many more
    (Chen Hao Note: Visual selection is a very interesting command, you can press v first, then move the cursor, you will see the text is selected, then you may d, or y, or change to uppercase, etc.)

4th Level – Vim Superpowers

You just need to master the previous commands and you can use VIM comfortably. But, for now, we’re introducing you to the killer feature of VIM. The following functions are the reasons why I only use vim.

Move on current line: 0 ^ $ g_ f F t T , ;

  • 0 → to the beginning of the line
  • ^ → to the first non-blank character on the line
  • $ → to end of line
  • g_ → to the last position of the line that is not a blank character
  • fa → go to the next character that is a, you can also fs to the next character that is s
  • t, → to the first character before the comma. Commas can be changed to other characters
  • 3fa → find the third occurrence of a in the current line
  • F and T → same as f and t , but in opposite directions

Another useful command is dt" → delete everything until a double quote is encountered – " .

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

In visual mode, these commands are very powerful, the command format is

<action>a<object> and <action>i<object>

  • action can be any command, such as d (delete), y (copy), v (can be selected depending on the mode)
  • object may be: w a word, W a space-separated word, s a sentence, p a paragraph. Can also be a special character: " , ' , ) , } , ] .

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

  • vi" → will select foo.
  • va" → will select “foo”.
  • vi) → will select “foo”.
  • va) → will select(“foo”).
  • v2i) → will select map (+) (“foo”)
  • v2a) → will select (map (+) (“foo”))

Select rectangular blocks: <Cv> .

Block operation, typical operation: 0 <Cv> <Cd> I-- [ESC]

  • ^ → to the beginning of the line
  • <Cv> → start block operation
  • <Cd> → move down (you can also use hjkl to move the cursor, or use %, or something else)
  • I-- [ESC]I is insert, insert “-“, press ESC key to take effect for each line.

In vim under Windows, you need to use <Cq> instead of <Cv> , <Cv> is to copy the clipboard.

Completion: <Cn> and <Cp> .

In Insert mode, you can enter the beginning of a word, then press <Cp> or <Cn> , and the auto-completion function will appear…

Macros : qa do something q , @a , @@

  • qa records your operation in register a
  • So @a will replay the recorded macro
  • @@ is a shortcut key to replay the latest recorded macro.

Example

In a text with only one line and only “1” on this line, type the following command:

  • qaYp<Ca>q
    • qa start recording
    • Yp copy line
    • <Ca> increases by 1
    • q stop recording
  • @a → write 2 under 1
  • @@ → Write 3 in front of 2
  • Now doing 100@@ will create new 100 rows and increase the data to 103.

Visual selection: v , V , <Cv>

Earlier, we saw an example of <Cv> (which should be <Cq> under Windows), and we can use v and V Once selected, you can do the following:

  • J → join all lines (into one line)
  • < or > → indent left and right
  • = → Automatic indentation (Chen Hao Note: This function is quite powerful, I like it very much)

Add something after all selected lines:

  • <Cv>
  • Select the relevant line (you can use j or <Cd> or /pattern or % , etc…)
  • $ to end of line
  • A , enter a string, press ESC

Splits: :split and vsplit .

Here are the main commands, you can use VIM’s help :help split .

  • :split → create split screen ( :vsplit creates vertical split screen)
  • <Cw><dir> : dir is the direction, which can be one of hjkl or ←↓↑→, which is used to switch the split screen.
  • <Cw>_ (or <Cw>| ) : maximize size ( <Cw>| vertical split screen)
  • <Cw>+ (or <Cw>- ) : increase size

Conclusion

The above are 90% of the most commonly used commands by the author, I suggest you learn 1 or 2 new commands every day. After two to three weeks, you will feel vim powerful.

Sometimes learning VIM is like reciting something. Fortunately, vim has a lot of great tools and excellent documentation. Run vimtutor until you are familiar with the basic commands, the online help file you should read carefully is :help usr_02.txt .

You’ll learn about ! , directories, registers, plugins, and many more. Learning vim is like learning to play the piano. Once you learn it, you will benefit greatly.

If you liked this article, there is a follow up: Vim as IDE

This article is reprinted from: https://yousazoe.top/archives/a10f3033.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment