Skip to content
thawk edited this page Sep 20, 2019 · 29 revisions

VIM常用命令

目录

1. 日常使用

1.1. 寄存器registers

"

the unnamed register, containing the text of the last delete or yank

%

the current file name

#

the alternate file name

*

the clipboard contents (X11: primary selection)

+

the clipboard contents

/

the last search pattern

:

the last command-line

.

the last inserted text

-

the last small (less than a line) delete

=

the expression register: you are prompted to enter an expression (see |expression|)

_

黑洞寄存器

0

保存最近的yank命令的结果

1 - 9

1保存最近一次deletechange命令的结果;在后续删除操作时,依次移到2/3…​

1.2. 常用热键和命令

1.2.1. 不常用但有用的快捷键

g_CTRL-A: 选择多行,会把带数字的行,每行依次增加1/2/3/4等,可以方便地生成数字序列

1.2.2. 普通模式热键

VIM缺省热键
常用
.

重复上一条编辑命令

<C-[>

相当于ESC键,但手不用离开基本区

光标跳转
gg

跳到第一行

G

跳到最后一行

数字G

跳到指定行

gf

打开光标下的文件名

%

从左括号跳到对应的右括号或相反

<C-]>

跳转到光标下的符号处。需要ctags支持。

<C-O>

回到上次跳转前的位置

<C-I>

在<C-O>后,可以前进到<C-O>之前的位置

复制、粘贴
v

进入选择模式

V

进入行选择模式

<C-V>

进入块选择模式

p

粘贴。在按p后,可以通过按<C-P>粘贴,切换为剪切板上的历史内容

缓冲区切换
<C-^>

切换到上一个文件。在两个文件间来回切换时很方便

数字<C-^>

切换到指定编号的缓冲区(可以通过:ls列出所有缓冲区)

窗口操作
<C-W><C-S>

水平切分窗口

<C-W><C-V>

垂直切分窗口

<C-W>+HJKL等方向键

跳转到相应方向的切分窗口中

<C-W><C-Z>

关闭Preview窗口

<C-W> }

在preview窗口中查看当前光标下的word

<C-W>r

rotate windows downwards/rightwards

<C-W>R

rotate windows upwards/leftwards

<C-W>x

exchange current window with next one. or exchange current window with Nth window

<C-W>K/J/H/L

把当前窗口移动到最上/下/左/右

符号查找
[i

列出当前文件及头文件中,包含光标下word的第一行

]i

与[i类似,但从当前位置开始找

[I ]I

与[i及]i类似,但列出所有行

[<C-I>

从文件第一行开始找(包含头文件)跳到包含光标下word的第一行

]<C-I>

与[<C-I>类似,但从当前位置开始找

<C-W><C-I>

打开一个新窗口,显示包含光标下word的第一行

[d

显示光标下的宏的第一个定义

]d

与[d类似,从当前位置开始查找

[D ]D

与[d及]d类似,但列出所有行

1.2.3. 插入模式热键

0<C-D>

删除本行所有缩进

^<C-D>

删除本行所有缩进,在下一行恢复缩进。可用于插入一个label

<C-X><C-O>

补全当前命令

<C-X><C-L>

行补全。找以已输入内容开始的行

<C-X><C-F>

文件名补全,在写#include时很有用

<C-P> 及 <C-N>

从上面或下面查找以所输入内容开头的单词进行补全

1.2.4. 命令行热键

<C-B>

<Home>

<C-E>

<End>

<C-K> {char1} {char2}

输入digraph

<C-R> {0-9a-z"%#:-=.}

插入register的内容

<C-R><C-F>

插入the Filename under the cursor

<C-R><C-P>

插入the Filename under the cursor, expanded with 'path' as in |gf|

<C-R><C-W>

插入the Word under the cursor

<C-R><C-A>

插入the WORD under the cursor; see |WORD|

<C-R><C-L>

插入the line under the cursor

<S-Up>/PageUp>

调出更早的命令历史

<S-Down>/PageDown>

调出更新的命令历史

<C-]>

触发abbreviation

命令行补全

<C-D>

列出匹配光标前模式的名称

<C-A>

插入所有匹配光标前模式的名称

<C-L>

匹配光标前模式的名称。如果只有一个候选项就直接插入,有多个就插入最长前缀

1.2.5. Normal命令

:Dox

把光标放在函数定义上,执行本命令可以生成函数对应的Doxygen注释框架

:A

在.h/.cpp文件之间进行切换

:make

编译

:copen

打开编译结果窗口。在quickfix窗口中,可以用<F11>/<F12>在各出错行之间来回移动

:b word

切换到包含word的缓冲区

:nohl

取消高亮显示当前搜索内容

1.2.6. 代码折叠(Folding)

打开、关闭折叠
zo

打开光标行所属的折叠(可以指定打开多少层)

zO

打开光标行所属所有折叠

zc

关闭光标行所属的折叠(可以指定关闭多少层)

zC

关闭光标行所属的所有折叠

za

切换光标行所属折叠

zA

切换光标行所属所有折叠

zv

使光标所在行可见。将会打开适当层次的折叠,使得光标下的行可见

zx

更新折叠。恢复折叠到缺省状态,根据’foldlevel’打开适当的折叠,并 执行"zv",使得当前行可见

zX

更新折叠。恢复折叠到缺省状态,根据’foldlevel’打开适当的折叠

zm

多折叠一层。将’foldlevel’减一

zM

全部折叠。将’foldlevel’清0

zr

少折叠一层。将’foldlevel’加一

zR

打开全部折叠。将’foldlevel’设为最大折叠层次

zn

Fold none。清除’foldenable'。不折叠

zN

Fold normal。设置’foldenable'。恢复之前的折叠状态

zi

切换’foldenable'

折叠模式下,移动光标
[z

移动到当前打开的折叠的开始处。如果已经在折叠开始处,则移动到更高 一层折叠的开始处

]z

移动到当前打开的折叠的结束处。如果已经在折叠结束处,则移动到更高 一层折叠的结束处

zj

移动到下个折叠的开始处

zk

移动到上个折叠的结束处

zJ

移动到下个折叠的开始处,打开该折叠,关闭其它折叠

zK

移动到上个折叠的结束处,打开该折叠,关闭其它折叠

1.2.7. snippets

输入下列关键字,按<C-K>展开,展开后有多个可以修改的字段,用<C-K>在字段间进行跳转:

fc

生成文件头的Doxygen注释

once

生成include guard

unittest

生成Boost.Test单元测试框架

1.2.8. 缩写(abbr)

输入下列序列后,会自动展开:

/**空格

自动展开为 /** @brief */

/**回车

自动展开为Doxygen块注释

/**<

自动展开为 /**< @brief */

1.3. 有用的digraph

<C-K> {char1} {char2}

输入digraph

符号 输入 编码 十进制编码 字符名称

¡

!I

0xa1

161

INVERTED EXCLAMATION MARK

¢

Ct

0xa2

162

CENT SIGN

£

Pd

0xa3

163

POUND SIGN

¥

Ye

0xa5

165

YEN SIGN

Eu

20AC

8364

EURO SIGN

¦

BB

0xa6

166

BROKEN BAR

§

SE

0xa7

167

SECTION SIGN

©

Co

0xa9

169

COPYRIGHT SIGN

®

Rg

0xae

174

REGISTERED SIGN

°

DG

0xb0

176

DEGREE SIGN

0S

2070

8304

SUPERSCRIPT ZERO

¹

1S

0xb9

185

SUPERSCRIPT ONE

+S

207A

8314

SUPERSCRIPT PLUS SIGN

-S

207B

8315

SUPERSCRIPT MINUS

=S

207C

8316

SUPERSCRIPT EQUALS SIGN

(S

207D

8317

SUPERSCRIPT LEFT PARENTHESIS

)S

207E

8318

SUPERSCRIPT RIGHT PARENTHESIS

nS

207F

8319

SUPERSCRIPT LATIN SMALL LETTER N `

0s

2080

8320

SUBSCRIPT ZERO

1s

2081

8321

SUBSCRIPT ONE

+s

208A

8330

SUBSCRIPT PLUS SIGN

-s

208B

8331

SUBSCRIPT MINUS

=s

208C

8332

SUBSCRIPT EQUALS SIGN

(s

208D

8333

SUBSCRIPT LEFT PARENTHESIS

)s

208E

8334

SUBSCRIPT RIGHT PARENTHESIS

oC

2103

8451

DEGREE CELSIUS

1.

2488

9352

DIGIT ONE FULL STOP

2.

2489

9353

DIGIT TWO FULL STOP

1c

3220

12832

PARENTHESIZED IDEOGRAPH ONE

2c

3221

12833

PARENTHESIZED IDEOGRAPH TWO

·

.M

0xb7

183

MIDDLE DOT

,.

2026

8230

HORIZONTAL ELLIPSIS

%0

2030

8240

PER MILLE SIGN

½

12

0xbd

189

VULGAR FRACTION ONE HALF

13

2153

8531

VULGAR FRACTION ONE THIRD

¼

14

0xbc

188

VULGAR FRACTION ONE QUARTER

78

215E

8542

VULGAR FRACTION SEVEN EIGHTHS

×

*X

0xd7

215

MULTIPLICATION SIGN

÷

-:

0xf7

247

DIVISION SIGN

α

a*

03B1

0945

GREEK SMALL LETTER ALPHA

β

b*

03B2

0946

GREEK SMALL LETTER BETA

γ

g*

03B3

0947

GREEK SMALL LETTER GAMMA

δ

d*

03B4

0948

GREEK SMALL LETTER DELTA

ε

e*

03B5

0949

GREEK SMALL LETTER EPSILON

θ

h*

03B8

0952

GREEK SMALL LETTER THETA

λ

l*

03BB

0955

GREEK SMALL LETTER LAMDA

π

p*

03C0

0960

GREEK SMALL LETTER PI

1.4. 基本概念

1.4.1. 范围Range

  • 地址分隔符

    • ,: 第二项从当前行开始。

      4,/this line/

      从第4行到当前行后面匹配“this line”的行。

    • ;: 第二项从第一项开始。

      5;/that line/

      从第5行到第5行后面匹配“this line”的行。

2. SpaceVim

2.1. 可能有用的配置选项

init.toml[options]中设置。

  • windows_leader: 指定Window快捷键前缀。缺省为s,设为空串禁用此快捷键

  • enable_language_specific_leader: 逗号,作为语言特定快捷键前缀。缺省为1,即启用

  • windows_smartclose: 智能关闭缓冲区的按键。缺省为q,覆盖了原有记录宏的功能。空串禁用。

    <Leader>qr为原来q的功能

  • disabled_plugins: 禁用部分插件

    [options]
        disabled_plugins = ["neomake.vim"]
  • enable_guicolors: 是否使用true color。如果终端不支持真彩色,设为false

2.2. 不常用但有用的快捷键

  • 工具

    • <Leader>qr: 录制宏(原来的q

  • 切换开关

    • [SPC]tm: 切换状态栏上的不同元素

  • 窗口管理

    • <F2>: 打开Tagbar

    • <F3>: 打开VimFiler

    • [SPC]tt: 打开Tab Manager

    • Ctrl-x: 交换窗口内容

    • <Leader>qr+:///?: 打开命令行历史/搜索历史

    • [SPC]wc: Distraction-free reading

    • [SPC]wr: 轮换窗口

    • [SPC]wW: 选择窗口

  • 缓冲区

    • [SPC]fW: 以root身份写文件。需要启用sudo

    • <Leader>+: 下一窗口

    • <Leader>-: 上一窗口

    • [SPC]bw: 切换只读模式

    • ]f / [f: 打开目录中下一个文件/上一个文件

    • [SPC]bN + h/j/k/l: 在当前窗口4个方向创建新空白缓冲区

    • [SPC]bNn: 在当前窗口创建新空白缓冲区

  • 编辑

    • ,[SPC]: 去掉所有行尾空格

    • [[SPC] / ][SPC]: 上/下加空行

    • [e / ]e: 把当前行上移/下移

    • [SPC]jk: 自动缩进下一行(j==)

    • [SPC]jn: 在当前位置拆成两行(光标移到下一行)

    • [SPC]jo: 在当前位置拆成两行(光标留在当前行)

    • [SPC]js / [SPC]jS: 在当前位置把字符串拆成两个字符串。[SPC]jS添加换行

    • [SPC]sh: 进入highlight模式

    • [SPC]se: 进入iedit模式

      • <Tab>: 切换光标下的选中状态

  • 剪贴板

    • <Leader>y: 拷贝选中内容到系统剪贴板("+y)

    • <Leader>p: 从系统剪贴板粘贴("+p)

    • <Leader>P: 在当前字符前粘贴系统剪贴板内容

    • gp: 选择最后粘贴的内容

    • [SPC]bY: 拷贝整个缓冲区的内容到系统剪贴板

    • [SPC]fy: 将当前文件的绝对路径拷贝到系统剪贴板

  • 跳转

    • [SPC]jf: 向前跳(原生C-I

    • [SPC]jb: 向后跳(原生C-O

    • [SPC]jj: EasyMotion跳到单个字符

    • [SPC]jJ: EasyMotion跳到两个字符

    • [SPC]jw: EasyMotion跳到Word

    • [SPC]jl: 跳到行

  • 书签Bookmark

    • [SPC]fb: 列出所有书签

    • ma: 列出所有书签

    • mm: Toggle当前行书签

    • mn: 下一书签

    • mp: 上一书签

    • mi: Annotate

  • 帮助

    • <Leader>f[SPC]: 显示快捷键列表

    • <Leader>fp: 显示插件列表

    • [SPC]hl: 显示插件列表

  • Unite相关

    • <Leader>fe: 列出各registers的内容

    • <Leader>fh: 列出剪贴板历史

    • <Leader>fr: Resume

    • <Leader>fj: Jump

  • Location/Quickfix

    • <Leader>fl: Location list

    • <Leader>fq: quickfix

    • <Leader>q + c/l/n/p: quickfix下的操作

  • File tree

    • 打开file tree

      • [SPC]fo: 在file tree中定位当前文件

      • [SPC]ft: Toggle file tree

      • [SPC]fT: Show file tree

    • 在file tree中

      Key Bindings Descriptions

      l / <Enter>

      open file in one window

      sg

      open file in an vertically split window

      sv

      open file in an horizontally split window

  • vim table mode

    • <Leader>tm: 为当前buffer切换table mode状态

    • <Leader>tt: 选中内容转换为表格

    • <Leader>T: 选中内容转换为表格,将询问用户使用什么分隔符

    • <Leader>tr: 重新对齐表格列

    • <Leader>t?: Echo the current table cells representation for defining formulas

    • <Leader>tdd: 删除表行

    • <Leader>tdc: 删除表列

    • <Leader>tfa: 增加公式

    • <Leader>tfe: 计算公式

    • <Leader>ts: 排序

    • ||: 展开为表头分隔行

    • [|: Move to previous cell

    • ]|: Move to next cell

    • {|: Move to the cell above

    • }|: Move to the cell below

3. Google编码规范

3.1. 移植性

  • 字符串使用单引号。

    双引号有可能会破坏正则表达式。

  • 进行字符串匹配时,使用=~#`或=~?而不是=~`。

    匹配结果可以不受`ignorecast`和`smartcase`的影响。

  • 正则表达式前建议加上\m\C

    分别代表`nomagic`和`noignorecase`两个选项。

    也可以根据需要指定\v(magic levels)和\c(case sensitivities)。

  • 避免不确定性

    • normal!`而不是`normal,后者会收到用户自定义mapping的影响。

    • 避免使用`s[ubstitute]`等会受到用户设置影响的命令。可以用`search()代替。

  • 缩进

  • 正常: 2个空格

  • 续行: 4个空格

  • 命名

  • plugin-names-like-this

  • FunctionNamesLikeThis

  • CommandNamesLikeThis

  • augroup_names_like_this

  • variable_names_like_this

  • 变量作用域前缀

  • Global variables with g:

  • Script-local variables with s:

  • Function arguments with a:

  • Function-local variables with l:

  • Vim-predefined variables with v:

  • Buffer-local variables with b:

4. 安装

4.1. 依云版: (含lua,含中文行禁则补丁)

4.2. 支持lua的windows版本

通过`:version`可以看出需要哪个版本的lua库,可以到 https://sourceforge.net/projects/luabinaries/files/ 下载。

4.3. windows下可用的LLVM/Clang

4.4. 64位Windows下安装的版本

64位Windows下,应安装64位Vim,vimproc也使用64位,这样vimproc才可以正常工作。

Python也安装64位版本。

4.5. 解决E887找不到Python目录的问题

执行:python print 'hello'对Python进行测试,如果出现E887错误:

E887: Sorry, this command is disabled, the Python's site module could not be loaded.

两种解决方法:

  • 设置PYTHONHOME,指向Python目录,如C:\Python27

  • 把注册表中,HKLM\Software\Python\PythonCore\2.7改为HKLM\Software\Python\PythonCore\2.7-32

4.6. neovim安装专用python

5. debug调试

5.1. verbose 查看函数调用等详细信息

5.1.1. 启动方法

  1. 设置全局级别

    set verbose=9
  2. 启动vim时进行设置

    vim -V 9 file.text
    vim -V9vimlog file.text
  3. 单独为一条命令进行设置

    :12verbose call MyFunction()

5.1.2. 把verbose信息保存到单独的文件中

可以用verbosefile把结果保存在单独的文件中,以便查看:

set verbosefile=filename.txt

5.1.3. verbose级别

" >= 1  When the viminfo file is read or written.
" >= 2  When a file is ":source"'ed.
" >= 5  Every searched tags file and include file.
" >= 8  Files for which a group of autocommands is executed.
" >= 9  Every executed autocommand.
" >= 12 Every executed function.
" >= 13 When an exception is thrown, caught, finished, or discarded.
" >= 14 Anything pending in a ":finally" clause.
" >= 15 Every executed Ex command (truncated at 200 characters).

5.2. 查看vim启动耗时

vim --startuptime startup.log

6. 技巧

6.1. 使用:scriptencoding指定vim script文件的编码

set encoding=utf-8
scriptencoding utf-8
Note
scriptencoding一定要在encoding之后,否则无效

6.2. 局部变量应加s:

function外的变量缺省是global的,就此应该加s:以限定范围

for s:i in range(0,2)
  execute printf('source .local_%d.vim', s:i)
endfor

6.3. autocmd都应该属于一个group

" define a group `vimrc` and initialize.
augroup vimrc
  autocmd!
augroup END

" register autocmds to group `vimrc`.
autocmd vimrc FileType cpp setlocal expandtab
autocmd vimrc FileType make setlocal noexpandtab

6.4. 清除一个register

如要清除register a

qaq

6.5. 解决windows下菜单乱码问题

if has("win32")
    set guifont=Courier_New:h12
    set guifontwide=NSimsun:h12
    "解决菜单乱码
    source $VIMRUNTIME/delmenu.vim
    source $VIMRUNTIME/menu.vim
    "解决consle输出乱码
    language messages zh_CN.utf-8
endif

6.6. tab相关

To insert space characters whenever the tab key is pressed, set the 'expandtab' option:

:set expandtab

With this option set, if you want to enter a real tab character use Ctrl-V<Tab> key sequence.

To control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 4 spaces for a tab, use:

:set tabstop=4

After the 'expandtab' option is set, all the new tab characters entered will be changed to spaces. This will not affect the existing tab characters. To change all the existing tab characters to match the current tab settings, use:

:retab

To change the number of space characters inserted for indentation, use the 'shiftwidth' option:

:set shiftwidth=4

6.7. vimdiff

忽略空白字符
set diffopt+=iwhite
忽略大小写
set diffopt+=icase

6.8. 插件配置

align
  • 正确处理汉字的宽度

    let g:Align_xstrlen= 3

6.9. python编程环境

配置基于Vim的Python编程环境

  1. 更好的语法高亮

  2. 增强的代码补齐提示

    pydiction

    把解压出来的pydiction文件放到$HOME/.vim下,在vimrc中增加:

    " Python 自动补全功能,用 Ctrl-N 调用
    " isk+=.,表示把小数点和逗号作为word的一部分
    if has("autocmd")
      autocmd FileType python set complete+=k~/.vim/pydiction isk+=.,
    endif

6.11. 正确显示ANSI Escape Code

AnsiEsc.vim

6.12. 为系统的头文件建立ctags

  1. 建立ctags

    # 使用行号定位,大大缩小了tags文件的大小
    pacman -Ql gcc glibc | cut -d ' ' -f 2 | grep --color=none include | ctags --c++-kinds=+p --fields=+iaS --extra=+q --language-force=c++ --excmd=number -f ~/.tags/c++.tags -L -
    # 去掉/detail/下的实现文件
    pacman -Ql boost | cut -d ' ' -f 2 | grep --color=none include | grep --color=none -v '/detail/' | ctags --c++-kinds=+p --fields=+iaS --extra=+q --language-force=c++ --excmd=number -f ~/.tags/boost.tags -L -
  2. 在vim中使用该ctags

    set tags+=~/.vim/commontags
  3. 用’echo tagfiles()'可以查看当前使用tags

6.14. 控制 Ctrl-P/Ctrl-N 的扫描范围

可以通过’complete'('cpt')选择进行控制,其中’i’就是include

6.15. 控制include范围

'include'

匹配include行

':checkpath'

检查要搜索的文件,':checkpath!'列出将要搜索的文件

'includeexpr'

匹配行如何变为文件名

6.16. echofunc 自动显示函数的签名

如果有多个原型,可以用Alt--/Alt-=进行切换

6.17. 用vim查看彩色的manpage

vman () { vim +"set ft=man" +"Man $*" }

6.18. 模仿vim操作的工具

6.18.1. vimperator for firefox

另有pentadactyl为vimperator的一个分支

6.18.2. ranger

操作象vi的字符模式文件管理器

6.18.3. vifm

也是操作象vi的字符模式文件管理器

6.19. colorscheme

solarized

16色,light/dark背景适用

动态切换整个终端的配色

https://github.com/sos4nt/dynamic-colors

vim中固定使用solarized的dark模式即可,上述脚本可以通过更换调色板来变换颜色。

Firefox的Evernote Clearly插件设置solarized配色
  • Background Color #002b36

  • Foreground Color #839496

  • Links Color #268bd2

  • Custom CSS

    #text blockquote { border-color: #586e75; color: #93a1a1; }
    #text thead { background-color: #839496; color: #002b36}
    #text tr:nth-child(even) { background: #073642; }
    #text #articleHeader { border-color: #c5c5c5; }

6.20. 使用pathogen管理插件

可以用 ~/.vim/update_bundles script 来管理插件的升级。 https://gist.github.com/570398

vim-scripts.org上有vim.org上所有插件的git库。 http://vim-scripts.org/index.html

6.21. 插件

zencoding-vim

方便编写html/xml的插件

6.22. statusline

vim-airline lightline.vim

6.24. 检测终端是深色背景还是浅色背景

  • 在ssh服务器上,允许传递COLORFGBG环境变量

    echo -e "Defaults\tenv_keep=\"VISUAL EMAIL COLORFGBG\"" >> /etc/sudoers
    echo "AcceptEnv COLORFGBG" >> /etc/ssh/sshd_config
  • 在客户端设置该变量

    echo -e 'Host *\n\tSendEnv COLORFGBG' >> ~/.ssh/config

6.25. 有意思的插件

https://github.com/kana/vim-arpeggio

可以同时按几个键执行一个功能(而不是依次按)

https://github.com/MattesGroeger/vim-bookmarks

可以设置bookmark,并可以加上annotation,用于进行走查

https://github.com/skeept/Ultisnips-neocomplete-unite

为ultrasnips提供neocomplete和unite支持

https://github.com/dhruvasagar/vim-table-mode

制作表格。自动对齐、自动扩宽列

https://github.com/junegunn/vim-easy-align

对齐插件

https://github.com/kana/vim-altr

类似FSwitch,但可以在一系列文件(如.h、.cpp、单元测试)间切换

6.26. 查看当前位置的高亮组(hightlight group)

for id in synstack(line("."), col("."))
    echo synIDattr(id, "name")
endfor
echo synIDattr(synstack(469,1)[0], "name")

6.27. 把plugin文件中的command提取出来

'<,'>v/-complete=/s/^command\S\+\s\+\(-\S\+\s\+\)*\(\w\+\).*/'\2',/
'<,'>g/-complete=/s/^command\S\+\s\+\(-\S\+\s\+\)*-complete=\(\S\+\)\s\+\(\w\+\).*/{'name': '\3', 'complete': '\2'},/

6.28. 帮助NeoBundle识别FreeBSD

可以安装gmake,NeoBundle就可以识别为FreeBSD。

6.29. 忘记用sudo进行编辑

:w !sudo tee %

6.30. 避免用xargs调用vim时,报“Vim: Warning: Input is not from a terminal

IFS=$'\n' vim $(find . | percol)

6.31. 把markdown的符号列表的不同级别用不同符号

:%s/^\(  \(    \)*\)\* /\1- / | %s/^\(\(    \)*\)\- /\1* /

7. 常用例程

7.1. 取当前script路径

" Relative path of script file:
let s:path = expand('<sfile>')

" Absolute path of script file:
let s:path = expand('<sfile>:p')

" Absolute path of script file with symbolic links resolved:
let s:path = resolve(expand('<sfile>:p'))

" Folder in which script resides: (not safe for symlinks)
let s:path = expand('<sfile>:p:h')

" If you're using a symlink to your script, but your resources are in
" the same directory as the actual script, you'll need to do this:
"   1: Get the absolute path of the script
"   2: Resolve all symbolic links
"   3: Get the folder of the resolved absolute file
let s:path = fnamemodify(resolve(expand('<sfile>:p')), ':h')

7.2. 获取当前选中的内容

" s:VisualSelection() {{{
" Thanks to xolox!
" http://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript
func! s:VisualSelection() abort
    " Why is this not a built-in Vim script function?!
    let [lnum1, col1] = getpos("'<")[1:2]
    let [lnum2, col2] = getpos("'>")[1:2]
    let lines = getline(lnum1, lnum2)
    let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
    let lines[0] = lines[0][col1 - 1:]
    return join(lines, "\n")
endf
" }}}

7.3. gvim使用的字体

getfontname()

7.4. 把unicode转换为&encoding的编码

iconv(nr2char(0xe0b0, 1), "utf-8", &encoding)
Clone this wiki locally