记录一些vim基础

记录自己在使用vim中使用的命令和一些方法。



VIM

Basic

i: Insert text before the cursor
o: Begin a new line below the cursor
R: Replace character continually
r: Replace character once

1、在 vi 的环境中,如何将目前正在编辑的文件另存新档名为 newfilename

1
:w newfilename

2、到达指定行
2.1 In normal mode: line_numberG
例: 到达第九行即9G
2.2 In command mode: :line_number
例: 到达第九行即:9
: 认为第一种方法更为快速,因为如果是第二种方法的话,目光还需要向下移动。

3、到达指定行
( vim <leader>键: \

4、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.

5、vim 替换
vim精确查找
先看一个例子,准确查找字符串int:

1
/\<int\>

其中

  • \<: 匹配单词开头
  • \>: 匹配单词结尾

6、拷贝整个文件

1
2
:%y
ggyG

7、 获得当前文件的名称

1
2
3
4
5
# 在屏幕上显示当前名称
:echo expand("%:t")
# 粘贴复制当前文件名称
:let @f=expand("%:t")
然后粘贴即可

8、 <esc> == Ctrl - [
9、 合并上下行shift + j
shift + j可以删除行末尾的回车
10、 查找到结果后
(1) 向下查询:n
(2) 向上查询:shift + n(联想到网页的tab与shift tab)

11、 Visual mode in vim
(1) v: 选择字符
(1) shift + v: 选择整行
(1) ctrl + v: 选择整块

12、 gUaW:大写一个单词

13、 Paste local timestamp
"=strftime("%c")<cr>p
"=: expression register

14、 q:
Opens a history of previous commands、

14、 vim宏录制
目的: 减少重复动作、
(1) In normal mode input qa(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 input q, you will end this process、
使用宏:
In normal mode input @a, a is your register name、 If you want it to run several times, add number in front of @a. For example, 7@a.

16、 到达行首
In normal mode input 0

17、 替换实例
17.1 删除行首数字

1
:%s/^[0-9]//g

17.2 删除行尾数字

1
:%s/[0-9]$//g

18、 删除空行

1
:g/^$/d

如果空行开头有空格,这种方法就不好用了,使用

1
:g/^\s*$/d

19、 在使用vim中的y进行复制后,使用p进行粘贴,再次使用p就不能粘贴了,想要粘贴还得去复制一次,很繁琐

解决方法: 将使用p替换成"0p,这样就可以无限粘贴。
注: 寄存器0的位置放置的就是最近复制的内容。

20、 vim read

1
:read !command

The way to append the output of an external command、

21、 ctrl + f / ctrl + b: 快速浏览
22、 di(
Delete all content inside ()

23、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

1
"Hello world!"

to change it to
1
'Hello world!'

Now press cs'<q> to change it to
1
<q>Hello world!</q>

To go full circle, press cst" to get
1
"Hello world!"

To remove the delimiters entirely, press ds"

1
Hello world!

24、 !$
!$是一个特殊的环境变量,它代表了上一个命令的最后一个字符串。如:你可能会这样

1
2
3
$ mkdir mydir
$ mv mydir yourdir
$ cd yourdir

可以改成:

1
2
3
$ mkdir mydir
$ mv !$ yourdir
$ cd !$

25、 在 bash 里,使用 Ctrl-R 而不是上下光标键来查找历史命令。
26、 使用 man ascii 来查看 ASCII 表。
27、 命令行注释命令: alt + #
28、 vim中查看缓冲区: :ls
然后使用:buffer num进行切换

29、 vim 书签
29.1 创建书签: m + 字母

1
ma  // 书签名称必须是单个字母,此处示例为a

29.2 跳转书签

1
` + 书签名称

29.3 列出所有书签: :marks
29.4 删除书签: :delmarks name

30、 在vim中访问shell
30.1 进入shell: :shell or :sh
30.2 离开shell: exit

Plug插件管理器删除插件

1、注释掉或删除在、vimrc中的添加的Plug命令;
2、运行:source ~/vimrc或重启vim;
3、运行:PlugClean删除插件。

Vim example

1、 How to convert this piece of text(from vim in reddit)

1
2
3
Arch linux
Manjaro
Ubuntu

into
1
2
3
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 0s

2、正向匹配删除、反向匹配删除
删除文本中包含 day 的行

1
2
3
4
day1 
day2
our
us
1
:%g/day/d

删除文本中不包含 day 的行

1
:%v/day/d

Vim fzf

使用fzf模糊查找文件,并使用vim编辑。

1
vim $(fzf)

附: 我的.vimrc