Basic usage of the Vim text editor

The Vim text editor is one of the most commonly used tools in Linux/Unix systems. With this tool, you can easily create, modify, and edit documents or program files. Its function is similar to Notepad or notepad++ under Windows systems. The use of this tool is very necessary to use the linux system. Use this article to record the basic usage of the Vim editor to strengthen your memory or review it later. ( The combination of cat and <<EOF can also realize some functions of the editor, which can generate and edit documents: cat >log.txt <<EOF)

The main references are:

1. "Brother Bird's Linux Private Kitchen"

2. "This is how Linux should be learned"

3. Quick Vim https://learnxinyminutes.com/docs/zh-cn/vim-cn/

4. Rookie Tutorial http://www.runoob.com/linux/linux-vim.html

5. Other Internet information, search engines such as google, baidu, etc.

 

1. Three modes of the Vim editor


Basically vi/vim is divided into three modes, namely command mode (Command mode) , input mode (Insert mode) and bottom line command mode (Last line mode) .
 
(1) Command mode:
The user enters the command mode just after starting vi/vim.
Keystrokes in this state are recognized by Vim as commands, not characters. For example, if we press i at this time, a character will not be entered, and i is regarded as a command.
The following are some commonly used commands:
  • i Switch to input mode to enter characters.
  • x deletes the character at the current cursor position.
  • : Switch to bottom line command mode to enter commands on the bottom line.
  • u Cancel
  • ctrl+r Reply undo
To edit text: Start Vim, enter command mode, press i to switch to input mode.
Command mode only has some basic commands, so you still have to rely on the bottom line command mode to enter more commands.
 
(2) Input mode:
Press i in the command mode to enter the input mode.
In input mode, the following keys are available:
  • Character keys and Shift combinations to enter characters
  • ENTER, carriage return, line feed
  • BACK SPACE, backspace key, delete the character before the cursor
  • DEL, delete key, delete the character after the cursor
  • Arrow keys, move the cursor in the text
  • HOME/END, move the cursor to the beginning/end of the line
  • Page Up/Page Down, page up/down
  • Insert, switch the cursor to input/replace mode, the cursor will become a vertical line/underline
  • ESC, exit input mode, switch to command mode
 
(3) Bottom line command mode:
Press : (English colon) in the command mode to enter the bottom line command mode.
The bottom line command mode can enter single or multi-character commands, and there are many commands available.
In bottom-line command mode, the basic commands are (colons have been omitted):
  • q to quit the program
  • q! Force quit
  • w save file
  • wq filename: save as the specified filename and exit

The bottom line command mode can be exited at any time by pressing the ESC key.
Simply put, we can think of these three modes as the icons at the bottom:

 

2. Basic command operations of Vim


 

Vim Navigation Basics

    vim <filename> # Open <filename> in Vim
    :q # Quit Vim
    : w                # save the current file
    :wq # save the file and exit Vim
    :q !               # Quit Vim without saving the file
                     # ! *forces* :q, so exit Vim without saving
    :x # save the file and exit Vim, shorthand for :wq

    u # undo
    CTRL + R # redo

    h # move left one character
    j # move down one line
    k # move up one line
    l # move one character to the right

    # move within the line

    0                 # move to the beginning of the line
    $ # move to end of line
    ^                 # move to the first non-whitespace character in the line

    # find in text

    / word # all the word after the cursor is highlighted
     ? word # all the word before the cursor is highlighted
    n # After searching, move the cursor to the next occurrence of the word
    N # move the cursor to the previous occurrence of the word

    : %s/foo/bar/g # Change all ' foo ' on each line of the file to ' bar ' 
    :s /foo/bar/g # Change all ' foo ' on the current line to ' bar '

    # jump to character

    f <character> # jump forward to <character>
    t <character> # jump forward to the left of <character>

    # E.g,    
    f < # jump forward to <
    t < # jump forward to the left of <

    # move by word
    # By default a word consists of letters, numbers and underscores

    w                 # move to the next beginning of the word
    b # move to the previous beginning of the word
    e # move to the next word ending


    # other commands to move

    gg # move to top of file
    G# move to end of file
    :NUM # move to line NUM (NUM is any number)
    H # move to top of screen
    M # move to the middle of the screen
    L # move to the end of the screen

model:

Vim is based on the concept of patterns.

Command Mode - Vim starts in this mode for navigation and manipulation commands Insert Mode - For making changes in your files Visual Mode - For highlighting text and manipulating them Ex Mode - For jumping to the bottom enter the command on the ':' prompt line of the

    i # Before the cursor position, switch Vim to insert mode
    a # After the cursor position, switch Vim to insert mode
    v # Switch Vim to visual mode
    : # Switch Vim to ex mode
    <esc>             # Return to command mode no matter what mode you are currently in

    # copy and paste text

    y # copy the selection
    yy # copy the current line
    d # delete selection
    dd                # delete the current line
    p # Paste the copied text after the current cursor position
    P # Paste the copied text before the current cursor position
    x # delete the character at the current cursor position

 

Vim's 'syntax'

Vim can be thought of as a set of commands in a 'verb-modifier-noun' format:

verb - your action modifier - how you perform your action noun - what your action acts on

A few important examples of 'verbs', 'modifiers', and 'nouns':

    # ' verb '

    d # delete
    c# modify
    y # copy
    v # Visual selection

    # ' Modifier '

    i # internal
    a # around
    NUM # number (NUM is any number)
    f # find text and place on it
    t # find text and stop before it
    /                 # Find the string starting
     at the cursor ?                 # Find the string before the cursor

    # ' noun '

    w                 # word
    s # sentence
    p # paragraph
    b # block

   # Example ' statement ' or command

    d2w # delete 2 words
    cis # modify the content of the paragraph
    yip # copy the content of the paragraph (copy the paragraph you are in)
    ct <               # Modify until the opening of the bracket
                     # Modify your current position until the next bracket opening
    d$ # delete until end of line

 

Some shortcuts and tricks

    <!--TODO: Add more!-->

    >                 # Indent selection one level
     <                 # Unindent selection one level
    :earlier 15m # restore the document to the state it was 15 minutes ago
    :later 15m # Reverse the above command
    ddp # Swap adjacent lines, first dd and then p
    . # Repeat previous action

 macro

Macros are basically recordable actions. When you start recording a macro, it records every action and command you use until you stop recording. When the macro is invoked, it applies this exact same sequence of actions and commands again to the selected text.

qa # start recording a macro     called ' a '
    q # stop recording
    @a # replay macro

configure ~/.vimrc

.vimrc can be used to configure Vim at startup.

Here is an example ~/.vimrc file:

" Example~/.vimrc 
" 2015.10

" Requires Vim iMproved version 
set nocompatible

" Detects the file type based on the file name so that you can do things like smart auto-indent. 
filetype indent plugin on

" Enable syntax highlighting 
syntax on

" Better command line completion 
set wildmenu

" except use case-insensitive lookup 
set ignorecase when uppercase letters are used
set smartcase

" When opening a new line, if no file-specific indentation rules are enabled, 
" the indentation remains the same as your current line 
set autoindent

" Display line number set number on the left


" Indentation options, modify according to personal preference

" Number of visible spaces per TAB 
set tabstop= 4

" The number of spaces corresponding to TAB when editing 
set softtabstop= 4

" Number of spaces to indent when using indent operations (>> and <<) 
set shiftwidth= 4

" Convert TAB to spaces 
set expandtab

" Turn on smart tab and space toggles for indentation and alignment 
set smarttab

 

2018-04-27 22:36:47 In Nanjing Xianlin Yadong City

Guess you like

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