打开最近的文件

:ol
# ol 指 old files

之后每个文件对应一个数字
然后使用 :e <数字> 打开这个文件

清除搜索的高亮

:nohl

替换换行

%s/foo/bar

foo 中换行符根据操作系统来确定,win: \r\n, mac: \r, linux: \n
bar 中换行符用 \r

替换开头空白

%s/^+\s//g 空格
%s/^+\t//g tab

开启自动折行

:set wrap
:set nowrap # 关闭

使用拷贝的内容替换

比如本来准备写 User.GetAge(),结果写成了User.GetAgge(),这个时候可以去拷贝 GetAge(使用 yw,或者其他),然后移动到 GetAgge 上按 viwp

v 表示进入可视模式,iw 是 inner word,表示这个单词,然后 p 粘贴。

复制整个文件

ggyG 是比较通用的,还可以使用 :%y

搜索当前词

* 继续按查找下一个,# 查找上一个。

Vim 调试

https://alpha2phi.medium.com/neovim-debugging-application-70c525754064
https://alpha2phi.medium.com/neovim-dap-enhanced-ebc730ff498b

Vim 学习资料

Vim实战视频教程
https://edu.51cto.com/course/11219.html
https://github.com/sg552/happy_book_vim

Practice Vim

use vim as IDE
https://github.com/yangyangwithgnu/use_vim_as_ide

vim 自动命令

自动命令可以让 Vim 自动执行某些指定的命令,这些指定的命令会在某些事件发生的时候执行。

自动命令结构

autocmd BufNewFile,BufRead *.html :write 

BufNewFile 是要监听的事件
*.html 表示事件过滤的模式 pattern
:write 表示要执行的命令

参考
自动命令
https://zhuanlan.zhihu.com/p/98360630

为 vim + tmux 开启真彩色

验证终端是否支持真彩色

awk 'BEGIN{
    s="/\\/\\/\\/\\/\\"; s=s s s s s s s s;
    for (colnum = 0; colnum<77; colnum++) {
        r = 255-(colnum*255/76);
        g = (colnum*510/76);
        b = (colnum*255/76);
        if (g>255) g = 510-g;
        printf "\033[48;2;%d;%d;%dm", r,g,b;
        printf "\033[38;2;%d;%d;%dm", 255-r,255-g,255-b;
        printf "%s\033[0m", substr(s,colnum+1,1);
    }
    printf "\n";
}'

在终端执行上面的代码,如果显示不连续,则表示不支持真彩色。如果显示连续则支持真彩色。
不支持

支持

tmux 开启真彩色

tmux 2.2 之后开始支持真彩色,在 .tmux.conf 中添加如下内容

set -g default-terminal screen-256color
set-option -ga terminal-overrides ",*256col*:Tc" # 这句是关键

Vim 开启真彩色

vim >= 7.4.1770以及 neovim >= 0.2.2 都支持真彩色,但需要少许配置

if has("termguicolors")
    " fix bug for vim
    set t_8f=^[[38;2;%lu;%lu;%lum
    set t_8b=^[[48;2;%lu;%lu;%lum
    " enable true color
    set termguicolors
endif

其中 termguicolors 用来开启真彩色,前面两行用来解决 vim 的 bug(newvim 不需要)。其中 ^[ 代表 ESC 键,需要在 vim 中按 Ctrl-v ESC 来输入。
可以在 vim 中开启终端(vim8 或 neovim 中执行:terminal),执行上面的判断代码来验证。
参考
为 vim + tmux 开启真彩色(true color)

vim executable 方法

可以在 vim 里使用 help executable 查看使用说明
executable(expr) 检查传进去的参数是否存在,如果存在放回 1,它不会返回路径。

调试

日志级别

>= 1        读写 viminfo 文件时
>= 2         ":source" 一个文件时,通常是载入一个配置文件
>= 5        所有搜索到的 tag 文件和 include 文件
>= 8        执行到 autocommands 的文件
>= 9        每次执行到 autocommand 
>= 12        每次执行到 function 
>= 13        产生、捕获、结束处理、忽略一个异常时
>= 14        ":finally" 语句中等待的所有命令
>= 15        每一个执行到的 Ex 命令(截断到 200 字符)

https://harttle.land/2018/12/05/vim-debug.html