关于vim(主要用的还是用的nvim)的Ex命令其实算是比较容易被忽略的一个东西

这些东西有时候堪称魔法一般的东,虽说在当时行编辑器的时候这些命令更像是活命的神技

通常情况下来说,ex命令大概由以下组成

1
:[range] <command> [args]

表示在range上执行具有args的命令command

关于vim中的神秘符文

vim/nvim 中很容易出现一些令人看不懂的符号,在这里的话列出一部分(混个眼熟)

关于一些范围的表示

比较常见的就是在visual模式下的两个:'‘<'>分别代表区间的开始和结束 大部分情况下,这个东西都是在visual模式下触发才注意到的

实际上,这个ex命令的范围可以是:

  • 某一行:1(指定行号),0(文件开头),$末行,’a(某个标记,大写的话是跨文件)
  • 一个范围,通常是由逗号分割开的两个指定的行号,也可以是%代指从头到尾
  • 搜索的结果:由\\(向下搜索)或者??(向上搜索)指定,遵循vim拓展的正则语法(大概还是正则), 具体的放到了底下说 除此之外也可以使用+-指定偏移的范围

其他的一些符号

这里列举一些可能会有用的符号(说不定哪里用上了)

  • !:放开头是执行命令,如果和命令结合通常代表强制写入,作为子参数也可以说把内容传入给它然后执行, 也可以对选中范围进行过滤(奇怪的知识点)
  • >:重定向输入
  • >>:追加重定向
  • <:重定向输出 后三个其实比较好理解,和bash里面用的其实差不多,很多时候是为了筛选和分离什么的(什么vim神技巧)

关于常见的ex命令

关于这些ex命令,很多都是从ed,ex这一路子传下来的,很多命令一个命令就有很多作用,通常来说这些 命令的help文档相当。。。。。

Eg. Writing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

==============================================================================
4. Writing *writing* *save-file*

Note: When the 'write' option is off, you are not able to write any file.

*:w* *:write*
*E502* *E503* *E504* *E505*
*E512* *E514* *E667* *E949*
:w[rite] [++opt] Write the whole buffer to the current file. This is
the normal way to save changes to a file. Fails when
'readonly' is set or when there is another reason why
the file can't be written, such as when the parent
directory doesn't exist (use |++p| to avoid that).
For ++opt see |++opt|, but only ++p, ++bin, ++nobin,
++ff and ++enc are effective.


:w[rite]! [++opt] Like ":write", but forcefully write when 'readonly' is
set or there is another reason why writing was
refused.
Note: This may change the permission and ownership of
the file and break (symbolic) links. Add the 'W' flag
to 'cpoptions' to avoid this.

:[range]w[rite][!] [++opt]
Write the specified lines to the current file. This
is unusual, because the file will not contain all
lines in the buffer.

*:w_f* *:write_f*
......<<省略大概100多行

基本上是一个命令有很多用法,而帮助文档需要尽可能详尽地讲清楚(但是越看越糊涂)导致的

对于这个blog的话更多是面向已经很熟悉nvim/vim的,不太可能r,w,d,i这些基础的命令也不知道, 这个blog的话只讲一些奇怪神技(

执行,过滤以及外部命令(!符号)

没错,第一个讲的符号就是!符号

是一个很典型的在vim中出现二义性的符号,很多时候能写出容易让人看不懂的东西

OK,先说一下它的正常的用法:

1
2
:w!  -> 强制
:!bash ->执行命令

这两个是比较基本的,然后就是很少会有初学教程会提到的用法:

1
2
3
4
5
:[range]!<command>
eg.
:%xxd ->变为16进制视图
:%xxd -r ->变回来
:‘<,'>!xargs bash -c ->将选中的命令当作bash命令执行并且替换成指定的结果

可能不是很能被理解,实际上是运行指定的命令然后将范围内的的内容以stdin的形式发送给这个 程序并且使用这个程序的stdout替换给定的范围内的命令

可能说的比较绕其实也就是:「范围内的内容」-> 「这个命令」->「粘贴回来」

再配合xargs等命令总会有点奇怪的用法(

对于w,e这些命令的参数

其实这个文档讲的比较详细不过用的比较少一点,这里直接贴出来不作过多的解释

经常用来临时覆盖一些文件的设置比如临时保存为其他的编码什么的其实这方面是很有用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
The [++opt] argument can be used to set some options for one command, and to
specify the behavior for bad characters. The form is: >
++{optname}
Or: >
++{optname}={value}

Where {optname} is one of: *++ff* *++enc* *++bin* *++nobin* *++edit*
ff or fileformat overrides 'fileformat'
enc or encoding overrides 'fileencoding'
bin or binary sets 'binary'
nobin or nobinary resets 'binary'
bad specifies behavior for bad characters
edit for |:read|: keeps options as if editing a file
p for |:write|: creates the file's parent directory

{value} cannot contain whitespace. It can be any valid value for the options.
Examples: >
:e ++ff=unix
This edits the same file again with 'fileformat' set to "unix". >

:w ++enc=latin1 newfile
This writes the current buffer to "newfile" in latin1 format.

The message given when writing a file will show "[converted]" when
'fileencoding' or the value specified with ++enc differs from 'encoding'.

There may be several ++opt arguments, separated by whitespace. They must all
appear before any |+cmd| argument.

匹配

这一部分应该放到开头说的,不过这样子的开头可能有点啰嗦

这就另一个vim很有趣的东西基本上符合正则表达式的比如说常见的通配什么的

不过的话一些特殊的符号比如说是括号方括号什么的需要换成\(这样子的(否则vim默认是认为你想 要搜索的是普通字符)或者直接使用very magic模式(开头加入\v

实际上的话vim的这个虽然大体上遵循正则的模式但是其实这个东西是它自己实现的一套引擎,而不是PCRE标准(挖坑)