What is Vim?

The following sections attempt to describe the basic TypeScript type system for Vim reading. Look at Vim from a TypeScript perspective.

The content is exploring how the Vim tutorial works and is subject to change at any time (this time learning in TypeScript type format to see how it works). This review of TS type system constraints, and to use JS/TS objects to describe. It looks pretty simple. At the same time, as far as possible with pictures, more intuitive

// what is vim?
type Vim = {
    author: string
    from: string;
    brothday: number | Date;
    description: string;
    [index: string] :any;
}

const vim: Vim = {
    author: 'Bram Moolenaar';
    from: 'vi'.brothday: 1991.description: `Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple OS X.`,}Copy the code

Low efficiency in the beginning, high efficiency in the later period

Efficiency is actually proficiency, and VIM shows different efficiency in the hands of different familiar people.

type effective =  'proficiency' & 'think' & 'patience';
Copy the code

The Vim plug-in is not required to start learning

In fact, not vim plug-in vim provided by the function is already very powerful. Once you’re familiar with vim’s key layout, you can explore vim’s more powerful plug-in capabilities.

Vim basics

The basics start with the question: *** Why does Vim have so many modes? * * *

TypeScript types describe the different patterns in 6:

// Three visual modes
type VisualMode = 
    |"CharacterwiseVisualMode" // Character-level visual mode
    |"LinewiseVisualMode" // Row-level visual mode
    |"BlockwiseVisualMode" // Block level visual mode
 
type VimMode = 
    |"normalMode" // Common mode
    | VisualMode // Three visual modes
    |"InsertMode" // Insert mode
    |"CommandLinedMode" // Command line mode
    |"SelectMode"  // Select the mode
    |"ExMode" / / the ex model
    |"ReplaceMode" // Replace mode
    
// If you have a Vim constructor, build a schema
const mode: VimMode = 'InsertMode';
const viminstance = newVim({... options,mode})Copy the code

If you’re already familiar with the different ides and editors, open the project in edit mode. But why not vim?

Vim comes from VI, viM (VI IMproved) is the enhanced version of VI. Vi was created by Bill Joy when he was in college. If you are not familiar with Bill Joy’s bronze beard advice, you may have more fun learning vim.

Vim is 30 years old since its launch in 1991. Think ’91, not even born. Over a long period of time, as developers came and went, editors took on new forms. But only Vim seems to have been around in one form or another. It also seems to be a standard way of editing files.

Common mode

A typical editor /IDE has only one mode: input mode. But in VIM, the first thing to accept is that ViM has more than one mode. Vim uses multiple modes to complete different editing tasks.

  • normalMode (Normal mode: Mormal-Modal)
  • insertMode (input mode insert-modal)
  • bottom-lineMode (bottom-line command mode)
  • visualMode (visual mode: char/line/block-modal)
  • replaceMode (replace mode: replace-modal)

The mode switch

Vim allows quick editing without a mouse

Normal mode -> Insert mode:

type NormalToInsertLevel = 
    | "char"
    | "word"
    | "line"
    | "block"

type NormalToInsertPositoin = 
    | "before"
    | "after"
    
type NormalToInser = NormalToInsertLevel & NormalToInsertPositoin
Copy the code

Switching between normal mode and insert mode is the most common operation in VIM operations. Every operation of VIM is so delicate that I may not be used to such delicate key operation at the beginning, but as I use it more and more, I feel more and more smooth.

  1. Jumps to the specified locationPost-insertion cursor
  2. Deletes a specified character, word, or linePost-insertion cursor

Use cursor as reference point: line/char/before/after

inBefore the character/lineinsert
type InsertBefore = "char" | "line"
Copy the code
  • i: insert before char(current character:beforeInsert cursor)
  • I: insert before linebeforeInsert cursor)
inAfter a character/lineinsert
type InsertAfter =
    | "char"
    | "line"
Copy the code
  • a: insert after charafterInsert cursor)
  • A: insert after lineafterInsert cursor)

Insert row (current rowUp and downLine)

type InsertLinePosition = "up" | "down"
const up: InsertLinePosition = 'O';
const down: InsertLinePostion = 'o';
Copy the code
  • o: insert after lineUnder theInsert blank line)
  • O: insert before lineonInsert blank line)

In the deleteAfter a character/lineinsert

  • s: del char and insert
  • S: del line and insert (delete current row, then enterInsert mode)
    • ddDel a line and not insertDon't enterInsert mode)
  • ccDel line and insert (delete current line and insert cursor at first character)

Normal mode -> Visual mode:

  • vTo:characterFor the unit
  • VTo:lineFor the unit
  • ctrl + vTo:blockFor the unit

Normal mode -> Replace mode

  • r: Character level replacement
  • R: document level replacement
  • gr: character levelVirtual replacementreplace
  • gR: Document levelVirtual replacementreplace

The difference between virtual and non-virtual:

  • <Tab>Key use difference
  • <NL>Line breaks

Insert mode -> Normal mode

  • Esc

Visual mode -> Normal mode

  • Esc

Move the cursor (Normal mode)

type TDirection = "up" | "down" |"left" |"right"

const dircectionOp: TDirection = "up"; 
Copy the code
  • h(Keyboard ⬅️)Normal modetoOn the left.characterThe level of mobile
  • j(Keyboard ⬇️)Normal modetoUnder the.lineThe level of mobile
  • k(Keyboard ⬆️)Normal modetoon.lineThe level of mobile
  • j(Keyboard ⬆️)Normal modetoon.characterThe level of mobile

Basic operation

  • copyy= = =yank
  • pastep= = =paste
  • Modify thec= = =change
  • deleted= = =delete
  • inserti= = =insert
  • additionala= = =append
  • At the ende= = =end

The semantic words of these commands also explain the mode switch commands above. Note that these commands apply not only to normal mode but also to Visual mode.

Common operations in common mode

In addition to the previous command mode and some naming of VIM, the most important thing is that VIM has always operated on object-text. By operating on text objects, VIM’s operations are more abundant.

Number: indicates the number of commands. Command: indicates the specific commands to be executed, such as I, A, and R commands in normal mode. Text object or motion: indicates the text objects to be operated, such as words, sentences, and paragraphs

[number]<command>[text object or motion]
Copy the code
// typescript describes the type of an operation
type opLevel = "char" | "word" | "sentence" | "line" | "block";
const scope_char: opLevel = 'char'; // Character level
const scope_word: opLevel = 'word'; / / the word level
const scope_sentence: opLevel = 'sentence'; // Sentence level
const scope_line: opLevel = 'line'; / / line level
const scope_block: opLevel = 'block'; / / sectors
Copy the code

Move or jump

Move: characters left and right
  • h(Keyboard ⬅️)Normal modetoOn the left.characterThe level of mobile
  • j(Keyboard ⬆️)Normal modetoon.characterThe level of mobile
Move: word left and right
  • lJump to the first character of the next word
const jump_a_char = {
    name: 'l_jump'.c_name: "Jump to the next character".description: "Vim Normal mode jump command, jump to next character".scope: "char".size: 1.command: 'l'.before_vim_mode: "normal_mode".after_vim_mode: "Normal_mode"},Copy the code
  • WJump to the first character of the next word **
  • WJump to the first character of the next word
  • eJump to the first character of the next word
  • EJump to the first character of the next word
  • bJump to the first character of the previous word
  • BJump to the first character of the previous word
Mobile:First/tail
  • Front end of line
const jump_line_first_position = {
    name: 'line_first_jump'.c_name: "Jump to the front of the line".description: "Vim normal jumps to the front of the line, scope:"line", size: unknown, command: 0 | 'shift |', before_vim_mode: "normal_mode", after_vim_mode: "normal_mode", support_in_visual_model: false; }Copy the code

Sometimes we need to jump to the first character of the line.

  • The front end of the indent
const jump_line_indent_first_position = {
    name: 'line_indent_first_jump'.c_name: "Jump to the front of the indent.".description: "Vim normal jumps to the front of the row".scope: "line".size: unknown,
    command: I,
    before_vim_mode: "normal_mode".after_vim_mode: "normal_mode".support_in_visual_model: false;
}
Copy the code

Note: sometimes these two situations overlap and both commands have the same effect

  • Move to the end of the line
const jump_line_last_position = {
    name: 'line_last_jump'.c_name: "Jump to the end of the line".description: "Vim normal jumps to end of line".scope: "line".size: unknown,
    command: $,
    before_vim_mode: "normal_mode".after_vim_mode: "normal_mode".support_in_visual_model: trye;
}
Copy the code

The corresponding command to jump to the end of the line and enter insert mode (explained elsewhere) is A.

Movement: between lines
  • ^Jump to currentlineStart of (related to regular matching)
  • $Jump to currentlineEnd of (related to regular matching)
  • ggJump to firstlineThe beginning of the
  • nggJump to the firstN lineThe beginning of the
  • GJump toThe last lineThe beginning of the
Move: Pair matching

Pairwise matching jumps with %, supporting characters:

  • []
  • (a)
  • {}
  • other

⚠️ Note: <> Pairwise matching jumps are not supported.

Move: Find the same
  • #/nJump toOn aThe same
  • NJump toThe nextThe same
Move: Bottom of screen
  • LJump toAt the bottom of the screen
  • HJump toThe top of the screen
  • MJump toIn the middle of the screen

jump

In the word,wordLevel jump

  • b/BBefore a word
  • w/WThe next word

In a sentence,sentenceLevel jump

  • (The next 🍊
  • )On a 🍊

In the paragraph (paragraph)

  • }Next paragraph
  • {Previous paragraph

In the section (section) jump between

The function jumps to the defined position

Text content search

Vim uses/and? Perform forward and reverse lookup tasks, and the search results are highlighted.

  • Positive:/your_find_content
  • The reverse:/your_find_content

Second, plug-ins can be used for more complex lookup and jump tasks.

Vim text-file operation

Open the file

  • Vim -r

    Restores previously edited files (vim creates swap files)

Text related operations

File coding

  • utf8

The file format

  • unix, dos, mac

file

  • ZZSave the file andexit
  • wSave the fileDon't quit

Vim text all selected

  • ctrl + vThe use of visual
  • ggSkip to the beginning of the first line
  • vEnter Visual mode
  • GJump to the last line
  • Get the full selection function
  • Delete (d)/copy (specified register “*y “)

Undo and Restore

  • ctrl + rUndo last action
  • uRestore last operation

Text annotations

  • Inline-visual downlink inserts different comment characters depending on the language"/ /" or "#".

Annotate a single line

  • shift + vEnter inline Visual mode
  • ^Jump to the beginning of the line
  • escEnter normal mode
  • iTo enter insert mode, enter the comment content “//” or “#”

Use the Vim plug-in for annotation

Vim-commentary provides the comment operator command gc, which allows the following operations

  • gccComment lines
  • <number>gcc
  • gcapComment paragraphs. If functions are separated by empty lines, gCAP comments the entire function where the cursor is.

Character case change

  • gU/UConverts the selected character to uppercase
  • gu/uConverts the selected character to lowercase
  • ~Invert the case of characters

Vim register

Registers and buffers have a lot in common. For example, they both have a list that holds everything:

View register list

  • use:regCommand to view a list of registers, similar to buffer’s:ls/:files.

Access using the register name

“A single double quote indicates the beginning of a reference register. Now it’s time to introduce the type of storage. Different types of registers can be referenced to accomplish different tasks (so fine, not coarse!). .

System-level registers

System registers come in handy when we need viM external content, and we should use system registers more often than not:

type SystemClipboard = {
    name: "systemClipboardReg".c_name: "System Clipboard Register".description: "Interactive access with the system clipboard, with vim Y/P and other commands, operation in VIm.".fromSystemToVim: {
        "command"}}type SystemRegs = "SystemClipboardReg" | "currentBufferReg";
const sReg: SystemRegs = "SystemClipboardReg";
Copy the code
  • "*Current buffer
  • "+System clipboard

The type (or category) of the VIM register

Using TypeScript’s type system:

type VIMRegisters = {
    NumberReg: number;
    InlineDeleteReg: any;
    NameReg: string;
    readony ReadOnlyReg: string;
    // todo
    ExpressionReg: string;
    SelectionAndDropReg: string;
    BlackHole: string
    SearchPatternReg: string;
}
Copy the code

In the computer world, it’s best to call type. Because strongly typed languages are commonly used, typing is self-evident.

  • unnamedThe Unnamed Register
  • digitalThe Numbered Registers
  • Inline deleteRegister (The Small Delete Register)
  • namedRegisters (The Named Registers)
  • read-onlyRegisters (The Read-Only Registers)
  • Chaos in the bufferRegisters (The Read-Only Registers)
  • expressionThe Expression Register
  • Select and dragThe Selection and Drop Registers
  • A black holeThe Black Hole Register
  • Search patternRegister (Search Pattern Register)

The Unnamed Register

Also known as the anonymous register, deleted characters with d, C, x, etc. are stored in the anonymous register, in other words, the last deleted, modified, copied content is stored here, overwriting.

typeTheUnnamedReg = {name:string;
    operate: 'del' | 'change' | 'x';
    descriptions: string;
}
Copy the code

Delete operations in VIm

Delete the character

  • S Deletes the character at which the cursor is currently located, and then inserts the cursor at that position (into insert mode)
const vim_s_del = {
    name: 's_del'.c_name: "Delete character".description: "Current cursor delete, insert cursor, enter insert mode".scope: "character".size: 1.command: 's'.before_vim_mode: "normal_mode".after_vim_mode: "insert_mode",}Copy the code

Delete the word

const vim_s_del = {
    name: 's_del'.c_name: "Delete character".description: "Current cursor delete, insert cursor, enter insert mode".scope: "character".size: 1.command: 's'.before_vim_mode: "normal_mode".after_vim_mode: "insert_mode",}Copy the code

Delete rows

  • ddDeletes the current row without reservation
  • SClear row Deletes current row and keeps row (enters insert mode)

Note: When we use VIm to add, delete, modify and check the text, we should always pay attention to the change of the register of VIM, the data retained in the register can facilitate us to return the deleted content.

Restore after delete:

  • Enter Insert mode, use esc to exit insert mode, and then use u to exit.
  • Without entering insert mode, use the u command to withdraw
  • Using the data saved in the register, refill

Deletes rows in the specified range

  • {a,b}dAbility to delete rows in a specified range

Delete row-level blocks

Delete blocks with vim visual mode, and visual mode command to use

  • shift + v Enter line Visual -line mode
  • Navigate using the HJKL direction and select the desired rows
  • ddExecute the command to delete multiple lines

Because it’s multiple lines, it looks like a row-level block operation.

Delete the piece

Using Vim’s Visual mode, manipulating text editor blocks is very convenient.

Delete the contents inside the pairwise symbol

  • d(a/i)({, <, [,)delete[] {}, < >,, inside or{} and allThe code block

Clear all contents of the file

Register and command behavior

  • What are the register changes after YY copies a line?

  • “0 the 0 of the digit register holds the contents of yy commands

  • “* System register * B stores the contents of YY commands, that is to say, the contents can be copied directly in other parts of the system using YY names.

vim buffers

Understand the buffer

A buffer is a file that has been loaded into memory. All open files correspond to a buffer, and there are buffers that do not correspond to any files. You can think of it as a TAB that opens in the IDE.

To understand and become familiar with buffers, we can create a folder and files to test buffers and buffer-related operations.

cd your_target_dir

mkdir vim_buffers_test && cd vim_buffers_test
touch index.js about.js article.js

vim vim_buffers_test
Copy the code
  • Native ViM audio file directory structure management
  • Use NerdTree to manage directories

Add a buffer

  • baddBuffer add adds buffer.

The sample

# enter directory
cd your_project_dir

vim ./

# Edit in vim
:ls
:badd test.index.js

# vim Input test content
const a = this;

# save file
:wq!

Open nerdTree to see if the file we created is in the current directory
Or push vim to see if ls contains the test.index.js file
Copy the code

Create a split screen for storing different buffers

  • :vspCreate a vertical split screen

Open Open the file picker NERDTree to select different files

Because NEDTREE binding CTRL + D shortcut toggles NERDTree on and off

ctrl + d Switch nerdTree display state
Copy the code

Switch between different buffers

  • ctrl + w

Switch buffers according to the sequence number of buffers

Buffer provides b + num. Switch to the buffer of the specified num (num is :ls/:files/:buffers).

Press to switch position before and after finding buffer

  • bnEquivalent to next of the current buffer
  • bpEquivalent to preview of the current buffer

Final position switch buffers

  • bfirstOpen the first buffer
  • blastOpen the last buffer

Open buffer by filename

  • buffer your_file_name.extOpens the file with the specified file extension

Check the buffers

:buffers Get all buffer numbers and buffer-related information
:ls # similar to the Linux ls command
:files # is another alias for buffers
Copy the code

In other words, there are three commands to check the existing buffer information.

Buffer information

Different types of buffers. In the LS list, in addition to the serial number of buffers, there are also types of buffers.

  • aActive buffer represents the currently active buffer
  • #Alternate buffer Exchange buffer
  • %Current buffer Indicates the current buffer
  • -Readonly Buffer Read-only buffer (with modifiable option disabled)
  • =Readonly buffer Indicates the read-only buffer
  • HHidden Buffer already exists, but is hidden
  • +Modify buffer Indicates the buffer that has been modified
  • xError buffer Reads the buffer that reports an error
  • uUnlisted buffer using!Is displayed in the buffer

More on NerdTree here, nerdTree actually produces a hidden example.

The buffer where 3U A – is located is a hidden buffer. Do not use! Commands are not visible in the buggers list.

Example of Using buffer

Let’s compare the changes in buffer 2 and buffer 4. Figure 1 shows no active buffer because it is in the NerdTree selection phase. The presence of %a in Figure 2 indicates that the current buffer is in the same location as the active buffer

Switch information between buffers

  • Shortcut keys to operate buffer
    • ctrl+^Toggle between the two nearest buffers

Delete the buffer

  • :bdelete filename/num Is deleted
  • :num,num bdelete Indicates the pre-delete

Uninstall the buffer

  • bunload
  • bunload!

The interaction between buffer and window

Vim TAB operation

The most common TAB operation is to open many tabs and keep only the one currently in use: :tabo(just myself, delete everything else). In vscode, we use CTRL +p to switch between different files, and each file generates a TAB.

Vim skills

Vim copies a line up and down, using

  • Line copyyyp
  • Duplicate row copies.
  • Undoes redundant row copiesu

Commented code

Native annotation method

Plug-in annotation

Comments are a common content requirement in code, and we use the plug-in to complete vim comments:

  • Vim – Commentary vim’s gaze
    • Comment the current line: GCC
    • Uncomment: GCC (to switch with duplicate.)

Special comment syntax

  • JSX/TSX includes special annotation syntax. Vscode’s vim plugin already recognizes JSX syntax for annotation using plug-in injection, but in pure vim Chinese it does not recognize whether the code is contained in the JSX structure, which is particularly important.

File processing

Vim handles files very differently than we do in IDES and editors. Previously we learned some basic concepts of Vim and realized that the operation of ViM is much better than other ides in edge-editing text.

Find files

  1. NerdTree plug-inEngineered file manager
  2. FZF plug-inQuick file lookup
  3. ctrlpThe plug-in
type TVimPlugs = "nerdtree" | "fzf" | "ctrlp";

type TFindVimFiles = {
    name: string;
    vimPlugs: TVimPlugs[].
}

const vimFindFiles: TFindVimFiles  = {
    name: 'vim find files'.vimPlugs: ["nerdtree"."fzf"."ctrlp"],}Copy the code

Edit files in split screen

  1. Split screen editing of VIM files (multi-screen ViM writing files is a common requirement)
  2. Tmux + VIm file split-screen tool
type VimSplitScreenSolution = "vim" | "tmuxWithVim";
type VimSplitScreenOpportunity = "open" | "edit";
type VimSplitScreenDiretion = "vs" | "sv";
type VimSplitScreenWithNewFile = "newFileSv"  | "newFileVs"
Copy the code

reference

😸 continues to be updated…

  1. Vim shortcuts of www.cnblogs.com/codehome/p/…