Tuesday, August 27, 2019

Vim Notes 筆記

Up / Down / Left / Right
h - move left (←) (not high!)
l - move right (→) (not low!)
j - move up ()
k - move down ()

H - move to the top of the screen (High)
L - move to the end of the screen (Low)
Move between windows
Ctrl+W then ↑ or ↓ or ← or → - move cursor to next window
Ctrl+W then H or L           - move vertical left right
Ctrl+W then J or K           - move horizontally up down

Move to last line / first line
:$     - move to last line
:0     - move to the first line
:<num> - move to <number> line

Move in the line
HOME or 0 - move to the beginning of the line
$ or END  - move to the end of the line

Switch between tabs
gt - go to next tab
gT - go to previous tab

Ctrl+PgUp - go to previous tab (will conflict with GUI)
Ctrl+PgDown - go to next tab (will conflict with GUI)

Search ignore case

Search with case

Search Next
/ or n

Search Previous
? or N

Search keyword under cursor and Jump

Jump to variable declaration (a.k.a. go declaration)
gd     #goto declaration

Jump back to previous cursor

Undo / Redo
u / Ctrl+U

Insert new line

Insert or R or r

Reload without saving
:edit! or :e!

Forgot to sudo - gain sudo vim access when you are readonly
:w !sudo tee %

To delete all lines matching a certain regular expression pattern, use

To do the opposite, i.e. only keep those lines matching the pattern, use

And to only temporarily print matching lines, use

Set mouse enable in .vimrc
set mouse=a #enable
set mouse=c #disable

Set display line number
set number

Get current file path
:echo @%
:echo expand('%:p')

Set Syntax Highlights on bash scripts without shebang
:set syn=sh

During typing, you want autocomplete
ctrl-N in insert mode

Copy even if you're using set mouse=a
This is actually simple: by accessing register "+" or "*" in Linux 
  1. select text using any mode
  2. yank the text to register +

Paste even if you're using set mouse=a
This is actually simple: by accessing register "+" or "*" in Linux
  1. go to insert mode
  2. paste the text from register + (during insert mode)
Ctrl-R and then +
or you can do this (during command mode)

Find and Replace (Substitute)
Full command

Find all occurrence of 'foo' and replace it with 'bar'.

Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'.