Vi&Vim

  1. Introduction:

    The Vi editor is the standard editor under all Unix and Linux systems. In any version of Unix and Linux systems, the Vi editor is exactly the same. Vi is also the most basic text editor in Linux.

    Vim is the most used editor in Linux. Unlike Vi, Vim is more advanced and can be regarded as an upgraded version of Vi. Vi is used for text editing, but Vim is more suitable for coding.

    Vim features :

    • People who are used to using Windows may not be used to using Vim;
    • Vim is a powerful full-screen text editor and is the most commonly used text editor Vi on Linux / UNIX;
    • The editor called by default for many commands on Linux is Vim (Linux and emacs and other editors, you can choose your favorite after you are skilled);
    • Can create, edit, and display text files;
    • Vim has no menus, only commands. Commands can only be issued through commands for editing and other operations.
  2. Three modes of Vim:

    • Command line input Vi/Vim+filename(已经存在的文件名或者创建一个新文件), after Vi, enter the command mode by default;
    • After entering the command mode, wait for the correct command to be entered, and i/a/oenter the insert mode like Windows Notepad;
    • After completing the modification, press Escand return to the command mode, enter :to enter the editing mode;
    • After entering the command in the edit mode (that is, after ":"), press Enter to execute it. After the edit mode command is executed, it will automatically return to the command mode;
    • Enter in command mode :wqto save and exit.
    image-20200422211550667
  3. Command mode: When using Vim to edit a file, it is in command mode by default, and any character system you type will be treated as a command. In this mode, the file cannot be edited directly. You can enter shortcut keys to perform some operations (delete line, copy line, move cursor, paste, etc.).

    image-20200422215635689
  4. Input mode: In the command mode state, input i/a/oand other insert commands can enter the input mode. In this mode, Vim can perform a write operation on the file, similar to entering content in a Windows system document.

    image-20200422215758431

  5. Edit mode: Press in the command mode to :enter the edit mode, you can :enter the command after the last line to operate the file (search, replace, save, exit, undo, highlight, etc.), the command will automatically return to the command mode.

    image-20200422215824703

    In any mode, press the Escbutton can return to command mode.

  6. Common Vim commands (entered in command mode):

    • Insert command

      command effect
      i Insert the text entered next to the character under the current cursor, and the text after the cursor moves to the right accordingly
      I Insert the text you enter at the beginning of the line where the cursor is, the first non-blank character of the line
      O Insert a new line below the line under the cursor. The cursor stops at the beginning of a blank line, waiting for text
      O Insert a new line above the line where the cursor is. The cursor stops at the beginning of a blank line, waiting for text
      a Insert the text entered after the character under the current cursor
      A Insert the text you enter at the end of the line where the cursor is
    • Positioning command

      command effect
      : set no Set line number
      :set nonu Cancel line number
      gg To the first line
      G To the last line
      nG To line n
      :n To line n
      $ Move to end of line
      0 Move to the beginning of the line
    • Delete command

      command effect
      x Delete the character at the cursor
      nx Delete n characters after the cursor
      dd Delete the line where the cursor is
      ndd N lines of text after deleting the current line (including this line)
      dG Delete the line from the cursor to the end of the file
      D Delete the content from the cursor to the end of the line
      : n1, n2d Delete content from line a1 to line a2
    • Copy, paste and cut commands

      command effect
      yy Copy current line
      nyy Copy n lines below the current line
      dd Cut the current line
      ndd Cut n lines below the current line
      p、P p is to paste the copied data on the line under the cursor, and P is to paste the line on the cursor
    • Replace and cancel commands

      command effect
      r Replace the character at the cursor
      R Start to replace the isometric characters after the cursor, press Esc to end
      in Cancel the previous step
      :n1,n2s/a1/a2/g Replace all a1 in line n1 to n2 in the file with a2
      :g/a1/a2/g Replace all a1 in the file with a2
      :1,$s/word1/word2/g Find the word1 string from the first line to the last line, and replace the string with word2
      :%s/word1/word2/g Find the word1 string from the first line to the last line, and replace the string with word2
    • Search and search replace commands

      command effect
      /string
      Ignore case when searching for the specified string:set ic
      /^abc Find the line that starts with abc
      /abc$ Find lines ending with abc
      ?abc Find the string abc from the cursor to the main backward
      n Repeat the last search instruction in the same direction
      N Repeat the last search command in the opposite direction
      :%s/old/new/g Full text replacement of specified string
      :n1,n2s/old/new/g Replace the specified string within a certain range

      The string to be searched is strictly case sensitive. If you want to ignore the case, enter the command :set ic; adjust it back to enter :set noic.

      If special symbols appear in the string, you need to add the escape character "\". Common special symbols are \, *,?, $ And so on. If these characters appear, for example, to find the string "10 $", you need to enter "/ 10 $" in the command mode.

    • Save and exit commands

      command effect
      :w Save changes
      :w new_filename Save as specified file
      :wq Save changes and exit
      ZZ Shortcut key, save changes and exit
      :q! Exit without saving changes
      :wq! Save changes and force quit (available for file owner and root)

    vi-vim-cheat-sheet-sch

Guess you like

Origin www.cnblogs.com/yangyu-IoT/p/12757959.html