4. vim operation

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

I suggest the following four steps:

  1. survive
  2. feel good
  3. feel better, stronger, faster
  4. Using VIM's Superpowers

When you finish this article, you will be a vim superstar.

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

  • Learning vim is painful in the beginning.
  • needs time
  • It takes constant practice, just like you learn an instrument.
  • Don't expect you to be able to make vim more efficient than other editors in 3 days.
  • In fact, you need 2 weeks of hard work, not 3 days.

Level 1 - 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 the ESC key .

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

  • i →  Insert  mode, press  ESC to return to  Normal  mode.
  • x → Delete a character at the current cursor position.
  • :wq → save + exit ( :w save,  :q exit) (Chen Hao's note: :w can be followed by the file name)
  • dd → delete the current line and save the deleted line to the clipboard
  • p → Paste clipboard

recommend:

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

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  Ctrl keys, 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.

mark:

  • In the text below, if it were,  Ctrl-λI would write it as  <C-λ>.
  • Commands that  : start with you need to enter a  <enter>carriage return, eg — if I write it  :q that means you enter  :q<enter>.

Level 2 – feel good

Those commands above can 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

simple move cursor

  • 0 → digit zero, to the beginning of the line
  • ^ → Go 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 the end of the current line
  • g_ → Go to the last position of the line that is not a blank character.
  • /pattern →  pattern The searched string (Chen Hao Note: If you find multiple matches, you can press the n key to go to the next one)
  1. Copy/Paste (Note: p/P can be used, p means after the current position, P means before the current position)

    • P → paste
    • yy → Copy the current line at ddP
  2. Undo/Redo


    • u → undo
    • <C-r> → redo
  3. Open/Save/Exit/Change File (Buffer)

    • :e <path/to/file> → open a file
    • :w → save
    • :saveas <path/to/file> → Save as <path/to/file>
    • :xZZ or  :wq → save and exit ( :x meaning to save only when needed, ZZ do not need to enter a colon and enter)
    • :q! → Exit without saving  :qa! Force quits all files being edited, even if other files have changed.
    • :bn and  :bp → You can have many files open at the same time, use these two commands to switch to the next or previous file. (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 vim is a bit clumsy, but that's okay, you can progress to the third level.

Level 3 - 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 the text 3 times
  • 100idesu [ESC] → Under the photo of the meeting “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 and don't skip them.

  1. N G → to the Nth line (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 → to the first line. (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 → Go to the end of the next word.

    > Use lowercase e and w if you think the word is by default. 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)

    Word moves example

Now, 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 where the cursor is currently, 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$ the command means:

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

You can type  ye, 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 (capitalized)
  • gu (lowercase)
  • etc
(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, you can also capitalize, etc.)

Level 4 – 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 the cursor on the current line: 0 ^ $ 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_ → Go to the last position of the line that is not a blank character.
  • fa → to the next character a, you can also fs to the next character s.
  • t, → to the first character before the comma. Commas can become 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.
    Line moves

Another useful command is  dt" → to delete everything until double quotes are encountered - "。

region 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. It can also be a special character:"、 '、 )、 }、 ]。

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

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

Text objects selection

Block operation: <C-v>

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

  • ^ → to the beginning of the line
  • <C-v> → Start block operation
  • <C-d> → 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.

Rectangular blocks

In vim under Windows, you need to use  <C-q> instead  <C-v> , <C-v> copy the clipboard.

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

In Insert mode, you can enter the beginning of a word, then press <C-p>或是<C-n>,自动补齐功能就出现了……

Completion

Macro Recording:  qa Action Sequence  q@a@@

  • qa record your operations in registers a。
  • The recorded macro will then  @a be replayed.
  • @@ 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<C-a>q@a → Write 2 under 1
    • qa start recording
    • Yp Copy row.
    • <C-a> Add 1.
    • q Stop recording.
  • @@ → Write 3 in front of 2
  • Doing now  100@@ will create new 100 rows and increase the data to 103.

Macros

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

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

  • J → concatenate 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)

Autoindent

Add something after all selected lines:

  • <C-v>
  • Select the relevant row (use  j or  <C-d> or  /pattern or  % etc...)
  • $ to the end of the line
  • A, enter a string, press ESC。

Append to many lines

Split screen:  :split and  vsplit.

Below are the main commands you can use with the help of VIM  :help split

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

Split

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324938715&siteId=291194637