0%

VIM配置

公司的换皮vscode是真鸡儿难用啊,还不如自己配vim!

基本配置

~/vim.rc下配置缩进和高亮:

set nocompatible "do not compatible with vi

set number! "show line number
set expandtab
set autoindent "set auto indent
set cindent "set clang auto indent
set shiftwidth=4
set softtabstop=4
set tabstop=4

syntax on "set syntax highlight
autocmd BufNewFile,BufRead *.c set syntax=c

set cursorline "highlight current line
set showmatch "highlight bracket matching
set showmode "show current op mode at bottom
set bg=dark "set background color

set hlsearch "highlight search result

vim-plug

vim-plugin是一款vim的插件管理器。

安装:将plug.vim下载到~/.vim/autoload

curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

编辑~/.vimrc,在plug#begin([PLUGIN_DIR])call plug#end()之间添加插件:
call plug#begin()
" The default plugin directory will be as follows:
" - Vim (Linux/macOS): '~/.vim/plugged'
" - Vim (Windows): '~/vimfiles/plugged'
" - Neovim (Linux/macOS/Windows): stdpath('data') . '/plugged'
" You can specify a custom plugin directory by passing it as the argument
" - e.g. `call plug#begin('~/.vim/plugged')`
" - Avoid using standard Vim directory names like 'plugin'

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-default branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above)
Plug 'fatih/vim-go', { 'tag': '*' }

" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }

" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'

" Initialize plugin system
call plug#end()

vim中执行:PlugInstall安装插件,:PlugUpdate更新插件

coc.vim

coc.vim是另一款node作为后端的vim插件。

安装

使用vim-plug安装:

" coc.vim
Plug 'neoclide/coc.nvim', { 'branch': 'master', 'do': 'yarn install --frozen-lockfile' }

vim执行:PluginInstall

extensions

vim.coc安装各类语言对应的服务器插件,可以在Using-coc-extensions找到vim.coc支持的插件列表。

进入vim后在命令模式下用CocInstall [extension-name]来安装插件,例如:CocInstall coc-sh安装shell脚本插件,安装完毕后前往~/.vim/coc-settings.json或者在vim中执行CocConfig配置插件,插件的信息配置在languageserver下:

"languageserver": {
"bash": {
"command": "bash-language-server",
"args": ["start"],
"filetypes": ["sh"]
}
}

语言插件往往依赖开源语言服务协议(Language Server Protocal),

LSP(Language-Server-Protocol)开源的语言服务器协定。由红帽、微软和 Codenvy 联合推出,可以让不同的程序编辑器与集成开发环境(IDE)方便嵌入各种程序语言,允许开发人员在最喜爱的工具中使用各种语言来撰写程序。

唯一基于JSON的语言服务器数据交换协定,目前由GitHub代管,并采用CC及MIT授权。该协定主要用来促进编辑器及语言服务器之间的互动,允许开 发人员在各种编辑器或整合开发环境中存取智慧型的程序语言工具,像是以符号搜寻、语法分析、自动完成代码、移至定义、描绘轮廓或重构等。

例如shell的开源语言服务器:bash-language-server

sudo npm install -g bash-language-server
安装完成后命令行中运行which bash-language-server`检查安装。

C/C++ & CMake

CocInstall coc-clangd
CocInstall coc-cmake

需要依赖clangdcmake-language-server

"languageserver": {
"clangd": {
"command": "clangd",
"rootPatterns": ["compile_flags.txt", "compile_commands.json"],
"filetypes": ["c", "cc", "cpp", "c++", "objc", "objcpp"]
},
"cmake": {
"command": "cmake-language-server",
"filetypes": ["cmake"],
"rootPatterns": ["build/"],
"initializationOptions": {
"buildDirectory": "build"
}
}
}

Rust

CocInstall coc-rust-analyzer
需要依赖rust-analyzer

"languageserver": {
"rust": {
"command": "rust-analyzer",
"filetypes": ["rust"],
"rootPatterns": ["Cargo.toml"]
}
}

JSON

CocInstall coc-json

"json.enable": true, 
"json.format.enable": true

Markdown

CocInstall coc-webview
CocInstall coc-markdown-preview-enhanced
开启webview preview:
CocCommand markdown-preview-enhanced.openPreview

Markup

CocInstall coc-markmap
生成思维导图:
CocCommand markmap.create

explorer

CocInstall coc-explorer
文件浏览器

新文件模板

~/.vimrc中写入:

autocmd BufNewFile *.html 0r ~/.vim/template/template.html

" call setTitle() automatically when create .h .c .hpp .cpp .sh .py
autocmd BufNewFile *.[ch],*.hpp,*.cpp,*.sh,*.py exec ":call SetTitle()"
" add comment
func SetComment()
call setline(1,"/*================================================================")
call append(line("."), "* Copyright (C) ".strftime("%Y")." XUranus All rights reserved.")
call append(line(".")+1, "* ")
call append(line(".")+2, "* File: ".expand("%:t"))
call append(line(".")+3, "* Author: XUranus")
call append(line(".")+4, "* Date: ".strftime("%Y-%m-%d-"))
call append(line(".")+5, "* Description: ")
call append(line(".")+6, "*")
call append(line(".")+7, "================================================================*/")
call append(line(".")+8, "")
call append(line(".")+9, "")
endfunc
" comment for *.sh, *.py
func SetComment_script()
call setline(3,"#*================================================================")
call setline(4, "#* Copyright (C) ".strftime("%Y")." XUranus All rights reserved.")
call setline(5, "#* ")
call setline(6, "#* File: ".expand("%:t"))
call setline(7, "#* Author: XUranus")
call setline(8, "#* Date: ".strftime("%Y-%m-%d-"))
call setline(9, "#* Description: ")
call setline(10, "#*")
call setline(11, "#================================================================*/")
call setline(12, "")
call setline(13, "")
endfunc

" auto insert to head of file
func SetTitle()
if &filetype == 'sh'
call setline(1,"#!/usr/bin/bash")
call setline(2,"")
call SetComment_script()
elseif &filetype == 'python'
call setline(1,"#!/usr/bin/python")
call setline(2,"")
call SetComment_script()
else
call SetComment()
if expand("%:e") == 'hpp'
call append(line(".")+10, "#ifndef _".toupper(expand("%:t:r"))."_H")
call append(line(".")+11, "#define _".toupper(expand("%:t:r"))."_H")
call append(line(".")+12, "#ifdef __cplusplus")
call append(line(".")+13, "extern \"C\"")
call append(line(".")+14, "{")
call append(line(".")+15, "#endif")
call append(line(".")+16, "")
call append(line(".")+17, "#ifdef __cplusplus")
call append(line(".")+18, "}")
call append(line(".")+19, "#endif")
call append(line(".")+20, "#endif //".toupper(expand("%:t:r"))."_H")

elseif expand("%:e") == 'h'
call append(line(".")+10, "#pragma once")
elseif &filetype == 'c'
call append(line(".")+10,"#include \"".expand("%:t:r").".h\"")
elseif &filetype == 'cpp'
call append(line(".")+10, "#include \"".expand("%:t:r").".h\"")
endif
endif
endfunc

创建新文件时会自动创建模板

Disqus评论区没有正常加载,请使用科学上网