普通模式
复制
y
dd
gU
gu
x
X
缩进
>
, <
或 =
4==
indent current line and next 3=ap
indent around paragraph=%
indent to end of method
重复操作
.
小数点可以重复上一次的命令N<command>
重复某个命令 N 次
移动
%
匹配括号移动,包括[
{
(
,需要将光标移动到括号上。#
,*
匹配光标所在单词,*
下一个,#
上一个0
行头^
本行的第一个非空白字符$
到行尾g_
到本行最后一个非空白字符fa
向前移动到查找的第一个 a 字符t,
移动到逗号前的第一个字符3fa
当前行查找第三个出现的字母 adt"
删除查找到的第一个双引号之前的所有的内容w
下一个单词到开头e
下一个单词的结尾
单词默认由字母、数字和下划线组成(程序变量),而使用大写 W
、E
的移动方式,单词由空白字符分隔。
action + text object
action
d
deletey
yankv
visualc
delete and inserts
same ascl
S
same ascc
text object
w
words
sentencep
paragraph'
"
}
]
)
specific characters
字符串(map (+) (“foo”)),光标在第一个 o 的位置
vi"
select foova"
select “foo”vi)
select “foo”va)
select (“foo”)v2i)
select map (+) (“foo”)v2a)
select (map (+) (“foo”)
Scrolling relative to cursor
:help scrolling
zt
- reposition viewport so your current line is at the topzz
- middlezb
- bottom<c-y>
<c-e>
<c-u>
<c-d>
<c-f>
<c-b>
缓存
当使用 vim 编辑器时,vim 会在被编辑的文件目录下,建立一个名为 .filename.swap
的文件
buffers
Summary:
- A buffer is the in-memory text of a file.
- A window is a viewport on a buffer.
- A tab page is a collection of windows.
:ls
查看缓冲区被打开的文件,%a 表示当前文件,相关标记如下:-
非活动的缓冲区a
当前被激活缓冲区h
隐藏的缓冲区%
当前的缓冲区#
交换缓冲区=
只读缓冲区+
已经更改的缓冲区:buffer
[数字编号] 切换文件
同 buffer
:n
编辑下一个文件:N
编辑上一个文件:files
列出目前这个 vim 开启的所有文件,同:ls
:n
and:N
doesn’t switch between buffers
不同 buffers
:bn
编辑下一个文件(:bnext
):bp
编辑上一个文件(:bprevious
):blast
缩写:bl
:bfirst
缩写:bf
删除 buffer
:%bd
- delete all your buffers:bd
or:.bd
delete the current buffer:.,+2bd
delete the current buffer and 2 buffers after it (relative to their location in the buffer list)
多窗口
:sp {filename}
划分窗口,并打开指定文件:Hexplorer
缩写:He
(在下边分屏浏览目录):He!
(在上屏浏览文件目录):Vexplorer
缩写:Ve
(在左边分屏间浏览目录,右边为:Ve!
):Texplorer
缩写:Te
使用类似于浏览器 tab 标签的形式打开文件
窗口间移动
<crtl-w>j
光标移动到下方的窗口<ctrl-w>k
光标移动到上方的窗口<ctrl-w>q
退出窗口<ctrl-w>t
窗口在新标签页中打开
标签页间移动
在普通模式下,使用快捷键 <ctrl-w>t
,可以将一个 buffer 文件在另一个标签页中打开。
gt
- 下一个标签gT
- 前一个标签{i}gt
-i
数字,到指定标签页,比如 5gt 就是到第5个标签页:tabs
查看打开的窗口和 Tab 标签的情况:tabn
go to next tab:tabp
go to previous tab:tabfirst
go to first tab:tablast
go to last tab:tabclose[i]
指定标签数字,可以关闭指定标签页:bufdo tab split
把 buffer 中的文件全部转成 tabvim -p file1 file2
shell 命令行中以 tab 标签页的形式打开多个文件
会话 session
使用命令 :mksession ~/.mysession.vim
( :mks
is ok) 保存已经打开的多窗口文件。 如果文件重复,vim 默认会报错,使用 :mksession! ~/.mysession.vim
代替。打开保存的会话
vim -S ~/.mysession.vim
Quickfix
what a quick fix list is effectively what a quick fix list is a series of entries in which point to a specific file and location.
A lot of times they’re associated with errors links or search results.
- quickfix
- location-list
quickfix
:cw
错误信息分屏显示:cp
跳到上一个错误:cl
列出所有错误:cc
显示错误详细信息:cdo
location-list
:ldo
Power of g
:h g
gQ
g0
g$
g <ctrl-g>
g#
g&
g-
g+
g??
gI
gU
gt
gT
g_
gf
gd
gg
The global command :g
is very useful - multiple repeats
:[range]g[lobal]/{pattern}/[cmd]
Example:
:normal!
!
:g/pattern/d
– Remove lines matching pattern:3,4g/pattern/d
– Remove lines matching pattern between 3 and 4 line:g/pattern/y A
- Yank all lines matching ‘pattern’ into the register.:g!/pattern/d
– Remove lines that do NOT match the pattern:v/pattern/d
– Also removes lines that do not match the pattern:cdo g/function/norm! ciw func<cr>
Replace
:%s/https\?.*/[&](&)/g
%
– set the range to the entire files
– substitution/https\?.*/
– regex to matchhttp
orhttps
and anything else after it[&](&)
– The&
is the magic here and inserts the matched text. In this case, the URL. The rest of the characters are interpreted literally, giving us the linked URL in the resulting markdown./g
changes all the matches on a line- use any regex delimiter in your pattern substitution. No need to use
/
at all, try#
instead::s#/usr/local/bin#/usr/sbin#g
to avoid escaping slashes. - Limit a search and replace operation between lines matching 2 regex patterns using
/pattern1/,/pattern2/s/search/replace/
Search and replace | Vim Tips Wiki | Fandom
Repeat the last substitution
Developing efficient workflows in Vim is all about repetition, first and foremost by using .
to repeat the last command. But Vim can also repeat your last substitution. Here’s a few options with subtle differences:
:&
– Repeats last substitution but resets the flags. Also works with just :s.:&&
– Repeat last substitution with the same flags.:%&
– Repeat last substitution on entire file, reset flags.:%&&
– Repeat last substitution on entire file, keep flags.
:%s/something/newthing/g
:%&g
Search
- Append
/e
to the end of a search to place the cursor at the end of the next match. I.e/search phrase/e
- Stay in search mode
/some-pattern<C-g>
or/some-pattern<C-t>
vim 的环境配置和记录
.viminfo
主动记录你曾经做过的操作记录,以便你下次可以轻松地作业。
环境配置
- 在一般模式下输入
:set all
查看所有的参数设置值。 :set
显示与系统默认值不同的设置参数,一般来说就是自定变动过的设置参数。:syntax on
打开语法高亮
DOS 与 Linux 的换行符
字符转换命令
dos2unix [-kn] file [newfile]
unix2dos [-kn] file [newfile]
registers
To find out what’s stored in each register, simply use Vim’s :registers
command, or :reg
for short.
Marks
Use m{capital letter}
to make a global mark. Close and reopen VIm, and press '{capital letter}
to open the file w/ the global mark.
args
:args /path/*
:wn
https://vimtricks.com/p/vimtrick-edit-files-sequentially/ - Edit files sequentially
Tips
Copying and pasting lines
-
The slow way is to navigate to the line I want, yank it, go back and paste it.
-
The most efficient way I can think to do that is to jump by searching with
/
and pressing<CR>
. Then yank the line withyy
. Then use the jump list,<ctrl-o>
, to bounce back. And pressp
to paste the line below orP
to paste the line above. -
Use the ex
:yank
command:<line number>yank
– copies the line number specified to your default register. -
Use the ex
:copy
command:<line number>copy.
– copies the line number specified and pastes it to the line below -
The ex
:copy
command has a short versiont
:281t.
– Copy line 281 and paste it below the current line:-10t.
– Copy the line 10 lines above the current line and paste it below the current line:+8t.
– Copy the line 8 lines after the current line and paste it below:10,20t.
– Copy lines 10 to 20 and paste them below:t20
– Copy the current line and paste it below line 20
-
Pasting into Vim from @StackOverflow? Avoid indent fail by using set
:paste
or use the system clipboard with"*p
The shortcut keyboards in insert mode
<c-w>
- delete a word<c-x><C-f>
- autocomplete filenames in vim.<c-n>
- 自动提示<c-p>
- 自动提示<c-r>=
- From insert mode, enters Vim’s expression register<c-f>
- switch from Command-Line mode to the command-line window. Or During the/
portion of a search, open a search history window.<c-a>
or<C-x>
- increment or decrement hex, binary, and octal numbers in normal mode.<c-k>
+ 2 letters - add special characters in insert mode. Examples:<c-k>oo
• bullet<c-k>Db
◆ diamond bullet<c-k>Pd
£ pound<c-k>Eu
€ euro<c-k>-N
– en dash<c-k>-M
— em
The undo tree usr_32.txt
- If you make changes, undo, then make a different change, then undo, then make a different change, you create undo branches.
- To view the change tree, run
:undolist
, to navigate the undo branches, useg-
,g+
- Go back to an earlier text state with the
:earlier
command. - Go to newer text state with the
:later
command. This command accepts the following time units:s
(seconds),h
(hours),d
(days), andf
(number of saves).
Change the case of characters with ~
, u
and U
.
gUw
- Uppercase to end of wordgUiw
- Uppercase entire wordguap
- Lowercase paragraph
Sort lines in Vim:
sort-motion plugin - The primary interface to this plugin is via the gs
mapping, for sorting based on a text object or motion.
Examples:
gs2j
=> Sort down two lines (current + 2 below)gsip
=> Sort the current paragraphgsii
=> Sort the current indentation level (requires text-obj-indent plugin)gsi(
=> Sort within parenthesis.(b, c, a)
would become(a, b, c)
Vim has some built in options:
:sort
- sort all lines:sort!
- sort in reverse:sort u
- remove dupes and sort:sort i
- ignore case:sort n
- sort numerically
There are ways to sort elements of a single line in vanilla vim as well, as detailed in this StackExchange response, but they will involve some regex.
Jump between changes
:changes
- Show list of changesg;
- Jump to previousg,
- Jump to next
:norm
The command allows you to execute normal mode operations from the command line. By combining with %
, we can run a sequence of operations on an entire file.
:%norm
- Run a normal mode command on the entire file.
Examples:
ysiw
- surround plugin:%norm ysiw"A: ""
`
character
`[
- Navigate to the beginning of your most recently yanked or changed text`]
- Navigate to the end of your most recently yanked or changed text
Others
-
You can edit your visual selection by using
o
to bounce your cursor to the opposite end of the selection. Adjust the top bound as needed, then presso
to return to the bottom. -
Use
gv
in Vim to reselect the last visual selection -
filename-modifiers。关于Unix:在Vim中打开与当前文件相同的文件夹中的文件
:p
Make file name a full path,:h expand
,:wildcards
-
Use
ea
to append to the end of the current word. -
Swap two characters in Vim with
xp
-
Put from the
"%
register to put the name of the current file. -
To make it easier to navigate conflict markers, you could tweak the built-in matchit plugin: stackoverflow.com/a/71676129/853…
-
Multiple cursor support
-
Open the quickfix window with
:cwin
and see the results. Next we do another search. To get back to our previous, older quickfix window, we use:colder
. Then, we can return to the most recent search results with:cnewer
.
Ex Command-line
:set noignorecase
- make searches case sensitive (the default):set hlsearch
- highlight the remaining matches with the search highlight group.:set splitright
- open splits in a right direction.:set splitbelow
- open splits in a below direction.:set laststatus=3
- show only one activeted single status bar:edit!
- revert (go back or return to) all changes to the current buffer.:1,5j
- Use the ex commandj
to join some lines on a range. Use visual selection, and J to join or ‘gJ’ to join without spacing- Use
%:h
to get the path part of the current file. i.e.,cd %:h
to set the working directory to the directory of the current file. - Use
:reg
to view the contents of all registers, or:reg{register}
to view the contents of one. :Man
command - Open that command’s man page:dig
- open a list of all digraphs available on your system (complication options can affect the list):reg
- open the vim registers- Toggle Vim boolean options
- adding a bang
!
at the end.:set number!
,:set cusorcolumn!
:set number
will turn the feature on:set nonumber
will turn the feature off
- adding a bang
- Check the current state of any option by adding a question mark
?
to the end::set number?
will return eithernumber
ornonumber
depending on if the option is on or off. - Repeat the history command
- Enter command mode
:
and then press<C-p>
to cycle back through your history, finding a command and invoking it again. - if the command was the last colon command you ran, simply press
@:
to repeat the last command.j
- Enter command mode
- Delete using the
"_d{motion}
command to delete without overwriting your default register. - Non-printable Characters
^M
is a single character inserted by usingCTRL-v
followed byCTRL-m
. UTF-8 C1 Controls and Latin1 Supplement
Helps
:help wildoptions
- command-line completion allows fuzzy-matching in some cases:help registers
-"0p
will paste from the0
register, which automatically contains the last yanked text.:help global
:help no-greedy
- because.*
is greedy. It matches the maximum amount of text it can..\{-}
will match the fewest characters possible to make a match.:help jumplist
- jumps are cursor movements stored in a list called the jumplist. movements which modify the jump list are:/pattern
searches and?pattern
searches (forward and backward pattern matching)*
and#
(forward and backward search for the word under the cursor.%
(jump to a matching enclosing character like paren, brace, bracket, etc)- Any inter-file navigation like
gf
:help scroll-cursor
:help option-list
- all vim options:help usr<tab>
:help buffer
:help window
:help <tab>
:help split
:help motion
:help options
:help <tabcomplate or ctrl-d>
:help <specific option name>
:help quickfix
:help macros
:help motions
:help substitude
References
- Thoughts on coc.nvim
- Vim中文帮助文档
- vim-fundamentals
- Vim documentation
- jdhao blog about neovim
- amikai blog about vim
Learning Vim
:h usr<tab>
:h buffer
:h window
:h <tab>
:h split
:h motion
:h options
:h <tabcomplate or ctrl-d>
:h <specific option name>
:h quickfix
:h macros
:h registers
:h motions
:h substitude
:g/^$/d
- 删除空行:g/^\s*$/d
- 删除只有空格的行