Linux神器——Vim 作者:pandali 时间:2021年09月17日 分类:计算机技术,Linux 字数:4959 warning: 这篇文章距离上次修改已过282天,其中的内容可能已经有所变动。 Vim是从 vi 发展出来的一个文本编辑器  ### 升级 [官方资源](https://github.com/vim/vim "官方资源") 选择需要的release包 github 太慢?[参考这个改一下吧](https://blog.xskymm.com/archives/131/ "参考这个改一下吧") ``` wget https://github.com/vim/vim/archive/v8.1.1766.tar.gz tar -zxvf v8.1.1766.tar.gz # 进行安装,通过configure配置安装路径 cd vim-8.1.1766/ ./configure --prefix=/usr/local&&make && make install #利用alias将vim指令定向到刚刚安装的vim8,同时修改.bashrc确保之后一直能生效 alias vim='/usr/local/bin/vim' echo "alias vim='/usr/local/bin/vim' " >> ~/.bashrc #最后检查安装是否成功 vim -version ``` ### 块注释 ##### 批量注释: Ctrl + v 进入块选择模式,然后移动光标选中你要注释的行,再按大写的 I 进入行首插入模式输入注释符号如 // 或 #,输入完毕之后,按两下 ESC,Vim 会自动将你选中的所有行首都加上注释,保存退出完成注释。 ##### 取消注释: Ctrl + v 进入块选择模式,选中你要删除的行首的注释符号,注意 // 要选中两个,选好之后按 d 即可删除注释 ### 插件安装(一) ##### 1.登场的插件是管理VIM插件的插件Vundle 下载Vundle ``` git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim ``` 配置Vundle 编辑~/.vimrc文件,内容如下: ``` set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'gmarik/Vundle.vim' " The following are examples of different formats supported. " Keep Plugin commands between vundle#begin/end. " plugin on GitHub repo (github上的插件配置方案,tpope为用户名,vim-fugitive为repo名) Plugin 'tpope/vim-fugitive' " plugin from http://vim-scripts.org/vim/scripts.html(vim官方插件配置方法) Plugin 'L9' " Git plugin not hosted on GitHub(没有托管到github上的插件配置方法) Plugin 'git://git.wincent.com/command-t.git' " git repos on your local machine (i.e. when working on your own plugin) Plugin 'file:///home/gmarik/path/to/plugin' " The sparkup vim script is in a subdirectory of this repo called vim. " Pass the path to set the runtimepath properly. Plugin 'rstacruz/sparkup', {'rtp': 'vim/'} " Avoid a name conflict with L9 Plugin 'user/L9', {'name': 'newL9'} " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " To ignore plugin indent changes, instead use: "filetype plugin on " " Brief help " :PluginList - list configured plugins " :PluginInstall(!) - install (update) plugins " :PluginSearch(!) foo - search (or refresh cache first) for foo " :PluginClean(!) - confirm (or auto-approve) removal of unused plugins " " see :h vundle for more details or wiki for FAQ " Put your non-Plugin stuff after this line ``` 精简版 ``` set nocompatible filetype off set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() Plugin 'gmarik/Vundle.vim' " 安装插件 放在这里 call vundle#end() filetype plugin indent on ``` ##### 2.添加您的第一个插件 使用vim打开~/.vimrc,在call vundle#end()之前一行增加Plugin 'The-NERD-tree',然后执行Esc :w,保存文件。 此时只是在配置文件中增加了插件配置,而真正的插件还未安装,您还需要在vim命令模式执行:PluginInstall,当您看到最下面一行的Done!时才完成插件的安装 Vundle插件还支持下面的命令: ``` :PluginList - 列出已经配置的插件 :PluginInstall(!) - 安装 (更新) 插件 :PluginSearch(!) foo - 查询foo插件 :PluginClean(!) - 清除已下载但未配置的插件 ``` ##### 3.使用 VIM命令模式输入 ``` :NERDTree ``` ### 插件安装(二) ##### 插件管理软件 vim-plug 下载vim-plug 一定要下载 release包后cp过去,否则可能会出现意想不到的情况 ``` wget https://github.com/junegunn/vim-plug/archive/refs/tags/0.10.0.zip unzip vim-plug-0.10.0.zip mkdir -p ~/.vim/autoload/ cp plug.vim ~/.vim/autoload/plug.vim ``` ##### 安装插件 编辑 ~/.vimrc 文件中的内容,比如安装“lightline.vim” 插件 ``` call plug#begin('~/.vim/plugged') Plug 'itchyny/lightline.vim' call plug#end() ``` 运行命令重新加载: ``` 在vim 命令行模式下,执行完使用 :wq退出 :source ~/.vimrc ``` #####插件的安装和卸载: ``` 打开 vim 使用命令 :PlugInstall 打开 vim 使用命令 :PlugClean ``` ### 常用插件 ##### nerdtree [官方介绍](https://github.com/preservim/nerdtree "官方介绍") 使用vim时调用树形文档结构 [快捷键介绍](https://blog.xskymm.com/archives/135/ "快捷键介绍") Vim-Plug 安装: ``` call plug#begin() Plug 'preservim/nerdtree' # 自动开启树 autocmd VimEnter * NERDTree call plug#end() ``` ##### phpcd [官方介绍](https://github.com/epii1/phpcd.vim "官方介绍") phpcd 会自动解析当前打开nvim位置的composer.json, 解析出psr-4的自动加载。如果你的框架使用了非composer的自动加载,就需要改造 比如,框架将 App 这个 namespace 指向到了 application/ 目录下, 正常的做法是 composer.json 加上如下块 ``` { ... "autoload": { "psr-4": { "App\\": "application/" } } } ``` Vim-Plug 安装: ``` call plug#begin() Plug 'lvht/phpcd.vim', { 'for': 'php', 'do': 'composer install' } call plug#end() ``` ##### tagbar [官方介绍](https://github.com/preservim/tagbar "官方介绍") tagbar是一个taglist的替代品,比taglist更适合c++使用,函数能够按类区分,支持按类折叠显示等,显示结果清晰简洁 Vim-Plug 安装: ``` Plugin 'majutsushi/tagbar' ``` 插件依赖 ctags,安装命令: ``` #debian apt install ctags -y #centos yum install ctags -y ```