记录自己在使用vim中使用的命令和一些方法。
Basic
i
: Insert text before the cursoro
: Begin a new line below the cursorR
: Replace character continuallyr
: Replace character once
在 vi 的环境中,如何将目前正在编辑的文件另存新档名为 newfilename
:w newfilename
到达指定行 2.1 In normal mode:
line_numberG
例: 到达第九行即9G
2.2 In command mode::line_number
例: 到达第九行即:9
注: 认为第一种方法更为快速,因为如果是第二种方法的话,目光还需要向下移动。到达指定行 ( vim
<leader>
键:\
vim 替换 具体查看vim帮助文档
:h substitute
全局替换::%s/search_string/substituted_string/g
此处:%
: 指范围,相当于1~$
,文档的第一行到最后一行g
: replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line.
vim 替换 vim精确查找 先看一个例子,准确查找字符串int:
/\<int\>
其中
\<
: 匹配单词开头\>
: 匹配单词结尾
拷贝整个文件
:%y ggyG
获得当前文件的名称
# 在屏幕上显示当前名称 :echo expand("%:t") # 粘贴复制当前文件名称 :let @f=expand("%:t") 然后粘贴即可
<esc>
:=Ctrl - [
合并上下行
shift + j
shift + j
可以删除行末尾的回车查找到结果后 (1) 向下查询:
n
(2) 向上查询:shift + n
(联想到网页的tab与shift tab)Visual mode in vim (1)
v
: 选择字符 (1)shift + v
: 选择整行 (1)ctrl + v
: 选择整块gUaW
:大写一个单词Paste local timestamp
"=strftime("%c")<cr>p
"=
: expression registerq:
Opens a history of previous commands.vim宏录制 目的: 减少重复动作. (1) In
normal
mode inputqa
(or qb, qc. ..a, b, c, d is your register) (2) And then you will see “starting . ..” singnal below. (3) Quit this function: in normal mode inputq
, you will end this process. 使用宏: Innormal
mode input@a
,a
is your register name. If you want it to run several times, addnumber
in front of@a
. For example,7@a
.到达行首 In normal mode input
0
.替换实例
删除行首数字
:%s/^[0-9]//g
17.2 删除行尾数字
bash :%s/[0-9]$//g
18. 删除空行
:g/^$/d
如果空行开头有空格,这种方法就不好用了,使用
:g/^\s*$/d
- 在使用vim中的
y
进行复制后,使用p
进行粘贴,再次使用p
就不能粘贴了,想要粘贴还得去复制一次,很繁琐 解决方法: 将使用p
替换成"0p
,这样就可以无限粘贴。 注: 寄存器0
的位置放置的就是最近复制的内容。 - vim
read
:read !command
The way to append the output of an external command.
ctrl + f / ctrl + b
: 快速浏览di(
Delete all content inside ()delete a pairs by using tpope/vim-surround
删除一对括号: ds(
# delete surround
删除一对双引号: ds"
删除一对单引号: ds'
添加一对括号:
普通模式: ysiw(
可视模式: S(
以下是tpope/vim-surround节选
It’s easiest to explain with examples. Press cs"'
inside
"Hello world!"
to change it to
'Hello world!'
Now press cs'<q>
to change it to
<q>Hello world!</q>
To go full circle, press cst"
to get
"Hello world!"
To remove the delimiters entirely, press ds"
.
Hello world!
!$
!
$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串。如:你可能会这样mkdir mydir mv mydir yourdir cd yourdir
可以改成:
mkdir mydir mv !$ yourdir cd !$
在 bash 里,使用 Ctrl-R 而不是上下光标键来查找历史命令。
使用 man ascii 来查看 ASCII 表。
命令行注释命令:
alt + #
vim中查看缓冲区:
:ls
然后使用:buffer num
进行切换vim 书签
创建书签:
m + 字母
ma // 书签名称必须是单个字母,此处示例为a
跳转书签
` + 书签名称
列出所有书签:
:marks
删除书签:
:delmarks name
- 在vim中访问shell
30.1 进入shell:
:shell
or:sh
30.2 离开shell:exit
Plug插件管理器删除插件
- 注释掉或删除在
. vimrc
中的添加的Plug
命令; - 运行
:source ~/vimrc
或重启vim; - 运行
:PlugClean
删除插件。
Vim example
How to convert this piece of text(from vim in reddit)
Arch linux Manjaro Ubuntu
into
1. Arch linux 2. Manjaro 3. Ubuntu
Ways:
(1) Make 3 lines of text out first one (2) use visual-block mode to insert `0.` for 3 lines (3) use visual-block again and `g<Ctrl-a>` to "sequentially" increase your `0`s
正向匹配删除. 反向匹配删除 删除文本中包含 day 的行
day1 day2 our us
:%g/day/d
删除文本中不包含 day 的行
:%v/day/d
Vim fzf
使用fzf模糊查找文件,并使用vim编辑。
vim $(fzf)
附: 我的.vimrc