公众号关注 「奇妙的 Linux 世界」
设为「星标」,每天带你玩转 Linux !
导语:在日常工作中,我们或多或少要跟vim编辑器打交道。在大家的潜意识里,vim需要配置额外插件才能实现比较完整的代码编辑功能。实际上,只要通过合理利用vim的原生能力,就可以实现90%的编辑需求。本文旨在提供一些vim高效实用的技巧,同时加深我们对vim原生设计的理解。
下面,我们直接开始 本文将介绍以下几种常用能力
快速文件检索
符号跳转
代码自动补全
文件树浏览
自定义代码段
编译一体化
保命声明:
1. 没有反对使用插件的想法,很多第三方插件也非常实用
2. vim是宇宙第二编辑器,第一是大家正在使用的(手动狗头)
前置的基础配置
" 关闭vi兼容模式
set nocompatible
" 启用语法高亮
syntax enable
" 启用文件类型检测插件
filetype plugin on
1. 快速文件搜索(支持模糊查询)
代表插件
plugin | github |
---|---|
ctrlp | https://github.com/kien/ctrlp.vim |
fzf | https://github.com/junegunn/fzf.vim |
leaderf | https://github.com/Yggdroot/LeaderF |
telescope | https://github.com/nvim-telescope/telescope.nvim |
原生配置
" 递归查找子文件
set path+=**
" 展示match的文件列表
set wildmenu
开始施法
1、使用:find查找部分匹配文件
2、使用*实现模糊查询
3、使用:b搜索已打开的buffer文件
2. 符号跳转
代表工具/插件
基于字符匹配查找
Tools | Link |
---|---|
grep | https://www.gnu.org/software/grep/ |
ack | https://beyondgrep.com/ |
ag | https://github.com/ggreer/the_silver_searcher |
rg | https://github.com/BurntSushi/ripgrep |
功能对比:https://beyondgrep.com/feature-comparison/
性能对比:https://blog.burntsushi.net/ripgrep/
基于静态符号索引
Tools | Link |
---|---|
ctags | https://ctags.sourceforge.net/ |
gtags | https://www.gnu.org/software/global/ |
基于lsp(language server protocol)跳转
Plugins | Link |
---|---|
vim-lsp | https://github.com/prabirshrestha/vim-lsp |
LanguageClient-neovim | https://github.com/autozimu/LanguageClient-neovim |
coc-nvim | https://github.com/neoclide/coc.nvim |
nvim native lsp | https://neovim.io/doc/user/lsp.html |
原生操作
ctags是unix操作系统自带,且vim原生支持,故采用ctags静态符号索引
// 在项目根目录生成tags文件
ctags -R .
开始施法
keymap | operation |
---|---|
ctrl + [ | 跳转定义 |
ctrl + t | 返回 |
g + ctrl + [ | 查询多个模糊定义 |
一些补充
1、ctags只需要在项目根目录下生成tag文件,通过以下配置,可以在任意子目录下打打开
" 递归向上层寻找tags文件
set tags=tags;/
2、对于项目代码改动,可以使用 Guntentags 插件自动生成/更新并管理tags文件
3. 自动补全
常用插件
Header | Header |
---|---|
YouCompleteMe | https://github.com/ycm-core/YouCompleteMe |
coc-nvim | https://github.com/neoclide/coc.nvim |
nvim-cmp | https://github.com/hrsh7th/nvim-cmp |
原生支持,不需要额外配置
开始演示
1、使用 ctrl-x + ctrl-n,进行buffer补全
2、根据文件路径补全
3、根据tags符号补全
4. 文件树浏览
常用插件
Plugins | Link |
---|---|
NerdTree | https://github.com/preservim/nerdtree |
coc-explorer | https://github.com/weirongxu/coc-explorer |
nvim-tree.lua | https://github.com/nvim-tree/nvim-tree.lua |
原生配置
let g:netrw_banner=0 " 禁用没用的横幅
let g:netrw_winsize=25 " 初始窗口大小为25%
let g:netrw_liststyle=3 " 使用树状模式
let g:netrw_altv=1 " 分割窗口时默认在右边
let g:netrw_browse_split=4 " enter时在上一个窗口打开
简单演示
1、:vexplore打开文件树,enter以覆盖上一窗口打开,v以分割窗口打开
2、D删除文件,R重命名文件,其他操作可参考:help netrw
5. 自定义代码段
常用插件
Plugins | Github |
---|---|
vim-snippets | https://github.com/honza/vim-snippets |
ultisnips | https://github.com/SirVer/ultisnips |
LuaSnip | https://github.com/L3MON4D3/LuaSnip |
原生配置
以写markdown文件为例,我们可以利用vim keybind的配置来实现简单的snippets。
例如下面配置第3行表示,对于识别出的markdown文件,当我们在insert模式按下,b
,那么vim就会自动帮我们生成 **** <++>
代码,并且把光标移动 **|**
的中间。这样我们在写md时,就可以快速地使用加粗格式。
那可能有人会问:<++>
这串符号是做什么的?这是我从别人学到的一个非常巧妙的设计。我们可以将它理解为placeholder,当我们写完加粗的内容后,为了快速地移动下一个编辑区域,我们暂时在****
之外预埋了<++>
。配合配置第1行,按下,f
时vim会找到这一特定字符串<++>
删除并进入插入模式,从而免去了额外移动光标的操作。
autocmd Filetype markdown inoremap <buffer> ,f <Esc>/<++><CR>:nohlsearch<CR>"_c4l
autocmd Filetype markdown inoremap <buffer> ,n ---<Enter><Enter>
autocmd Filetype markdown inoremap <buffer> ,b **** <++><Esc>F*hi
autocmd Filetype markdown inoremap <buffer> ,s ~~~~ <++><Esc>F~hi
autocmd Filetype markdown inoremap <buffer> ,i ** <++><Esc>F*i
autocmd Filetype markdown inoremap <buffer> ,d `` <++><Esc>F`i
autocmd Filetype markdown inoremap <buffer> ,c ```<Enter><++><Enter>```<Enter><Enter><++><Esc>4kA
autocmd Filetype markdown inoremap <buffer> ,m - [ ]
autocmd Filetype markdown inoremap <buffer> ,p ![](<++>) <++><Esc>F[a
autocmd Filetype markdown inoremap <buffer> ,a [](<++>) <++><Esc>F[a
autocmd Filetype markdown inoremap <buffer> ,1 #<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> ,2 ##<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> ,3 ###<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> ,4 ####<Space><Enter><++><Esc>kA
autocmd Filetype markdown inoremap <buffer> ,l --------<Enter>
简单演示
6. 编译一体化
原生配置
" 可以根据使用的编译命令自行配置,默认包含make
set makeprg=xxxxx
开始演示
1、使用:make内置命令编译项目
2、使用 :cl 显示编译错误信息,:cc+数字 跳转编译错误位置,:cn/:cp 向前向后导航
7. 其他实用操作
场景1. 非文件所有者保存时提示没有权限,为了避免丢失原有的改动,可以使用以下命令。其中实现原理也很简单,可以参考:https://unix.stackexchange.com/questions/301256/how-does-w-sudo-tee-work
:w !sudo tee %
场景2. 使用
.
高效执行一些重复的操作
例如我们想快速删除one two wrong words three four中间隔的one two three four
,或者删除3个连续的单词3dw时,不想移动到数字区域,可以dw..
进行操作。
备注:想要更复杂的重复操作,可以使用宏录制
场景3. 关闭文件再重新打开时,无法撤回历史动作。以下配置可以实现持久化undo记录
set undofile
" 配置你的undo保存路径
set undodir=~/.vim/undodir
习惯4. 使用:x替代:wq,使用:qa替代每个窗口执行一次:q
还有相当多实用高效的操作,列举意义不大,大家感兴趣的话可以翻阅vim手册:https://yianwillis.github.io/vimcdoc/doc/usr_toc.html
参考链接
vim improved recommendations: https://www.vi-improved.org/recommendations/
thoughtbot course:https://thoughtbot.com/blog/how-to-do-90-of-what-plugins-do-with-just-vim
vim doc:https://www.vim.org/docs.php
Vim 8 中 C/C++ 符号索引:GTags 篇 : https://zhuanlan.zhihu.com/p/36279445
本文转载自:「鹅厂架构师」,原文:https://url.hi-linux.com/jlDHu,版权归原作者所有。欢迎投稿,投稿邮箱: editor@hi-linux.com。
最近,我们建立了一个技术交流微信群。目前群里已加入了不少行业内的大神,有兴趣的同学可以加入和我们一起交流技术,在 「奇妙的 Linux 世界」 公众号直接回复 「加群」 邀请你入群。
你可能还喜欢
点击下方图片即可阅读
Memos:一款开源的超高颜值备忘录,GitHub 星标 6.3k
点击上方图片,『美团|饿了么』外卖红包天天免费领
更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!