Vim Tutorial

Fundamentals

Vim Basics

Vim means vi improved, which should be installed on Linux or Mac by default. For Windows, recommend to get a subsystem of Linux on Windows system.

First of all, use vim/vi followed by a file path (if not exist, it would be created by vim)

vim demo

Then, you are in the vim interface. One thing you might want to know is that how to quit it. Just type colon followed by q and then, press enter.

:q

However, if you have write something since last change, and you type :q, it would say,

image-20240827152833974

In this situation, type exclamation ! followed :q, to dismiss the change.

:q!

Note: type :q! , not :!q, the latter :! means transfer the other things after :! as a command to the terminal, but there is no command named by q.

image-20240827153247402

Typically, you can't type anything to the file when you open the file at the beginning, because it's in the normal mode, in which every key has its functionality. For example, type : to start to execute a vim command. Type i to get into the insert mode, in which whatever you press doesn't have any functionality, just text. To leave the insert mode to the normal mode, press escape esc.

image-20240827154508201

After you type something in the insert mode, then you want to save and quit. Type : to start the vim command, w for writing the changes to the file, and q for quit.

Another key to open the insert mode is a and o.

  • i for inserting before the cursor
  • a for inserting after the cursor, or appending
  • o for opening a new line below the line

the capital version,

  • I for inserting at the beginning of the line
  • A for inserting at the end of the line or appending the line
  • O for opening a new line above the line

To have some line numbers, type :set number ,

image-20240827155945541

Numbers can be used to combine key bindings. For example, each ation can be repeated x times. Type 5 before the arrow key down, you would move five lines down. Similarly, type 5 before the arrow key right, you would jumps 5 characters to the right.

If you do not want to use the arrow key, that you do not want your finger leave the main typing space, there for you,

  • h for left
  • j for down
  • k for up
  • l for right

With all above, you can set relative number by typing :set relativenumber

image-20240827161307386

the number of current line is the absolute line number, with the relative line number to the cunrrent line for the other lines.

There is the other setting commands, for example,

  • :set mouse=a, to active the mouse
  • :colorscheme , then tab to see the different color schemes and pick one by press enter

However, once you quit the vim and open it again, you'll find that all the settings are gone. So, if you have like 20 settings, you don't want to set them every time you open vim. A setting file or configuration file called the vim rc file is there for you, which should sit on the path of user directory ~/.vimrc
Here are some basic configrations for example,

set number
set relativenumber
set tabstop=4
set shiftwidth=4
set autoindent
set mouse=a
colorscheme slate

Vim Keybindings&Functions

Looking for vim cheat sheet when you want to train you vim or remember them.

  • u, undoing

  • Ctrl + R, redoing

  • and of couse, you can combine the above two keybindings with number to repeat the action x times.

There is the third mode called Visual Mode, which is for selecting. Type v in the normal mode, to active the visual mode. In visual mode, you can select words, and do a couple of things to them, for example cutting, deleting, copy and so on.
image-20240829161912381

  • d, deleting the selecting things (the deleted things are copied, same as cutting)
    • dd, deleting the whole line
      • 5dd, deleting 5 lines
    • D, deleting the rest of the line from the current position of the cursor
  • y, yanking called in vim, which means copies.
    • yy, yanking the whole line
    • Y, the same as yy, doesn't copy the rest of the line
  • p, pasting (combing with number for repeating paste)
    • P, pasting above the current line.
    • If what you yank dosen't contain the newline character, p for pasiting after the cursor, P for pasting before the cursor.
  • c, changing, which means deleting and getting into the insert mode for changing
    • cc, changing the whole line
    • C, changing the rest of the line
  • r, replacing one character (don't need to go in to the insert mode)

Move in an efficient way,

  • w, jumping to the next word (forwarding)

Note, a word means either a space or some special character in between, such as,

Hello I am a simple sentence.
Hello-I-am-a-simple-sentence.
  • W, jumping to the next word, but only use space as the separator.
  • b&B, jumping backword
  • e, jumping to the end of the word
    • E, jumping to the end of the word, but only use space as the separator.
  • 0, jumping to the beginning of the line
  • $, the dollar symbol, jumping to the end of the line

If we combine all above we have mentioned, things are going to be interesting,

  • 2dw or d2w, 2 times delete words or delete 2 words
  • 2db or d2b, delete 2 words backward
  • d$, delete everything until the end
  • d0, delete everything until the beginning

And when you are in a word and you press dw, it won't delete the full word, but just the remaining part of the word.

  • diw, means "delete in a word"
  • ciw, means "change in a word"
  • and so on

When we go into parentheses and brackets and other symbols,

  • ci", change in a quotation marks
  • ci(, change everything in a parentheses

When you are focusing on one, say, curly bracket, the corresponding one is highlighted. You can type % to jump to the place of the corresponding one.

  • d%, delete the whole function body

The other jumping or finding keybindings,

  • t+character, jumping forward to one position before the specified character, in the same line
    • T, jumping backward
  • f+character, jumping forward onto that position of the specified character, in the same line
    • F, jumping backward
  • gg, jumping to the beginning of the file
  • G, jumping to the end of the file
  • 123G or :123, jumping to the absolute line number of 123

Intermediate

Indentation

  • >>, indent to the right
  • <<, indent to the left
  • ==, automatically indent the current line to the correct position
  • > , <, =, indent if you select the line
    • gg=G, to indent the whole file automatically

Visual Mode

Except the normal visual mode, there are visual line mode, and visual block mode.

  • V, visual line mode to select the full line, not part of the line
  • ctrl v, visual block mode to select character by colomn

image-20240830112555123

Searching

  • /, searching the given term forward
    • n, to the next term
    • N, to go back up
  • ?, searching the given term backward
    • n, move up
    • N, move down
  • #, going up to find the next occurrence of the current token
  • *, going down to find the next occurrence of the current token

Waypoint

If there are some sections of the code that you might access them frequently, you can set the waypoint to them.

  • m followed by character, such as wa to set waypoint a to the section
  • 'a, jump to the waypoint a

Substitute

  • :%s/source/target/g, to substitute source to target across the whole file
  • :s/source/target/g, to substitute source to target across the seleted section

Macros and Registers

Type :reg to see the register in vim,

image-20240830140927059

Whatever you copy or something, they have stored in registers, type "6p to take the register with name 6, and paste it. This is how you read from the register, of course you can write to the register by press "6yy to write the line to the register.

The first register without a name is the clipboard of the system in which vim integrated. Type "+ to specify this register.

so,

  • "+, to specify this clipboard
  • "0, to the last one

Macros are also stored in registers. A macros is essentially just a sequence of vim keybindings of vim actions.

**How to store a macro manually? **

Type q to start to record, followed by the specified register name, such as a, and you can see, "recording@a" at the bottom of the screen.

image-20240830144310935

then, whatever you type until press the next q to exit recording would be stored in the register of a.

image-20240830144518015

To execute that macro, press @a.

What interesting is that you can use a macro c to combine the macro a and the macro b.

Other things

Center the current selection

  • zz, center the current line

image-20240830135554597

Repeat the last command

  • ., to repeat the last command

Plugins

to be continued...

Reference

Vim Tutorial for Beginners

posted @ 2024-08-30 15:31  zhihh  阅读(19)  评论(0)    收藏  举报