vim配置及基本用法

奔付山河發表於2024-04-02
capslock -> ctrl // system key change
inoremap jk <Esc> // esc config to jk
let mapleader = "'" // leader to '

Verbs
d: delete
c: change
y: yank (copy)
v: visually select (V for line vs. character)

Modifiers
i: inside
a: around
NUM: number (e.g.: 1, 2, 10)
t: searches for something and stops before it
f: searches for that thing and lands on it
/: find a string (literal or regex)

Nouns
w: word
s: sentence
): sentence (another way of doing it)
p: paragraph
}: paragraph (another way of doing it)
t: tag (think HTML/XML)
b: block (think programming)

Nouns as motion
# Delete two words
d2w

# Change inside sentence (delete the current one and enter insert mode)
cis

# Yank inside paragraph (copy the paragraph you’re in)
yip

# Change to open bracket (change the text from where you are to the next open bracket)
ct<

vi file: open your file in vim
:w: write your changes to the file
:q!: get out of vim (quit), but without saving your changes (!)
:wq: write your changes and exit vim
:saveas ~/some/path/: save your file to that locationvim

nmap zz :wq<cr> // a faster way to do :wq

replace
:{作用範圍}s/{目標字元}/{替換的字元}/{替換標誌}
:%s/first/second/g

A search reference
/{string}: search for string
t: jump up to a character
f: jump onto a character
*: search for other instances of the word under your cursor
n: go to the next instance when you’ve searched for a string
N: go to the previous instance when you’ve searched for a string
;: go to the next instance when you’ve jumped to a character
,: go to the previous instance when you’ve jumped to a character

Basic motions
j: move down one line
k: move up one line
h: move left one character
l: move right one character

Moving within the line
0: move to the beginning of the line
$: move to the end of the line
^: move to the first non-blank character in the line
t": jump to right before the next quotes
f": jump and land on the next quotes
, and ; will repeat the previous t and f jumps.

Moving within the line
0: move to the beginning of the line
$: move to the end of the line
^: move to the first non-blank character in the line
t": jump to right before the next quotes
f": jump and land on the next quotes
, and ; will repeat the previous t and f jumps.

Moving by word
w: move forward one word
b: move back one word
e: move to the end of your wor
W: move forward one big word
B: move back one big word

Moving by sentence or paragraph
): move forward one sentence
}: move forward one paragraph
Moving within the screen
H: move to the top of the screen
M: move to the middle of the screen
L: move to the bottom of the screen
gg: go to the top of the file
G: go to the bottom of the file
^U: move up half a screen
^D: move down half a screen
^F: page down
^B: page up

Jumping back and forth
Ctrl-i: jump to your previous navigation location
Ctrl-o: jump back to where you were

Other motions
:line_number: move to a given line number
^E: scroll up one line
^Y: scroll down one line

在vim中有3中方法可以跳轉到指定行(首先按esc進入命令列模式):
1、ngg/nG (跳轉到檔案第n行,無需回車)
2、:n (跳轉到檔案第n行,需要回車)
3、vim +n filename (在開啟檔案後,跳轉到檔案的第n行

Basic change/insert options
i: insert before the cursor
a: append after the cursor
I: insert at the beginning of the line
A: append at the end of the line
o: open a new line below the current one
O: open a new line above the current one
r: replace the one character under your cursor
R: replace the character under your cursor, but just keep typing afterwards
cm: change whatever you define as a movement, e.g. a word, or a sentence, or a paragraph.
C: change the current line from where you’re at
ct?: change change up to the question mark
s: substitute from where you are to the next command (noun)
S: substitute the entire current line

# Change inside sentence
cis

# Go to the beginning of the line and enter insert mode
I

# Start typing right after the cursor
a

# Delete the line from where you’re at, and enter insert mode
C

# Delete the entire line you’re on, and enter insert mode
S

Basic deletion options
x: exterminate (delete) the character under the cursor
X: exterminate (delete) the character before the cursor
dm: delete whatever you define as a movement, e.g. a word, or a sentence, or a paragraph.
dd: delete the current line
dt.: delete delete from where you are to the period
D: delete to the end of the line
J: join the current line with the next one (delete what’s between)

Undo and Redo
u: undo your last action.
Ctrl-r: redo the last action

Using the “.” to repeat your last action
# delete a word
dw
# delete five more words
5.

Copying text
y: yank (copy) whatever’s selected
yy: yank the current line

A copy and paste reference
y: yank (copy) from where you are to the next command (noun)
yy: a shortcut for copying the current line
p: paste the copied (or deleted) text after the current cursor position
P: paste the copied (or deleted) text before the current cursor position

# Change “foo” to “bar” on every line
:%s /foo/bar/g

# Change “foo” to “bar” on just the current line
:s /foo/bar/g

# Search for the string
/delinquent

# Append some text to the end of the line
A[DO NOT PAY] [Esc]

# Go to the next instance and append the text to the line
n.

Word Text Objects
iw: inside word
aw: around word

# Delete around a word
daw

Sentence Text Objects
is: inside sentence
as: around sentence

# Change inside a sentence
cis

words: iw and aw
sentences: is and as
paragraphs: ip and ap
single quotes: i' and a'
double quotes: i" and a"
back ticks: i` and a`
parenthesis: i( and a(
brackets: i[ and a[
braces: i{ and a{
tags: it and at

Selecting items with visual mode
character-based: v
line-based: V
paragraphs: Ctrl-v

# Select inside of parenthesis
vi(

# Select inside of brackets
vi[

# Select everything inside the second tier braces
v2i{

# Enter visual mode, select two more words of text, and copy them
vwwy

# Enter line-based visual mode and delete a couple of lines below
Vjjd

# Visually select an entire paragraph
vip

# Visually select an entire paragraph then paste it down below
vipyjjp

Using Macros
qa: start recording a macro named “a”
q: stop recording
@a: play back the macro

Wrapping Content
Using the Surround Plugin you can do some seriously epic stuff in terms of wrapping text with markup.
cs"': for the word you’re on, change the surrounding quotes from double to single
cs'<q></q>: do the same, but change the single quotes to <q></q>
ds": delete the double quotes around something
ysiw[: surround the current word with brackets
ysiw<em></em>: emphasize the current word (it works with text objects!) Want to know what’s crazier about that? It’s dot repeatable!.
Visual Mode: select anything, and then type S. You’ll be brought to the bottom of the window. Now type in what you want to wrap that with, such as <a href="/images">, and then press enter.</a>

相關文章