Skip to content

Instantly share code, notes, and snippets.

@BruceChen7
Last active August 30, 2022 15:59
Show Gist options
  • Save BruceChen7/a7fa1052b4741f4626201b235320905b to your computer and use it in GitHub Desktop.
Save BruceChen7/a7fa1052b4741f4626201b235320905b to your computer and use it in GitHub Desktop.
[#vim#vimscript] #vim #tools #vimscript

目录

插件

gtags的安装

源码

拷贝

我们可以使用alt + mouse来拷贝。

vim-diff

编译

./configure --with-features=huge\
--enable-multibyte\
--enable-rubyinterp=yes\
--enable-pythoninterp=yes\
--with-python-config-dir=/lib64/python2.7/config\
--enable-perlinterp=yes\
--enable-luainterp=yes\
--enable-cscope\
--prefix=/usr/local


# ycm编译

cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=/usr/local/ . ~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp

## ubuntu等使用系统clang编译
mkdir ycm_build
cd ycm_build
cmake -G "Unix Makefiles" . ~/.vim/plugged/YouCompleteMe/third_party/ycmd/cpp -DUSE_SYSTEM_BOOST=ON -DEXTERNAL_LIBCLANG_PATH=/usr/lib/x86_64-linux-gnu/libclang-3.8.so.1

# command_json 软连接
ln -s build/compile_commands.json  -rt ./

注意使用
 
 let g:ycm_server_python_interpreter="/usr/local/bin/python3.7", 来指定python的版本                                      

motion

*手册

移动的高级方式

  • Ctrl + g 查看当前光标的位置

  • g_ 到最后一个非空白字符上

  • g; go to last edit

  • g, 返回新的跳动的位置

  • ctrl + e : scroll one line down

  • ctrl + y : scroll one line up

  • ctrl + d : scroll half page down

  • ctrl + u : scroll half page up

  • ctrl + f : scroll one page donw

  • ctrl + b : scroll one page up

例子:

{foo(x, y, |bar(), baz());}
d[( : exclusive motion so result is {foo|bar(), baz());}
d]) : exclusive motion so result is {foo(x, y, |)}
d<( : inclusive motion so result is {foo(|bar(), baz());}
d>) : inclusive motion so result is {foo(x, y, |)} as same as d])

文本对象

一般我们操作的是一个个字符串,vim允许用户操作一个单词,一段句子,这里的单词,句子就可以当成是文本对象。 在vim中的文本编辑命令符合下面的模式:

<number><command><text object or motion>
  • The number is used to perform the command over multiple text objects or motions, e.g., backward three words, forward two paragraphs. The number is optional and can appear either before or after the command.
  • The command is an operation, e.g., change, delete (cut), or yank (copy). The command is also optional; but without it, you only have a motion command, not an edit command
  • The text object or motion can either be a text construct, e.g., a word, a sentence, a paragraph, or a motion, e.g., forward a line, back one page, end of the line.

简单的文本对象

words

  • aw – a word (includes surrounding white space)
  • iw – inner word (does not include surrounding white space)

sentence

  • as – a sentence
  • is – inner sentence

Paragraph

  • ap – a paragraph
  • ip – inner paragraph

motions command and text object command

区别:

  • cw 是从当前光标处开始改写
  • ciw 则是将整个单词开始改写

programming language text objects

  • a" – a double quoted string
  • i" – inner double quoted string
  • a' – a single quoted string
  • i' – inner single quoted string
  • a` – a back quoted string
  • i` – inner back quoted string

parenthese

  • a) – a parenthesized block
  • i) – inner parenthesized block

brackets

  • a] a bracketed block
  • i] inner bracketed block

braces

  • a} – a brace block
  • i} – inner brace block

markup language tags

  • at – a tag block
  • it – inner tag block

a single tag

  • a> – a single tag
  • i> – inner single tag

An editing command is a command plus a text object or motion, e.g., delete this word, change the next sentence, copy this paragraph.

文本对象的操作

  • ( sentences backward
  • ) sentences forward
  • { paragraphs backward.
  • } paragraphs forward

vim-surround

这个插件主要用来操作文本对象:

  • cs"' 将双引号改成单引号 "hello wrold " ---> 'hello world'

  • cs" 将"helo world" 改成 hello world

  • cs 命令有两个参数:a target and a replacement

  • ds 命令用来删除target

  • ys takes a valid Vim motion or text object as the first object, and wrap it using the second argument as with cs. hello wo*rld 使用命令ysiw) ,那么会变成 hello (world)

资料来源

点号

可以重复上一次的编辑命令,比如:

int foo(char *s, int len) {
    char *p = s;
    for (char *e = s+len; p < e; p++)
        /* do something */ ;
    return len / 2;
}

你可以:

  • 将光标放在第一个len上,按* 键高亮选择所有的len单词,并跳到下一个len上
  • 按N 键返回到第一个len上,按ciwn 将第一个len改成n
  • 按n 调到第二个len上,按.
  • 按n 调到第三个len上,按.

Instert模式的操作

  • 用来删除一个字符

  • 用来删除一个单词

  • 删除到开始 这些操作也可以在命令和shel中使用。

  • r 替换当前字符

  • R 进入替换模式,直至 ESC 离开

  • s 替换字符(删除光标处字符,并进入插入模式,前可接数量)

  • S 替换行(删除当前行,并进入插入模式,前可接数量)

  • cc 改写当前行(删除当前行并进入插入模式),同 S

  • cw 改写光标开始处的当前单词

  • ciw 改写光标所处的单词

  • caw 改写光标所处的单词,并且包括前后空格(如果有的话)

  • c0 改写到行首

  • c^ 改写到行首(第一个非零字符)

  • c$ 改写到行末

  • ci" 改写双引号中的内容

  • ci' 改写单引号中的内容

  • ci) 改写小括号中的内容

  • ci] 改写中括号中内容

  • ci} 改写大括号中内容

  • cit 改写 xml tag 中的内容

  • cis 改写当前句子

  • c2w 改写下两个单词

  • ct( 改写到小括号前

  • x 删除当前字符,前面可以接数字,3x代表删除三个字符

  • X 向前删除字符

  • dd 删除当前行

  • d0 删除到行首

  • d^ 删除到行首(第一个非零字符)

  • d$ 删除到行末

  • D 删除到行末(同 d$)

  • dw 删除当前单词

  • diw 删除光标所处的单词

  • daw 删除光标所处的单词,并包含前后空格(如果有的话)

  • di" 删除双引号中的内容

  • di' 删除单引号中的内容

  • di) 删除小括号中的内容

  • di] 删除中括号中内容

  • di} 删除大括号中内容

  • dit 删除 xml tag 中的内容

  • dis 删除当前句子

  • d2w 删除下两个单词

  • dt( 删除到小括号前

  • dgg 删除到文件头部

  • dG 删除到文件尾部

  • d} 删除下一段

  • d{ 删除上一段

  • u 撤销

  • U 撤销整行操作

  • CTRL-R 撤销上一次 u 命令

  • J 链接多行为一行

  • . 重复上一次操作

  • ~ 替换大小写

  • g~iw 替换当前单词的大小写

  • gUiw 将单词转成大写

  • guiw 将当前单词转成小写

  • guu 全行转为小写

  • gUU 全行转为大写

  • << 减少缩进

  • >> 增加缩进

  • == 自动缩进

  • CTRL-A 增加数字

  • CTRL-X 减少数

位置跳转

  • CTRL-O 跳转到上一个位置
  • CTRL-I 跳转到下一个位置
  • CTRL-^ 跳转到 alternate file (当前窗口的上一个文件)
  • % 跳转到 {} () [] 的匹配
  • gd 跳转到局部定义(光标下的单词的定义)
  • gD 跳转到全局定义(光标下的单词的定义)
  • gf 打开名称为光标下文件名的文件
  • [[ 跳转到上一个顶层函数(比如C语言以大括号分隔), 这个地方只有C, Python语言好像是这样,
  • ]] 跳转到下一个顶层函数(比如C语言以大括号分隔)
  • [m 跳转到上一个成员函数
  • ]m 跳转到下一个成员函数
  • [{ 跳转到上一处未匹配的 {
  • ]} 跳转到下一处未匹配的 }
  • [( 跳转到上一处未匹配的 (
  • ]) 跳转到下一处未匹配的 )
  • [c 上一个不同处(diff时)
  • ]c 下一个不同处(diff时)
  • [/ 跳转到 C注释开头
  • ]/ 跳转到 C注释结尾

文本对象

$ 到行末 0 到行首 ^ 到行首非空字符 tx 光标位置到字符 x 之前 fx 光标位置到字符 x 之处 iw 整个单词(不包括分隔符) aw 整个单词(包括分隔符) iW 整个 WORD(不包括分隔符) aW 整个 WORD(包括分隔符) is 整个句子(不包括分隔符) ib 小括号内 ab 小括号内(包含小括号本身) iB 大括号内 aB 大括号内(包含大括号本身) i) 小括号内 a) 小括号内(包含小括号本身) i] 中括号内 a] 中括号内(包含中括号本身) i} 大括号内 a} 大括号内(包含大括号本身) i' 单引号内 a' 单引号内(包含单引号本身) i" 双引号内 a" 双引号内(包含双引号本身) 2i) 往外两层小括号内 2a) 往外两层小括号内(包含小括号本身) 2f) 到第二个小括号处 2t) 到第二个小括号前

帮助信息

  • :h tutor 入门文档
  • :h quickref 快速帮助
  • :h index 查询 Vim 所有键盘命令定义
  • :h summary 帮助你更好的使用内置帮助系统
  • :h CTRL-H 查询普通模式下 CTRL-H 是干什么的
  • :h i_CTRL-H 查询插入模式下 CTRL-H 是干什么的
  • :h i_ 查询插入模式下方向键上是干什么的 :h pattern.txt 正则表达式帮助
  • :h eval 脚本编写帮助
  • :h function-list 查看 VimScript 的函数列表
  • :h windows.txt 窗口使用帮助
  • :h tabpage.txt 标签页使用帮助
  • :h +timers 显示对 +timers 特性的帮助
  • :h :! 查看如何运行外部命令
  • :h tips 查看 Vim 内置的常用技巧文档
  • :h set-termcap 查看如何设置按键扫描码
  • :viusage NORMAL 模式帮助
  • :exusage EX 命令帮助
  • :version 显示当前 Vim 的版本号和特性
  • :h motion.txt 查看移动的快捷键

查找替换

  • /pattern 从光标处向文件尾搜索 pattern
  • ?pattern 从光标处向文件头搜索 pattern
  • n 向同一方向执行上一次搜索
  • N 向相反方向执行上一次搜索
  • * 向前搜索光标下的单词
  • # 向后搜索光标下的单词
  • :s/p1/p2/g 将当前行中全替换p1为p2
  • :%s/p1/p2/g 将当前文件中全替换p1为p2
  • :%s/p1/p2/gc 将当前文件中全替换p1为p2,并且每处询问你是否替换
  • :10,20s/p1/p2/g 将第10到20行中所有p1替换为p2
  • :%s/1\2/3/123/g 将“1\2/3” 替换为 “123”(特殊字符使用反斜杠标注)
  • :%s/\r//g 删除 DOS 换行符 ^M

从插入模式到命令行模式

  • <C-[>
  • 回归到Insert-normal模式

使用/或者?进行搜索时

  • Ctrl + G,跳到下一个位置
  • Ctrl + T 跳到上一个位置

正确的设置alt键

自定义命令行

space开始的命令

  • e - : 用-来表示注释块
  • e = : 用=来表示注释块
  • e c : copy
  • f h : 打开实现,或头文件
  • f t 在当前buffer中打开目录
  • f e 分裂出一个窗口,然后选择文件编辑
  • f o 在整个buffer打开目录选择

g开头命令

  • gb 跳转到声明
  • gl 跳转到实现
  • gx 声明或实现
  • g1 grep 当前光标的文字
  • g2 同样是grep当前光标的内容
  • g3 cscope 全局查找内容
  • g4 cscope 当前文件查找

alt键开始的命令

  • alt-m : 列出之前打开过的文件
  • alt-u: 列出buffer
  • alt-t : 列出当前tags
  • alt-p :列出函数名

ctrl键开始的命令

  • p : 列出文件

tab键

  • tab - [h j k l]窗口的移动

基本概念

缓冲区

Vim 是一个文本编辑器。每次文本都是作为缓冲区的一部分显示的。每一份文件都是在他们自己独有的缓冲区打开的,插件显示的内容也在它们自己的缓冲区中。

缓冲区有很多属性,比如这个缓冲区的内容是否可以修改,或者这个缓冲区是否和文件相关联,是否需要同步保存到磁盘上。

窗口

窗口是缓冲区上一层的视窗。如果你想同时查看几个文件或者查看同一文件的不同位置,那样你会需要窗口。

请别把他们叫做 分屏 。你可以把一个窗口分割成两个,但是这并没有让这两个窗口完全 分离 。

窗口可以水平或者竖直分割并且现有窗口的高度和宽度都是可以被调节设置的,因此,如果你需要多种窗口布局,请考虑使用标签。

标签页

标签页 (标签)是窗口的集合。因此当你想使用多种窗口布局时候请使用标签。

简单的说,如果你启动 Vim 的时候没有附带任何参数,你会得到一个包含着一个呈现一个缓冲区的窗口的标签。

顺带提一下,缓冲区列表是全局可见的,你可以在任何标签中访问任何一个缓冲区。

返回主目录 arrow_heading_up

资料来源

基本了解

"将字符串当成命令来执行
exec 'set rtp+='.s:home  " . 表示连接字符串
" 暂时不知道啥意思 
let s:home = fnamemodify(resolve(expand('<sfile>:p')), ':h')

" 自定义命令 command! 申明一个命令,命名为IncScript 具体的定义是后面的 exec 相当于普通语言的include指令。
" :help :command
command! -nargs=1 IncScript exec 'so '.s:home.'/'.'<args>'

echom命令

打印当前变量的值,并且可以保存在message信息表中,可以通过messages命令查看。

:echom g:mapleader 查看当前leader键设置的默认值
:echo has('guirunning')
:echom has("python")
:echom has("python3")
:help echom 查看

set命令

可以查看和设置摸个变量

set ft? "查看当前缓冲区的文件类型

变量

  • 使用&来引用选项,我把选项理解成vim内置的变量
  • local选项,在前面设置l表示访问
  • :help internal_variables
  • 默认情况下,如果一个变量在函数体以外初始化的,那么它的作用域是全局变量;而如果它是在函数体以内初始化的,那它的作用于是局部变量。同时你可以通过变量名称前加冒号前缀明确的指明变量的作用域。
:let foo = "bar"
:echo foo
:let foo = 42
:echo foo

:set textwidth=80
:echo &textwidth

:let &textwidth = 100
:set textwidth? 
:let &l:number = 0

变量的作用域

  • : help internal-variables
  • b local to current buffer
  • w: local to current window
  • g: global
  • s: local to a :source ed vim script
  • a: function arguments inside a function
  • v: global predefined by vim
  • l: local to a function
:let b:hello = "world"
:echo b:hello

变量类型

Number:32 位有符号整数

-123
0x10
0177

Float: 浮点数,需要编译 Vim 的时候,有 +float 特性支持

123.456
1.15e-6
-1.1e3

String: NULL 结尾的 8位无符号字符串

"ab\txx\"--"
'x-z''a,c'

Funcref: 函数引用,函数引用类型的变量名必须以大写字母开头

:let Myfunc = function("strlen")
:echo Myfunc('foobar') " Call strlen on 'foobar'.

List: 有序列表

:let mylist = [1, 2, ['a', 'b']]
:echo mylist[0]
1
:echo mylist[2][0]
a
:echo mylist[-2]
2
:echo mylist[999]
E684: list index out of range: 999
:echo get(mylist, 999, "THERE IS NO 1000th ELEMENT")
THERE IS NO 1000th ELEMENT

Dictionary: 无序的 Key/Value 容器

:let mydict = {'blue': "#0000ff", 'foo': {999: "baz"}}
:echo mydict["blue"]
#0000ff
:echo mydict.foo
{999: "baz"}
:echo mydict.foo.999
baz
:let mydict.blue = "BLUE"
:echo mydict.blue
BLUE

没有布尔类型,整数 0 被当作假,其他被当作真。字符串在比较真假前会被转换成整数,大部分字符串都会被转化为 0,除非以非零开头的字符串才会转化成非零。

(译注:可以调用 type(varname) 来取得变量的类型,最新版 Vim 8.1 中已经包含 Boolean 类型,并且有 v:true, v:false 两个值)

字符串比较

<string> == <string>: 字符串相等
<string> != <string>: 字符串不等
<string> =~ <pattern>: 匹配 pattern
<string> !~ <pattern>: 不匹配 pattern
<operator>#: 匹配大小写
<operator>?: 不匹配大小写

注意:设置选项 ignorecase 会影响 == 和 != 的默认比较结果,可以在比较符号添加 ? 或者 # 来明确指定大小写是否忽略。

. : 字符串链接

:function! TrueFalse(arg)
:   return a:arg? "true" : "false"
:endfunction

:echo TrueFalse("X start" =~ 'X$')
false
:echo TrueFalse("end X" =~ 'X$')
true
:echo TrueFalse("end x" =~# 'X$')
false

条件语句

" 输出
:if 1
:    echom "ONE"
:endif 

" 不会输出
:if 0
:    echom "ZERO"
:endif
“ 不会输出
:if "something"
:    echom "INDEED"
:endif
" 输出
:if "9024"
:    echom "WHAT?!"
:endif


:echom "hello" + 10   " 10
:echom "10hello" + 10 " 20
:echom "hello10" + 10   " 10

规则:

  • Vim will try to coerce variables (and literals) when necessary. When 10 + "20foo" is evaluated Vim will convert "20foo" to an integer (which results in 20) and then add it to 10.
  • Strings that start with a number are coerced to that number, otherwise they're coerced to 0.
  • Vim will execute the body of an if statement when its condition evaluates to a non-zero integer, after all coercion takes place.
:if 0
:    echom "if"
:elseif "nope!"
:    echom "elseif"
:else
:    echom "finally!"
:endif

循环

for <var> in <list>
    continue
    break
endfor

while循环

while <expression>
endwhile

异常捕获

try
    ...
catch <pattern (optional)>
    " HIGHLY recommended to catch specific error.
finally
    ...
endtry

函数

  • 以大写字母开头,记住了。
  • 如果函数没有显示的返回,默认返回0
  • :help :call. 和 :help return
:function Meow()
:  echom "Meow!"
:endfunction
// 返回值
:function GetMeow()
:  return "Meow String!"
:endfunction

:call Meow() // 调用函数,

list

  • list可以是数字和字符串的组合
  • list从下标0开始计算,列表可以嵌套
  • list可以设置为-1或者是-2等,-1表示最后一个元素,-2表示倒数第二个元素
  • list和python一样可以切片,见下面的例子
  • string也可以使用切片来访问,但是不能够使用负索引
  • 列表的连接,可以使用+表示连接
:echo ['foo', [3, 'bar']]

:echo [0, [1, 2]][1]  # 

:echo ['a', 'b', 'c', 'd', 'e'][0:2]

:echo ['a', 'b', 'c', 'd', 'e'][:1] ; ["a", "b", "c"]
:echo ['a', 'b', 'c', 'd', 'e'][3:] ; ["d", "e"]
:echo "abcd"[0:2] ; abc
:echo ['a', 'b'] + ['c']

几个list函数

  • add
  • get
  • index
  • len
  • join
:let foo = ['a']
:call add(foo, 'b')
:echo foo

帮助文档

  • help List
  • help add()
  • help get()
  • help index()
  • help join
  • help reverse

循环

  • 使用for和while来表示循环
:let c = 0

:for i in [1, 2, 3, 4]
:  let c += i
:endfor

:echom c

:let c = 1
:let total = 0

:while c <= 4
:  let total += c
:  let c += 1
:endwhile

:echom total

string

  • :help literal-string
  • 用单引号表示字符串本身,即字符传没有特别的意思
:echom "Hello, " . "world"  " helllo, wrold
:echom 10 . "foo"   " 10foo
:echom "foo \"bar\""  " foo " bar
:echom 'That''s enough.' " That's enough

函数

使用 function 关键字定义一个函数,使用 function! 覆盖一个函数的定义,函数和变量一样也有作用范围的约束。需要注意函数名必须以大写字母开头。

function! <Name>(arg1, arg2, etc)
    <function body>
endfunction
delfunction <function> 删除一个函数

call 调用一个函数,函数调用前的 call 语句是必须的,除非在一个表达式里。

例子:强制创建一个全局函数(使用感叹号),参数使用 ... 这种不定长的参数形式时,a:1 表示 ... 部分的第一个参数,a:2 表示第二个,如此类推,a:0 用来表示 ... 部分共有多少参数。

function! g:Foobar(arg1, arg2, ...)
    let first_argument = a:arg1
    let index = 1
    let variable_arg_1 = a:{index} " same as a:1
    return variable_arg_1
endfunction

有一种特殊的调用函数的方式,可以指明该函数作用的文本区域是从当前缓冲区的第几行到第几行,按照 “1,3call Foobar() ”的格式调用一个函数的话,该函数会在当前文件的第一行到第三行每一行执行一遍,再这个例子中,该函数总共被执行了三次。

如果你在函数声明的参数列表后添加一个 range 关键字,那函数就只会被调用一次,这时两个名为 a:firstlinea:lastline 的特殊变量可以用在该函数内部使用。

例如:强制创建一个名为 RangeSize 的函数,用来显示被调用时候的文本范围:

function! b:RangeSize() range
    echo a:lastline - a:firstline
endfunction
  • strlen("foo")
  • len("foo")
  • split("one two three") " ["one", "two", "three"]
  • :echo split("one,two,three", ",")
  • :echo join(["foo", "bar"], "...") " foo...bar
  • :echo join(split("foo bar"), ";") foo;bar

简单的使用函数,条件语句

let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py'                                          
function! Load_ycm_config_file()                                                                       
    if &ft == 'cc' || &ft == 'cpp' || &ft == 'h'                                                       
        let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf_c.py'                                
    else                                                                                               
        let g:ycm_global_ycm_extra_conf = '~/.vim/.ycm_extra_conf.py'                                  
    endif                                                                                              
endfunc         
command! -nargs=0 LoadYcmFile call Load_ycm_config_file()    "这里创建一个命令                          
nnoremap <space>ycm :LoadYcmFile <cr>    "使用spaceycm来根据文件类型来加载

另一个例子:用来在重新生成compile_commands.json数据,然后放到工作目录下

"======================================================================
" 将build/compile_commands.json放在当前的工作目录中
"======================================================================
function! <SID>CopyBuildJsonToCWD()
    let l:cwd_directory = getcwd()
    let l:build_directory = l:cwd_directory . '/build'
    let l:build_json_file = l:build_directory . "/compile_commands.json"
    if !isdirectory(l:build_directory) || !filereadable(l:build_json_file)
        call asclib#errmsg(l:build_directory . " is not existed or ".l:build_json_file. " not exis>
        return
    endif
    call system('cp ' . l:build_json_file. ' '. l:cwd_directory)
    call asclib#cmdmsg('compile_commands move done', 1)
    " 清除Ycm编译数据
    YcmCompleter ClearCompilationFlagCache
endfunc

几个重要的变量

测试时,在/home/brookchen下

:echo expand("%:p")   " : 显示当前buffer中该文件的绝对路径包括文件名
:echo expand("%:h")  " 显示.vim/asc文件 head of file name,不包括文件名,只有路径信息
:echo expand("%:t")  "文件名,没有路径信息
:echo expand("%:e") "扩展名。

几个重要的函数

  • help line 获取当前光标位置 line('.')
  • help append 在当前缓冲去添加文本。
  • help expand

几个重要的函数

  • help line 获取当前光标位置 line('.')
  • help append 在当前缓冲去添加文本。
  • help expand

自动加载的函数

在autoload 文件夹中,所有在这个文件夹中会自动加载

function general#DeleteTrailingWS() abort
    normal mz
    %s/\v\s+$//ge
    normal `z
endfunc

当调用:call general#DeleteTrailingWS(),vim会

  • 查找autoload文件夹中名叫general的文件
  • 找到DeleteTrailingWS的函数
  • 加载并运行

你想找到general中的文件所有的函数,可以调用:function /general

声明命令

  • command 输出所有的用户命令
  • command 用于输出所有以开头的命令
  • command <attribute> <name> <cmd> 定义一个命令,命令的名字是name,调用命令<cmd>,后面command!,表示如果command名字相同,覆盖。

命令属性

  • -nargs=0 - No argument allowed (default).
  • -nargs=1 - One argument is required.
  • -nargs=* - Any number of arguments allowed.
  • -nargs=? - 0 or 1 argument allowed.
  • -nargs=+ - One argument or more are required.

比如:

function IScream(content)
   echom toupper(a:content)
endfunction

command -nargs=1 Scream call IScream(<args>)
:Scream "hello"

When a user command call a function which can take multiple arguments, you need to separate them with whitespaces and use the placeholder instead of .

If there is only one argument allowed, Vim will consider the whitespace as part of the argument itself.

使用help user-commands

在command-line mode 特殊占位符

  • % - Relative path of the current file.
  • <cword> - Word under the cursor.
  • <cWORD> - WORD under the cursor.
  • <cfile> - Filepath under the cursor.
  • <afile> - File open in the buffer when executing autocommands.
  • <sfile> - Filename of sourced file when used with command :source.

You can also use the following with %:

  • :p - 输出绝对路径,而不是相对路径. Also expand the tilda ~ to the home directory.
  • :. - Make the file path relative to the working directory.
  • :~ - Make the file path relative to the home directory (if possible).
  • :h - Keep the head of the file path (remove the last element).
  • :t - Keep the tail of the file path (remove everything except the last element).
  • :r - 保留根文件的名称,取出扩展名 (remove its extension).
  • :e - Remove everything except the extension of the filename.
  • :s?pat?sub? - Substitute the first occurrence of “pat” with “sub”.
  • :gs?pat?sub? - Substitute all occurrences of “pat” with “sub”.

These special strings only work when a command expects a filename as argument; as a result, it makes this functionality quite limited. Fortunately, You can use the function expand(<special_string>) to expand these placeholders in any command.

@BruceChen7
Copy link
Author

设置alt key为meta key

image
保存时注意选中
image

@BruceChen7
Copy link
Author

移动的高级方式

image

@BruceChen7
Copy link
Author

set encoding=utf-8 "设置gvim内部编码
set fileencoding=utf-8 "设置当前文件编码
set termencoding=utf-8 "设置终端编码
set fileencodings=ucs-bom,utf-8,gbk,gb18030,cp936,big5,euc-jp,euc-kr,latin "设置支持打开的文件的编码

@BruceChen7
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment