Vim基础配置

纯粹个人记录备份而已,如有错误,欢迎指正,更新于2018/5/13

UNIX like系统下用得比较多的编辑器主要是vimemacs,分别有“编辑器之神”和“神的编辑器”的美誉,emacs比较庞大,扩展性强,有人说它是“伪装成编辑器的操作系统”,而vim则是轻量级的编辑器,对于日常的开发维护非常方便,因此接下来要说的也主要是如何将vim打造成一个轻量级的IDE。

安装vim

首先,需要安装vim和gvim(vim的GUI版),我的系统是Debian,如果你的是其他Linux发行版的系统的话,包名可能会不一样要做适当更改,在终端执行($表示普通用户,#表示超级用户)

$ sudo apt-get install vim vim-gtk

基本配置

安装完成以后,首次打开gvim时,你可能觉得它异常简陋,连行号都不能显示出来,因此需要做些必要的修改使它更好用一些,这个可以通过修改/etc/vim/vimrc(在不同的Linux发行版上全局vimrc的位置也不一样,比如OpenSUSE的在/etc/vimrc)这个文件来实现(需要root权限),以下是我的配置,主要针对Linux,windows用户需要做一些修改

"vimrc文件修改之后自动加载, Unix
autocmd! bufwritepost .vimrc source %
"设置默认文件编码为utf-8
set encoding=utf-8
set fileencoding=utf-8
"自动判断编码时,依次尝试以下编码
set fileencodings=utf-8,ucs-bom,gbk,cp936,gb2312,gb18030
"设置字体为Source Code Pro
"set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 13
"set guifont=Powerline\ Consolas\ 13
set guifont=Source\ Code\ Pro\ for\ Powerline\ 13
"修改Leader键为空格键
let mapleader = "\<Space>"
let g:mapleader = "\<Space>"
"设置backspace行为
set backspace=indent,eol,start 
"显示行号
set number
"显示标尺
set ruler
"关闭自动备份
set nobackup
"set noswapfile
"在未保存时允许切换缓冲区
set hidden 
"开启语法高亮
syntax enable 
syntax on
"关闭兼容模式
set nocompatible
"设置命令行模式下的最大历史记录数
set history=200
"设置以unix的格式保存文件
set fileformat=unix    
"高亮当前行
set cursorline
"高亮当前列
set cursorcolumn
"始终显示状态栏
set laststatus=2
"只在需要时重绘窗口
set lazyredraw    
"使屏幕显示更流畅
set ttyfast
"命令行补全参数
set wildmenu
"设置tab键空4set tabstop=4
"将Tab自动转化成空格(需要输入真正的Tab键时,使用+Tab),set noet快速关闭这个选项
set expandtab
"退格键一次删掉4个空格
set smarttab
"可视化不可见字符
set listchars=tab:▸\ ,trail:¬,extends:>,precedes:<
set nolist
"空格显示为下划线,tab键不会显示下划线,只对GUI有效
"autocmd ColorScheme * highlight WhiteSpaces gui=undercurl guifg=LightGray | match WhiteSpaces / \+/ 
"实时搜索
set incsearch
"高亮搜索结果,:noh取消高亮
set hlsearch
"忽略大小写
set ignorecase
"有一个或以上大写字母时仍大小写敏感
set smartcase
"打开正则表达式
set magic
"搜索模式为默认更先进的正则规则
nnoremap / /\v
vnoremap / /\v
"设置忽略的文件
set wildignore=*.o,*.swp,*.bak,*.pyc,*.pyo,*.class,*.zip
set wildignore+=*/.git/*
"显示匹配的括号
set showmatch
"显示当前所处模式
set showmode
"设置右下角显示当前光标位置
set showcmd
set background=dark
"为GUI和Terminal添加不同的设置
set t_Co=256
if has('gui_running')
    colorscheme onedark
    :cd Projects
else
    colorscheme Tomorrow-Night-Eighties
endif
"禁止自动折行
set nowrap
"设置当文件被改动时自动载入
set autoread
"设置退出插入模式后自动保存
set autowrite
"当前目录随着被编辑文件的改变而改变
set autochdir
"允许插件  
filetype plugin on
"共享剪贴板  
set clipboard+=unnamed
"关闭输入错误的提示声音
set noerrorbells
set novisualbell
set t_vb=
set tm=500
"禁止光标闪烁
set gcr=a:block-blinkon0
"在处理未保存或只读文件的时候,弹出确认
set confirm
"上下移动光标时光标上方和下方至少保留两行
set scrolloff=2
"开启自动缩进,智能缩进
set autoindent
set cindent
set smartindent
set shiftwidth=4
"80个字符处显示分隔符
set colorcolumn=80  
"set textwidth=79
"合并两行中文时,不在中间加空格
set formatoptions+=B
"为特定文件类型载入相关缩进文件
filetype indent on
"gvim自动全屏,vim则设置窗口默认大小为80*32
if has("gui_running")
  set lines=999 columns=999
else
  if exists("+lines")
    set lines=35
  endif
  if exists("+columns")
    set columns=86
  endif
endif
"自动检测文件类型
filetype plugin indent on
"设置显示消息的列数
set cmdheight=2
"隐藏工具栏
set guioptions-=T
"Ctrl+T新建标签页
nnoremap      :tabnew
inoremap  :tabnew
"修改分屏移动快捷键
map  j
map  k
map  h
map  l
"W删除当前文件中的空格
"nnoremap W :%s/\s\+$//:let @/=''
"设置插入模式和可视模式下的退出键
inoremap jj :w
vnoremap jj 
"全选并复制到系统剪贴板
nmap a ggVG"+y
"Y复制,P粘帖
map Y "+y
map P "+p
"q关闭当前buffer
nnoremap q :bd 
"m和n进行Buffer切换
nnoremap m :bp
nnoremap n :bn
"快捷缩进
vnoremap <  >v
"禁止通过方向键进行移动
"noremap <LEFT> 
"noremap  
"noremap  
"noremap  
"禁止使用鼠标
"set mouse-=a
"设置状态栏显示的内容
set statusline=%F%m%r%h%w\ [FORMAT=%{&ff}]\ [TYPE=%Y]\ [POS=%l,%v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"自动插入文件头
autocmd BufNewFile *.cpp,*.cc,*.c,*h,*.sh,*.py exec ":call SetHeader()" 
func! SetHeader() 
    if expand("%:e") == 'sh'
        call setline(1,"\#!/bin/bash") 
        call append(line("."), "") 
    elseif expand("%:e") == 'py'
        call setline(1,"#!/usr/bin/env python3")
        call append(line(".")+1, "") 
    elseif expand("%:e") == 'cpp' 
        call setline(1,"#include ") 
        call setline(2, "")
        call setline(3, "using std::cin;") 
        call setline(4, "using std::cout;") 
        call setline(5, "using std::endl;") 
        call setline(6, "")
    elseif expand("%:e") == 'cc' 
        call setline(1,"#include ") 
        call setline(2, "")
        call setline(3, "using std::cin;") 
        call setline(4, "using std::cout;") 
        call setline(5, "using std::endl;") 
        call setline(6, "")
    elseif expand("%:e") == 'c'
        call setline(1,"#include ") 
        call setline(2,"#include ")
        call setline(3,"")
    elseif expand("%:e") == 'h'
        call setline(1, "#ifndef ".toupper(expand("%:r"))."_H")
        call setline(2, "#define ".toupper(expand("%:r"))."_H")
        call setline(3,"")
        call setline(4, "#endif")
    endif
endfunc 
autocmd BufNewFile * normal G
"打开一个文件自动定位到上一次退出时的位置
if has("autocmd")
    au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif
"打开或者新建markdown文件时,手动启用语法高亮并设置自动折行
autocmd BufRead,BufNewFile *.md,*markdown syntax manual
autocmd BufRead,BufNewFile *.md,*markdown setlocal wrap
autocmd BufWritePre *c,*h,*.cpp,*.hpp,*py setlocal fileformat=unix
"F12放大字体,Shift+F12缩小字体
if has("unix")
    function! FontSizePlus ()
      let l:gf_size_whole = matchstr(&guifont, '\( \)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole + 1
      let l:new_font_size = ' '.l:gf_size_whole
      let &guifont = substitute(&guifont, ' \d\+$', l:new_font_size, '')
    endfunction

    function! FontSizeMinus ()
      let l:gf_size_whole = matchstr(&guifont, '\( \)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole - 1
      let l:new_font_size = ' '.l:gf_size_whole
      let &guifont = substitute(&guifont, ' \d\+$', l:new_font_size, '')
    endfunction
else
    function! FontSizePlus ()
      let l:gf_size_whole = matchstr(&guifont, '\(:h\)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole + 1
      let l:new_font_size = ':h'.l:gf_size_whole
      let &guifont = substitute(&guifont, ':h\d\+$', l:new_font_size, '')
    endfunction

    function! FontSizeMinus ()
      let l:gf_size_whole = matchstr(&guifont, '\(:h\)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole - 1
      let l:new_font_size = ':h'.l:gf_size_whole
      let &guifont = substitute(&guifont, ':h\d\+$', l:new_font_size, '')
    endfunction
endif
if has("gui_running")
    nmap  :call FontSizeMinus()
    nmap  :call FontSizePlus()
endif

安装插件

使用vim-plug管理器,具体安装方法参考官网说明

"be iMproved, required
set nocompatible
"required              
filetype off                  

"Specify a directory for plugins
call plug#begin('~/.vim/plugged')

Plug 'w0rp/ale'
Plug 'tpope/vim-surround'
Plug 'tpope/vim-dispatch'
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-commentary'
Plug 'tpope/vim-abolish'
Plug 'tpope/vim-dadbod'
Plug 'Chiel92/vim-autoformat'
Plug 'godlygeek/tabular'
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'xuyuanp/nerdtree-git-plugin'
Plug 'majutsushi/tagbar', { 'on': 'TagbarToggle' }
Plug 'Yggdroot/indentLine'
Plug 'kshenoy/vim-signature'
Plug 'jiangmiao/auto-pairs'
Plug 'mbbill/undotree', { 'on': 'UndotreeToggle' }
Plug 'ap/vim-css-color'
Plug 'sheerun/vim-polyglot'
Plug 'octol/vim-cpp-enhanced-highlight'
Plug 'lyuts/vim-rtags', { 'for': ['c', 'cpp'] }
Plug 'derekwyatt/vim-fswitch'
Plug 'tyru/open-browser.vim'
Plug 'elzr/vim-json'
Plug 'junegunn/goyo.vim'
Plug 'junegunn/limelight.vim'
Plug 'ctrlpvim/ctrlp.vim'
Plug 'mileszs/ack.vim'
Plug 'easymotion/vim-easymotion'
Plug 'honza/vim-snippets'
Plug 'SirVer/ultisnips'
Plug 'mattn/emmet-vim', { 'for': 'html' }
Plug 'fatih/vim-go', { 'for': 'go' }
Plug 'pangloss/vim-javascript'
Plug 'suan/vim-instant-markdown'
Plug 'Valloric/YouCompleteMe'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'chrisbra/unicode.vim'
"Plug 'terryma/vim-multiple-cursors'
"Plug 'rust-lang/rust.vim'
"Plug 'derekwyatt/vim-scala'
"Plug 'python-mode/python-mode'
"Plug 'nacitar/a.vim'
"Plug 'metakirby5/codi.vim'
"Plug 'ruben2020/codequery'
"Plug 'janko-m/vim-test'
"Plug 'google/vim-codefmt'
"Plug 'christoomey/vim-tmux-navigator'
"Plug 'szw/vim-ctrlspace'
"Plug 'tpope/vim-endwise'
"Plug 'nsf/gocode'
"Plug 'vim-ruby/vim-ruby'
"Plug 'tpope/vim-rails'
"Plug 'ekalinin/dockerfile.vim'
"Plug 'othree/html5.vim'
"Plug 'vim-perl/vim-perl'
"Plug 'wellle/targets.vim'
"Plug 'kana/vim-textobj-user'
"Plug 'rhysd/vim-grammarous'
"Plug 'reedes/vim-wordy'
"Plug 'reedes/vim-litecorrect'
"Plug 'plasticboy/vim-markdown'
"Plug 'mzlogin/vim-markdown-toc'

"color scheme
Plug 'rafi/awesome-vim-colorschemes'
Plug 'lifepillar/gruvbox8'
Plug 'chriskempson/vim-tomorrow-theme'
Plug 'sickill/vim-monokai'
Plug 'mbbill/vim-seattle'
Plug 'sjl/badwolf'
Plug 'jnurmine/zenburn'
let g:seoul256_background = 235
Plug 'kadekillary/subtle_solo'
Plug 'tyrannicaltoucan/vim-quantum'
Plug 'mhinz/vim-janah'
"Plug 'AlessandroYorba/Despacio'
"Plug 'nlknguyen/papercolor-theme'
"Plug 'reedes/vim-colors-pencil'
"Plug 'dikiaap/minimalist'
"Plug 'zeis/vim-kolor'
"Plug 'chriskempson/base16-vim'

"all of your Plugins must be added before the following line
call plug#end()  

"required          
filetype plugin indent on 

以下内容作为存档

安装插件

vim虽然扩展性不如emacs,但也有不少插件使得你可以更方便的编码,首先你需要安装一个插件管理器方便你管理vim的插件,常见的有Vundle、vim-plug和pathogen,Linux下推荐使用Vundle,执行下面的指令进行安装,注意不要在root权限下执行这一句,因为那样做的话你安装的插件只有在你以root身份运行vim时才会被加载

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

然后在~目录下创建.vimrc文件

$ vim ~/.vimrc

并写入以下内容

"be iMproved, required
set nocompatible
"required              
filetype off                  


"set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim


"keep Plugin commands between vundle#begin/end.
call vundle#begin()

"plugins
"let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'suan/vim-instant-markdown'
Plugin 'w0rp/ale'
Plugin 'nvie/vim-flake8'
Plugin 'tpope/vim-fugitive'
Plugin 'tpope/vim-surround'
Plugin 'tpope/vim-commentary'
Plugin 'tpope/vim-repeat'
Plugin 'Chiel92/vim-autoformat'
Plugin 'elzr/vim-json'
Plugin 'scrooloose/nerdtree'
Plugin 'kshenoy/vim-signature'
Plugin 'majutsushi/tagbar'
Plugin 'jiangmiao/auto-pairs'
Plugin 'ap/vim-css-color'
Plugin 'luochen1990/rainbow'
Plugin 'sheerun/vim-polyglot'
Plugin 'octol/vim-cpp-enhanced-highlight'
Plugin 'thaerkh/vim-workspace'
Plugin 'Yggdroot/indentLine'
Plugin 'godlygeek/tabular'
Plugin 'tyru/open-browser.vim'
Plugin 'vim-airline/vim-airline'
Plugin 'ryanoasis/vim-devicons'
Plugin 'ctrlpvim/ctrlp.vim'
Plugin 'keith/investigate.vim'
Plugin 'honza/vim-snippets'
Plugin 'SirVer/ultisnips'
Plugin 'Valloric/YouCompleteMe'
"Plugin 'google/vim-codefmt'
"Plugin 'easymotion/vim-easymotion'
"Plugin 'scrooloose/syntastic'
"Plugin 'tpope/vim-endwise'
"Plugin 'mattn/emmet-vim'
"Plugin 'sjl/gundo.vim'
"Plugin 'lervag/vimtex'
"Plugin 'mzlogin/vim-markdown-toc'
"Plugin 'mhinz/vim-startify'
"Plugin 'scrooloose/nerdcommenter'
"Plugin 'nathanaelkane/vim-indent-guides'
"Plugin 'junegunn/vim-easy-align'


"color scheme
"Plugin 'flazz/vim-colorschemes'

"all of your Plugins must be added before the following line
call vundle#end()  


"required          
filetype plugin indent on    
"To ignore plugin indent changes, instead use:
"filetype plugin on


"Brief help
":PluginList       - lists configured plugins
":PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
":PluginSearch foo - searches for foo; append `!` to refresh local cache
":PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal

"see :h vundle for more details or wiki for FAQ
"Put your non-Plugin stuff after this line

保存退出后,重新打开vim,并在普通模式下输入

:PluginInstall

网上有些说是:BundleInstall进行安装,这个是老版的,现在安装的Vundle都是用:PluginInstall安装插件的,下面简单介绍下各插件的功能
- vim-instant-markdown,markdown实时预览
- ale,实时语法检查,Vim 8.0以上版本推荐
- syntastic,语法检查,vim 8.0以下版本推荐
- vim-flake8,python语法检查和风格检查
- vim-fugitive,Git插件
- vim-surround,更改成对的符号
- vim-commentary,代码注释插件
- repeat.vim,.范式增强
- vim-autoformat,自动化格式代码
- vim-json,JSON插件
- vim-css-color,高亮显示CSS中的颜色
- The NERD tree,文件浏览
- The NERD Commenter,代码注释
- vim-signature,显示文件内部标记
- tagbar,tags浏览
- auto-pairs,自动补全成对的符号
- vim-cpp-enhanced-highlight,增强的C++语法高亮
- vim-workspace,项目管理和文件自动保存
- indentLine,缩进提示
- rainbow,括号美化
- vim-polyglot,语法高亮包
- investigate.vim,在线查找文档
- vim-easy-align,格式化工具
- vim-airline,状态栏美化
- vim-devicons,图标美化
- ctrlp,模糊搜索
- YouCompleteMe,智能补全
- UltiSnips,代码片段
- gundo,文件时光机
- open-browser,在Vim中访问链接
- startify,启动页面美化

你可能感兴趣的:(Vim)