Tutorial: https://docs.oracle.com/cd/E19683-01/806-7612/6jgfmsvq7/index.html

Summary of Basic vi Commands: https://docs.oracle.com/cd/E19683-01/806-7612/6jgfmsvqn/index.html

Vim Reference Guide

https://learnbyexample.github.io/vim_reference/Visual-mode.html

https://vimhelp.org

Cheatsheet: https://vim.rtorr.com, zh-cn

Modes of vi

Command/Normal mode: ESC

Visual mode: for selection

Edit/Entry mode: i


Copy from Vim to Clipboard on Mac

You may be familiar with the yank command: y to copy selected text, yy to copy the current line. It copies stuff to Vim’s internal buffer. I want to conpy in Vim and paste to other apps using cmd-C.

To copy to the system clipboard, we’ll use the same command with a few extras modifier to select the correct register.

  1. Choosing a register In Vim, you choose a register using ".
  2. The system register For both Mac and Windows, you can select the system register by using +.
  3. Putting it all together Enter visual mode by hitting v (or V if you want to select by lines). Select the text you want to copy, if you want to select til the end of line, press $, then type: "+y, it means “copy the selected text into the system register +”.
Keyboard shortcuts Function
Navigation  
h/j/k/l ←/↑/↓/→
o (lowercase o) Open a new line below the current line and enters insert mode
O (uppercase o) Open a new line above the current line and enters insert mode
0 (zero) move the cursor to the start of the line
$ go to the end of the line
^ jump to the first non-blank character of the line
gg beginning of file
G end of file
Move One Word  
w (“word”) to move the cursor to the right one word at a time.
b (“back”) to move the cursor to the left one word at a time.
e (“end”) to move the cursor to the last character of the current word.
Copy and Paste  
dd deletes the current line
yy yanks (copy) the current line
p (Lowercase) paste after cursor
P (Uppercase) paste before cursor
y$ Copy everything from the cursor to the end of the line
d$ Cut everything from the cursor to the end of the line
y^ Copy everything from the start of the line to the cursor.
Deleting Text  
x Delete character under the cursor
3x / x.. Delete three characters
X Delete character to the left of the cursor
daw / dw Delete a word
5dw Delete the next five words
Insert Mode  
i Insert before the cursor
I Insert at the beginning of the line
a Insert (append) after the cursor
A Insert (append) at the end of the line

FAQ

Q: What is visual mode?
A: It’s like selection using keyboard, instead of a cursor. The selected part will be highlighted.

Q: How to enter visual mode?
A: Press v to start from your cursor, V for single-line, and ctrl+V for multiple-line, i.e., visual block mode.

Q: How to cut entire line and paste to another place?
A: Press dd to cut, go to the place where you want to insert, press P (uppercase). The current line will move down by one line.

Q: How to indent a block by 2 spaces?
A: Enter visual mode (⇧V) → select your lines → enter colon (:), '<,'> will auto add, which means for the selected lines → Type norm I<space><space> (norm, capital I, two space)→ hit Enter.

Break down the command:

  • :'<,'>: for the lines I selected
  • norm: Execute the following sequence of keystrokes as if I was in normal mode
  • I: Insert at the beginning of the line the following characters
  • <space>: The character(s) you would like to insert

Another option: use AlignFromCursor plugin.

Tip:

  • When in Visual mode ( ⇧V), select the block , hit (⇧>) will indent the block by 4 spaces.

Delete to the beginning or end of file

  • Delete to the beginning of the file

    :1,.d

    That command can be read as “From line 1 to the current position, delete”, or if you prefer, “Delete from line 1 to the current line position.”

  • Delete to the end of the file

    :.,$d

    That command can be read as “From the current position to the end of file, delete.”

Q: How to Undo and Redo?
A: In command mode,

  • use u (lowercase) or :undo key to Undo
    • Hit u multiple times will redo recursively
  • ctrl+r or :redo to Redo.

Tip:

  • You can undo multiple changes, [number]u. For example, 2u will undo the last 2 changes.

Using a Count to Repeat Commands

Many vi commands can be preceded by a repeat factor (called a count)—a number that precedes the command and tells it how many times to repeat the operation.

For example, 3dd repeats the command to delete a line three times, therefore deleting three lines. 2dw deletes two words, and 4x deletes four characters or spaces.

Using a period (.) to repeat the previous text-changing command.

For example, if you have just deleted a line with dd, you can move the cursor to another line and delete it by simply typing a period.


Block comment

Method 2 – Visual Mode

The other method you can use to comment out multiple lines is to use Visual Mode.

To do this, press ESC and navigate to the lines you want to comment out.

  • Press CTRL + V to enable Visual Mode.

    • Using the up and down arrow key, highlight the lines you wish to comment out.
  • Once you have the lines selected, press the SHIFT + I keys to enter insert mode.
  • Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

How to insert soft line break?

<!--- add two spaces -->
My line of text<space><space>
[Name of link](url)
<!--- or add one blackslash -->
My line of text\
[Name of link](url)

Regular expression

Regex 正则表达式

Short totorial: https://www3.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html

The [...], known as character class (or bracket list), encloses a list of characters. It matches any SINGLE character in the list. In this example, [0-9] matches any SINGLE character between 0 and 9 (i.e., a digit), where dash (-) denotes the range.

Symbol Meaning Example
* the preceding character can occur 0 or more times. meo*w will match mew, meow, meooow, and meoooooooooooow.
[0-9]* matches zero or more digits. It accepts all those in [0-9]+ plus the empty string.
+ the preceding character can occur 1 or more times. meo+w will match meow, meooow, and meoooooooooooow, but not match mew.
[0-9]+ matches one or more digits such as 123, 000.
. any character .* means zero or more of any character.
[A-Za-z] all letters (uppercase and lowercase)  
{m} repetition indicator, meaning the preceding character can repeat exactly m times  
{m,} the preceding character can repeat m or more times  
{m,n} the preceding character can repeat m to n times, inclusive  
^ start-of-line  
$ end-of-line ^[0-9]+$ matches a numeric string.