vim编辑器配置

都说vim编辑器十分强大,最近根据网上博主的文章配置了一下,现将配置过程中的注意事项记录一下。

vim配置主要有两个部分:配置vim配置文件、管理vim插件

1.vim配置文件需要自己在~/目录下新建一个名为.vimrc的文件(touch .vimrc),之后在其中写入vim的基本配置和插件配置,在配置文件中,可以使用双引号标记注释,如:

1 ”这是一行注释

 

2.vim插件放置在~/.vim/bundle/目录下,主要使用插件管理器Vundle管理。

vim插件安装时,需要在配置文件中写入插件配置,然后打开vim编辑器输入 :PluginInstall,之后vim会自动下载并安装插件。

vim自动下载插件时会从github上下载插件,但下载速度非常慢而且容易出错,所以可以在github找到相关插件后,复制地址,然后挂载到码云(https://gitee.com/)上,或者直接在码云上搜索相关插件(很多人已经挂载过来了),然后点击fork挂载到自己的仓库下,然后复制地址,在~/.vim/bundle/目录下执行指令:git clone 插件地址 ,就可以将插件下载下来了。之后打开vim,输入:PluginInstall ,就可以安装了,安装成功后会在vim下方显示:Done.

 

3.参考博客https://blog.csdn.net/zziahgf/article/details/49757185

                 https://blog.csdn.net/dreamdgl/article/details/79433975

4.问题解决

设置代码缩进

au BufNewFile,BufRead *.py
            \ set tabstop=4|
            \ set softtabstop=4|
            \ set shiftwidth=4|
            \ set textwidth=79|
            \ set expandtab|
            \ set autoindent|
            \ set fileformat=unix

au BufNewFile,BufRead *.js, *.html, *.css
            \ set tabstop=2|
            \ set softtabstop=2|
            \ set shiftwidth=2|

 

支持Virtualenv虚拟环境

开始时,将第二行py3 << EOF写为py << EOF,打开vim,报错,改为py3后正常

"python with virtualenv support---------------------------------------------
py3 << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
    project_base_dir = os.environ['VIRTUAL_ENV']
    activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))
EOF

 

添加配色方案

下载安装完插件,报错,Cannot find color scheme 'solarized',根据网上办法,先在~/.vim/目录下新建目录:mkdir colors,之后执行如下命令

cp ~/.vim/bundle/vim-colors-solarized/colors/solarized.vim ~/.vim/colors/

发现提示找不到Zenburn,遂将配置文件中的colorscheme Zenburn改为colorscheme zenburn,正常

Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'
if has('gui_running')
    set background=dark
    colorscheme solarized
else
    colorscheme zenburn
endif
call togglebg#map("<F5>")

 

5. GVIM,vim的界面版,安装后可以使用vim的所有配置,可以作为vim鼠标到键盘的过渡版本。gvim可以通过其界面上的菜单选项进行各种设置。

---

补充:

ctags安装:sudo apt install ctags

nodejs环境:ubuntu16.04/18.04配置nodejs环境,安装npm (需要安装16版本的nodejs,用以下指令替换原博客中的安装nodejs 10的指令

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
卸载原有yarn:
sudo apt remove cmdtest
安装yarn:
sudo npm install -g yarn

)

coc.nvim编译[参考:https://blog.csdn.net/qq_34548075/article/details/120092169]:

cd ~/.vim/plugged/coc.nvim/

yarn install

yarn build

[coc 插件:[VIM使用系列] coc.vim的安装和使用]

[上手Coc.nvim 完全指南]

[coc.nvim]: UnhandledRejection: Launching server “jedi” using command jedi-language-server failed:

 sudo pip3 install jedi-language-server

执行:whereis jedi-language-server

找到路径:jedi-language-server: /usr/local/bin/jedi-language-server

打开vim,输入: CocConfig打开coc-setting.json

输入jedi-language-server配置:

 "jedi.executable.command": "/usr/local/bin/jedi-language-server",
}

.vimrc文件


" VIM Setting
" ------ My Setting ------
filetype plugin indent on " 启用根据文件类型自动缩进
set autoindent " 开始新行时处理缩进
set smartindent " 智能缩进
set expandtab " 将制表符 Tab 展开为空格
set tabstop=4 " 要计算的空格数
set shiftwidth=4 " 用于自动缩进的空格数
set backspace=indent,eol,start " 开启退格跨行退格
set backspace=2 " 在多数终端上修正退格键Backspace的行为
set nu " 开启行数显示
" set relativenumber " 显示相对行数
set wrap " 自动换行
set wildmenu " 输入命令的时候Tab显示提示
" set mouse=a " 启用鼠标
set nobackup " 备份与缓存
set noswapfile " 不创建临时交换文件
set nowritebackup " 编辑时不需要备份文件
set hlsearch " 开启高亮显示结果
exec "nohlsearch"
set incsearch " 显示查找匹配过程
set cursorline "高亮显示当前
set smartcase
set showmatch " 高亮显示匹配的括号
filetype indent on " 针对不同的文件类型采用不同的缩进格式
filetype plugin on " 针对不同的文件类型加载对应的插件
filetype plugin indent on
filetype off
set autoread " 当文件在外部被修改,自动更新该文件
set clipboard+=unnamed " Vim 的默认寄存器和系统剪贴板共享
" 在3种模式下用3种光标
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_SR = "\<Esc>]50;CursorShape=2\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"


" --- gui ---
syntax enable
syntax on " 支持语法高亮显示
set showtabline=2 " 总是显示标签栏
set showcmd " 状态栏显示目前所执行的指令
set laststatus=2 " 开启状态栏信息
set cmdheight=1 " 命令行的高度
" colorscheme murphy " 修改配色
set guifont=Consolas:h12 " 设置字体
"不显示工具/菜单栏
set guioptions-=T "工具栏
"set guioptions-=m "菜单栏
set guioptions-=L "左边滚动条
set guioptions-=r "右边滚动条
set guioptions-=b " 底部滚动条
set guioptions-=e " 使用内置 tab 样式而不是 gui
" --- gui ---

"python with virtualenv support---------------------------------------------
py3 << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
project_base_dir = os.environ['VIRTUAL_ENV']
activate_this = os.path.join(project_base_dir,'bin/activate_this.py')
execfile(activate_this, dict(__file__=activate_this))
EOF


" ------ 插件 ------
set nocompatible " required
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'https://gitclone.com/github.com/scrooloose/nerdcommenter' "注释
Plugin 'https://ghproxy.com/https://github.com/clouduan/vim-searchme'
Plugin 'https://gitclone.com/github.com/jiangmiao/auto-pairs'
Plugin 'https://gitclone.com/github.com/junegunn/vim-easy-align' "对齐
Plugin 'https://gitclone.com/github.com/preservim/nerdtree' "目录树
Plugin 'https://gitclone.com/github.com/majutsushi/tagbar' "标签侧边栏
Plugin 'https://ghproxy.com/https://github.com/neoclide/coc.nvim', {'branch': 'release'} "补全
Plugin 'https://ghproxy.com/https://github.com/vim-airline/vim-airline'
Plugin 'https://ghproxy.com/https://github.com/vim-airline/vim-airline-themes'
Plugin 'https://gitclone.com/github.com/joshdick/onedark.vim' " onedark主题
Plugin 'https://gitclone.com/github.com/ervandew/supertab' "Tab插件
Plugin 'https://gitclone.com/github.com/dense-analysis/ale' " 多语言代码查错
Plugin 'https://ghproxy.com/https://github.com/luochen1990/rainbow' " 彩虹花括号
call vundle#end() " required
filetype plugin indent on " required
" ------ 插件 ------


" ------ 插件配置 ------
" let g:plugged#enable_at_startup = 1

" --- nerdtree 目录树快捷键 ---
map <C-n> :NERDTreeToggle<CR> " Ctrl+n快速开启目录树
" --- nerdtree 目录树快捷键 ---

" --- auto-pairs 自动补括号 ---
au Filetype FILETYPE let b:AutoPairs = {"(": ")"}
au FileType php let b:AutoPairs = AutoPairsDefine({'<?' : '?>', '<?php': '?>'})
" --- auto-pairs 自动补括号 ---

" --- nerdcommenter 配置 ---
" Add spaces after comment delimiters by default
let g:NERDSpaceDelims=1

" Use compact syntax for prettified multi-line comments
let g:NERDCompactSexyComs=1

" Align line-wise comment delimiters flush left instead of following code indentation
let g:NERDDefaultAlign='left'

" Set a language to use its alternate delimiters by default
let g:NERDAltDelims_java=1

" Add your own custom formats or override the defaults
let g:NERDCustomDelimiters={'c':{'left':'/**','right':'*/'}}

" Allow cddommenting and inverting empty lines (useful when commenting a region)
let g:NERDCommentEmptyLines=1

" Enable trimming of trailing whitespace when uncommenting
let g:NERDTrimTrailingWhitespace=1

" Enable NERDCommenterToggle to check all selected lines is commented or not
let g:NERDToggleCheckAllLines=1

map <C-m> <leader>cc
map <A-m> <leader>cu
unmap <CR>
" --- nerdcommenter 配置 ---

" --- 标签侧边栏 ---
nmap <F8> :TagbarToggle<CR>
" --- 标签侧边栏 ---

colorscheme onedark " 修改配色
" --- vim-airline-themes ---
"set ambiwidth=double " 设置为双字宽显示,否则无法完整显示如:☆
let g:airline_powerline_fonts = 1 "关于状态栏的配置
" 缺省自动匹配主题
let g:airline_theme='onedark'
let g:airline#extensions#tabline#enabled = 1 " Air-line 显示上面的buffer tab
let g:airline#extensions#ale#enabled = 1
let g:airline#extensions#coc#enabled = 1 "coc
let g:airline#extensions#tabline#left_sep = ' ' "tabline中未激活buffer两端的分隔字符
let g:airline#extensions#tabline#left_alt_sep = '|' "tabline中buffer显示编号
let g:airline#extensions#tabline#buffer_nr_show = 1
" --- vim-airline-themes ---

" --- ale ---
"始终开启标志列
let g:ale_sign_column_always = 1
let g:ale_set_highlights = 0
"自定义error和warning图标
let g:ale_sign_error = '❌'
let g:ale_sign_warning = '🔰'
"在vim自带的状态栏中整合ale
let g:ale_statusline_format = ['✗ %d', '⚡ %d', '✔ OK']
"显示Linter名称,出错或警告等相关信息
let g:ale_echo_msg_error_str = 'E'
let g:ale_echo_msg_warning_str = 'W'
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
"普通模式下,sp前往上一个错误或警告,sn前往下一个错误或警告
nmap sp <Plug>(ale_previous_wrap)
nmap sn <Plug>(ale_next_wrap)
"<Leader>s触发/关闭语法检查
nmap <Leader>s :ALEToggle<CR>
"<Leader>d查看错误或警告的详细信息
nmap <Leader>d :ALEDetail<CR>
"set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}\ %{ALEGetStatusLine()}
"cpp NOT C++
let g:ale_linters = {
\ 'cpp': ['gcc'],
\ 'c': ['gcc'],
\ 'python': ['pylint'],
\}

" --- ale ---

" --- rainbow ---
" 1. vscode defult 2. author defult 3. vscode show
" \ 'guifgs': ['#B21212', '#1B9CED','#FFFC00'],
" \ 'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick'],
" \ 'guifgs': ['#C186BF', '#268EDB','#F79318'],
let g:rainbow_conf = {
\ 'guifgs': ['#C186BF', '#268EDB','#F79318'],
\ 'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
\ 'operators': '_,_',
\ 'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/{/ end=/}/ fold'],
\ 'separately': {
\ '*': {},
\ 'tex': {
\ 'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/'],
\ },
\ 'lisp': {
\ 'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],
\ },
\ 'vim': {
\ 'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/', 'start=/{/ end=/}/ fold', 'start=/(/ end=/)/ containedin=vimFuncBody', 'start=/\[/ end=/\]/ containedin=vimFuncBody', 'start=/{/ end=/}/ fold containedin=vimFuncBody'],
\ },
\ 'html': {
\ 'parentheses': ['start=/\v\<((area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)[ >])@!\z([-_:a-zA-Z0-9]+)(\s+[-_:a-zA-Z0-9]+(\=("[^"]*"|'."'".'[^'."'".']*'."'".'|[^ '."'".'"><=`]*))?)*\>/ end=#</\z1># fold'],
\ },
\ 'css': 0,
\ }
\}
let g:rainbow_active = 1

" --- rainbow ---

" --- coc ---
" if hidden is not set, TextEdit might fail.
set hidden
" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup
" You will have bad experience for diagnostic messages when it's default 4000.
set updatetime=300
" don't give |ins-completion-menu| messages.
set shortmess+=c
" always show signcolumns
set signcolumn=yes
" Use tab for trigger completion with characters ahead and navigate.
" Use command ':verbose imap <tab>' to make sure tab is not mapped by other plugin.
" inoremap <silent><expr> <TAB> pumvisible() ? "<C-n>" :<SID>check_back_space() ? "<TAB>" : coc#refresh()
" inoremap <expr><S-TAB> pumvisible() ? "<C-p>" : "<C-h>"

function! s:check_back_space() abort
let col = col('.') - 1
return !col || getline('.')[col - 1] =~# 's'
endfunction
" Use <c-space> to trigger completion.
inoremap <silent><expr> <c-space> coc#refresh()
" Use <cr> to confirm completion, `<C-g>u` means break undo chain at current position.
" Coc only does snippet and additional edit on confirm.
inoremap <expr> <cr> pumvisible() ? "<C-y>" : "<C-g>u<CR>"
" Or use `complete_info` if your vim support it, like:
" inoremap <expr> <cr> complete_info()["selected"] != "-1" ? "<C-y>" : "<C-g>u<CR>"
" Use `[g` and `]g` to navigate diagnostics
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
" Remap keys for gotos
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
nmap <silent> gr <Plug>(coc-references)
" Use K to show documentation in preview window
nnoremap <silent> K :call <SID>show_documentation()<CR>
function! s:show_documentation()
if (index(['vim','help'], &filetype) >= 0)
execute 'h '.expand('<cword>')
else
call CocAction('doHover')
endif
endfunction
" Highlight symbol under cursor on CursorHold
autocmd CursorHold * silent call CocActionAsync('highlight')
" Remap for rename current word
nmap <leader>rn <Plug>(coc-rename)
" Remap for format selected region
xmap <leader>f <Plug>(coc-format-selected)
nmap <leader>f <Plug>(coc-format-selected)

augroup mygroup
autocmd!
" Setup formatexpr specified filetype(s).
autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
" Update signature help on jump placeholder
autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end
" Remap for do codeAction of selected region, ex: `<leader>aap` for current paragraph
xmap <leader>a <Plug>(coc-codeaction-selected)
nmap <leader>a <Plug>(coc-codeaction-selected)
" Remap for do codeAction of current line
nmap <leader>ac <Plug>(coc-codeaction)
" Fix autofix problem of current line
nmap <leader>qf <Plug>(coc-fix-current)
" Create mappings for function text object, requires document symbols feature of languageserver.
xmap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap if <Plug>(coc-funcobj-i)
omap af <Plug>(coc-funcobj-a)
" Use `:Format` to format current buffer
command! -nargs=0 Format :call CocAction('format')
" Use `:Fold` to fold current buffer
command! -nargs=? Fold :call CocAction('fold',<f-args>)
" use `:OR` for organize import of current buffer
command! -nargs=0 OR :call CocAction('runCommand','editor.action.organizeImport')
" --- coc ---



" ------ 插件配置 ------




" ------ My Setting ------`
 

 

posted @ 2020-07-19 00:37  BrianSun  阅读(816)  评论(0编辑  收藏  举报