Thick books and thin books丨The first part mode of "Vim practical skills"

Chapter 1 Vim's way of solving problems

  1. .The command will repeat the last modification

    • From entering insert mode to returning to normal mode, the operation in between is considered to be a repeatable modification
  2. f{char}: Find the position where the next specified character appears and jump

    • : Repeat search backward
    • ,: Repeat search forward
  3. *: Find the word under the cursor and jump

    • nForward repeat search
    • NReverse repeat search
  4. How to replace a word in the document?

    ① It can be used %s/old/new/g, just press enter

    ② Move the cursor to an old and press *, then press cwto modify the word, and then press it n.n.(compared to ①, it can deal with some positions that may not need to be modified)

The first part of the pattern


Chapter 2 Normal Mode

  1. uCommand will undo the latest modification

    • Entering insert mode to returning to normal mode is considered a modification, so the granularity of undo can be controlled by controlling the use of <Esc>.
    • It is best <Esc>oto start a new line to better control the granularity of undo
    • If you press the arrow keys while inserting, a new undo block will be generated!
    • ⭐️ <C-r>redo
  2. n<C-a>Increase the next number by n; n<C-x>Decrease the next number by n

    • Vim interprets numbers starting with 0 as octal, such as 007 + 001 = 010
    • You can be set set nrformats=so that all the figures are as Vim decimal
  3. zz Command to redraw the screen and display the current line in the center of the screen

  4. ga Display the character code (decimal, hexadecimal and octal) of the current character at the bottom of the screen
    Insert picture description here

  5. Operator + motion command (motion) = operation

  6. gg=G Files in the corresponding language can be automatically indented

  7. Operator pending mode : the state between calling the operator and inputting the action command, press <Esc> to return to the normal mode

  8. REnter replacement mode from normal mode

    • The tab character is represented by a single character in the file, but it occupies the width of several columns on the screen, which is determined by the tabstopsetting
    • In the replacement mode, the tab character is treated as a single character (Vscode's Vim will not). To deal with this situation, you can use the gRcommand to enter the virtual replacement mode
    • rAnd the grcommand allows to replace a character and immediately return to normal mode

Chapter 3 Insert Mode

  1. ⭐️ Shortcut delete method in insert mode (also available in bash shell)

    • <C-h> or Backspace: Delete the previous character
    • <C-w> Delete the previous word
    • <C-u> Delete to the beginning of the line
  2. Insert-normal mode

    • Press to <C-o>enter in insert mode , in this mode, you can execute a normal mode command, after executing it, immediately return to insert mode
    • ⭐️You <C-o>zzcan display the current line in the middle of the window in insert mode
  3. In insert mode, <C-r>{register}you can easily paste a few words without returning to normal mode by pressing p

    • There may be unnecessary line breaks/indents when there are multiple lines of text, you can use it at this time <C-r><C-p>{register}, or return to normal mode and press p
    • For the part of the register, see Part 4
  4. Expression register : allows us to do some operations and insert the result of the operation directly into the document

    • By =indicating the use of the expression register
    • For example <C-r>=1+1<CR>,, 2 is displayed at the cursor position
  5. <C-v>{code} Insert any character with character encoding (3 bits)

    • For example <C-v>065, enter A
    • Use gacommands in normal mode to query character encoding
    • Hexadecimal and digraphs can be queried if they are used

Chapter 4 Visual Mode

  1. 3 different visual modes
    • Operation character text v
    • Line text V
    • Block text C-v
    • ⭐️You gvcan repeat the last highlight selection
  2. The different modes can be switched by the above trigger key, which can achieve very special effects, such as selecting only the first half of each line
  3. Press in visual mode to oswitch the active end of the highlighted selection
  4. The selection area can be used with operators, such as selecting and pressing cto cut the selection area and enter the insert mode
  5. .Commands can be used to repeatedly execute commands in visual mode, but they are only useful when running text
    • For example, to increase the indentation of Vtwo lines of code twice, you can select these two lines first , then use >, and then press .repeat
  6. How to quickly select an area in visual mode:
    • Use various commands to move the cursor to change the selection, such as e, Getc.
    • f{char}Well /and ?repeat queries and the corresponding may also be used
    • Can be quickly selected with a text object, such asvaw
  7. ⭐️ Clever use of visual mode
    • Pressing rto replace in the visual mode is amazing! Can quickly replace horizontally and vertically
    • To modify the words in the same column at the same time, just select and press c. When modifying, only the first line will be updated in real time, and the other lines will display the modified results after returning to normal mode
    • Add the same text at the beginning or end of the selected line, just use the Ior Acommand
      • ⚠️ in visual mode iand ais no longer acting in the normal mode, and will be treated as part of the text objects
  8. Selection mode (some superfluous, basically not used)
    • Use the <Cg> switch to delete the selected text in visual mode and replace it with the entered text, and switch to insert mode at the same time, which is equivalent to pressing one c(then why don’t I just press c?)

Chapter 5 Command Line Mode

  1. Editor family history: ed -> ex ->vi ->vim, Ex commands (commands executed in command line mode) are inherited from the ex line editor, which can easily handle line editing tasks and :h ex-cmd-indexview the complete Ex command list

  2. : Enter command line mode

  3. Advantages of Ex commands: Normal mode commands generally operate on the current character or current line, while Ex commands can be executed at any position, and the ability to execute on multiple lines at the same time (wide range of influence and long distance)

  4. In the command line mode, you can use it <C-h> <C-w> <C-u>to delete quickly, <C-v> <C-k>insert unusual characters, or <C-r>{register}paste some text

  5. ⭐️Specify the [range]range, you can use line number, position mark or search mode to specify the start and end of the range

    range defaults to this line

    • Line number as address::index
      • If it :1will jump to the first line, it :3dwill jump to and delete the third line = 3Gdd
    • Use address as a range::{start},{end}
      • Address can be line number, location mark or search mode
      • Such as :2,5pprinting lines 2 to 5
    • Specify range with highlight selection
      • Press in the visual mode :, the command line will be filled :'<,'>, which '<means the first line of the highlighted area, '>means the last line
      • Input outside the visual mode, the :'<,'>plast selected content will also be echoed
    • Specify range with pattern
      • Such as:/<html>/,/<\/html>/p
    • Correct the address with offset :{address}+n
      • nCan be omitted, the default is 1
      • If :/<html>/+1,/<\/html>/-1pthe row where the first tag of html is located
      • Another example :.,.+3is the current line and the following 3 lines
command Shorthand use
:[range]delete [x] d Delete the line in the specified range [to register x]
:[range]yank [x] and Copy lines in the specified range [to register x]
:[range]put [x] Paste the contents of register x after the specified line
:[range]copy {address} t Copy the lines in the specified range to the line specified by {address}
:[range]move {address} m Move the line in the specified range to the line specified by {address}
:[range]join Concatenate rows in the specified range
:[range]normal {commands} Execute normal mode commands {commands} for each line in the specified range
:[range]substitute/{pattern}/{string}/{flags} s Replace {pattern} in the specified range with {string}
:[range]global/{pattern}/[cmd] Execute Ex command on all lines matching {pattern} in the specified range
  1. Some commonly used symbols:
symbol address
1 The first line of the file
$ The last line of the file
0 Dummy line, located above the first line of the file
. Cursor line
’m Line containing position marker m
’< Highlight the starting line of the selection
'> Highlight the end line of the selection
% The entire file (:1, short form of $)
  1. :tAnd yypdistinction, :twithout the use of registers, the default does not overwrite the contents of the register!

  2. @: Repeat the last Ex command

  3. ⭐️How to perform an operation for a large number of rows at the same time? (And countless times. Say byebye)

    For example, add a semicolon at the end of each line

    • You can execute it on the first line first, select the line that also needs a semicolon in visual mode, press :and enter :'<,'>normal .to achieve a lot of repetition
    • Can also be executed directly :%normal A;

    For example, comment out the entire file :%normal i//

  4. The Ex command can be used for Tabautomatic completion, and can be used to <C-d>display the available completion list. Each time you press Tab, it will be replaced with the next item in the list, and if you press <Shift-Tab>it , it will be replaced in the reverse direction (this one is not very useful after a try, under Windows (The vim press Tab is equivalent to the effect of <Cd> here)

    • There is also a magical effect. Press <Cd> to also display the parameter list, but there must be a space between the command
  5. ⭐️The <C-r><C-w>word under the cursor will be copied and inserted into the command line

    • For example: when replacing globally, use search to find old, then modify an old to new, call on new :%s//<C-r><C-w>/gto replace globally
    • To insert a string, use<C-r><C-a>
  6. ⭐️ Backtracking history command

    • Press <Up><Down> to view historical commands, and at the same time support filtering based on current input, which is also suitable for search mode /!
    • You can also use <Cp><Cn> to replace the up and down keys to avoid moving your hand to the small keyboard area, but it does not support filtering. You can customize the mapping item to completely replace <Cp> with <Up> cnoremap <C-p><Up>
    • set history=You can set the number of saved history records
  7. Use |the command combination Ex: :write|!ruby %, after which you can find this combination of command history

  8. ⭐️Command line window

    • enter
      • In normal mode: Enter q:(I often enter by mistake, I don’t know what it is before...)
      • In command line mode: <C-f>switch to enter, the current half of the input command will be retained
      • q/Open the command line window of the command history of the search mode, the search prompt is just another form of the command line mode
    • Use hjkl to move, carriage return represents the execution of this historical Ex command
    • Almost all operations of vim can be performed on the content in the command line window, including the Ex command itself
    • :qdrop out
  9. ⭐️ Run Shell commands: add a !prefix in command line mode:!{cmd}

    • E.g :!ls

    • %Represents the current file name, for example:!ruby %

    • You can use vim to :shellstart an interactive shell session, and then you can use this shell freely until you enter exit to return to Vim

      • A more convenient way is to press <C-z>to suspend vim first, then you can jobsview it and use the fgcommand to resume foreground operations
    • :read !{cmd}Will redirect the output of cmd to the buffer (the area currently being edited)

      :[range]write !{cmd}The content specified by range will be used as the standard input of cmd, the default is the entire file

      • Such as: :write !shthe role of the command is to execute each line of the command in the current buffer in the shell

      • Note !the position :write ! {cmd}= :write !{cmd}, but ⚠️ :write! shwill force the contents of the buffer to be overwritten in the sh file!

  10. Filter : :[range]!{cmd}Use the content of range as the standard input of cmd, and then replace the range specified by range with the output of cmd-that is, the text specified by range will be filtered by cmd

    • We can !{motion}switch to the command line mode with operators, and preset the range covered by the specified {motion} on the command line
      • For example !G, if you press , Vim will open the command line and fill in :.,$!, so you don’t have to worry about the entire range

Guess you like

Origin blog.csdn.net/weixin_43575658/article/details/113352564