总所周知,终端里使用vim只能Ctrl+o跳转到上一个位置,不能Ctrl+i跳转到下一个位置。因为xterm中tab和ctr+i都是一样,传递的键值是9,所以在vim中无法将tab和ctr_i分离使用。
这篇文章来解决这个问题。ctrl+g用于显示正在编辑的文件名,文件大小和位置信息,今天就牺牲ctrl+g原有的功能来实现ctrl+i的功能。有了状态栏插件以后ctrl+g原本的功能可有可无。以下以ubuntu中为例子。
下载vim源码
github打不开,这里从gitee上面下载:https://gitee.com/mirrors/vim/tags
修改源码
下载完后,打开目录有以下文件:
jek:~/Downloads$ cd vim-v9.0.2020/
jek:~/Downloads/vim-v9.0.2020$ ls
ci CONTRIBUTING.md LICENSE nsis READMEdir README.txt runtime src uninstall.txt vimtutor.com
configure Filelist Makefile pixmaps README.md README_VIM9.md SECURITY.md tools vimtutor.bat
找到src/nv_cmds.h文件,里面定义了按键与相应处理函数的映射。将Ctrl_G那一行的nv_ctrlg函数改成nv_pcmark函数后保存。
#endif // DO_DECLARE_NVCMD
{
NVCMD(NUL, nv_error, 0, 0),
NVCMD(Ctrl_A, nv_addsub, 0, 0),
NVCMD(Ctrl_B, nv_page, NV_STS, BACKWARD),
NVCMD(Ctrl_C, nv_esc, 0, TRUE),
NVCMD(Ctrl_D, nv_halfpage, 0, 0),
NVCMD(Ctrl_E, nv_scroll_line, 0, TRUE),
NVCMD(Ctrl_F, nv_page, NV_STS, FORWARD),
/* NVCMD(Ctrl_G, nv_ctrlg, 0, 0), */
NVCMD(Ctrl_G, nv_pcmark, 0, 0),
NVCMD(Ctrl_H, nv_ctrlh, 0, 0),
NVCMD(Ctrl_I, nv_pcmark, 0, 0),
NVCMD(NL, nv_down, 0, FALSE),
NVCMD(Ctrl_K, nv_error, 0, 0),
NVCMD(Ctrl_L, nv_clear, 0, 0),
NVCMD(CAR, nv_down, 0, TRUE),
NVCMD(Ctrl_N, nv_down, NV_STS, FALSE),
NVCMD(Ctrl_O, nv_ctrlo, 0, 0),
NVCMD(Ctrl_P, nv_up, NV_STS, FALSE),
编译vim
编译之前需安装依赖库,python之前已经安装了这里不再展示。
jek:~/Downloads/vim-v9.0.2020$ sudo apt install libncurses5-dev
jek:~/Downloads/vim-v9.0.2020$ sudo apt install libgtk2.0-dev libatk1.0-dev libcairo2-dev libx11-dev libxpm-dev libxt-dev
正式编译:
jek[2023-11-10 19:49]:~/Downloads/vim-v9.0.2020$ make
Starting make in the src directory.
If there are problems, cd to the src directory and run make there
cd src && make first
make[1]: Entering directory '/home/jek/Downloads/vim-v9.0.2020/src'
rm -f auto/config.status auto/config.cache config.log auto/config.log
rm -f auto/config.h auto/link.log auto/link.sed auto/config.mk
touch auto/config.h
cp config.mk.dist auto/config.mk
cd testdir; make -f Makefile clean
make[2]: Entering directory '/home/jek/Downloads/vim-v9.0.2020/src/testdir'
......
......
卸载vim
如果安装了vim先将vim删掉,~/.vimrc
文件和~/.vim
文件夹不需要删除。
sudo apt remove vim
安装vim
默认安装到/usr/local/share/vim/vim90
和/usr/local/bin
目录下。
jek:~/Downloads/vim-v9.0.2020$ sudo make install
# 创建链接
jek:/usr/local/bin$ sudo ln /usr/local/bin/vim /usr/bin/vim
然后就可以使用vim了。至此,可以用ctrl+g代替原来的ctrl+i完成跳转到下一个位置的功能。
参考:
https://zhuanlan.zhihu.com/p/46701273
https://www.cnblogs.com/JoiT/p/build_from_source_for_linux_vim.html