目录
对于python的IDE,现在pycharm和vscode都非常火,它们也确实好用,但如果你偶尔厌倦了键盘和鼠标的频繁切换,想获得那种纯键盘的体验,那么可以试试vim,先放一张效果图:
上面这套vim配置使用了十几个插件,vim的一个很大的乐趣就在于选择和配置插件的过程,本文会一步一步讲解vim的安装以及每个插件的配置过程。
安装vim8并支持python3
安装python3
系统安装的python版本可以通过以下命令查看
ls /usr/bin | grep python
本人使用的是centos7,默认安装的是python2,没有python3,通过以下命令安装python3.6
sudo wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sudo yum install -y python36 python36-devel
curl https://bootstrap.pypa.io/get-pip.py | python3.6
安装vim
进入你想要下载vim的目录
cd ~
如果没有安装git先安装,-y意为不用再询问yes/no
sudo yum install -y git
从github下载vim源码
git clone https://github.com/vim/vim.git
安装依赖
sudo yum install -y ncurses ncurses-devel gcc
进入源码中的src目录
cd vim/src
配置安装选项
./configure --with-features=huge \
--enable-multibyte \
--enable-rubyinterp=yes \
--enable-python3interp=yes \
--with-python3-config-dir=/lib64/python3.6/config-3.6m-x86_64-linux-gnu \
--prefix=/usr/local/vim8
其中with-python3-config-dir
与系统类型以及python3安装版本有关,centos7下python3.6的config目录位于/lib64/python3.6/config-3.6m-x86_64-linux-gnu
(如果系统中只安装了一个python3,那么with-python3-config-dir
可以缺省)
编译
sudo make
安装
sudo make install
添加软链接
sudo ln -s /usr/local/vim8/bin/vim /usr/bin/vim
检查vim对python3的支持
vim --version | grep python
出现+python3
说明所安装的vim支持python3
安装插件管理工具
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
该命令将把Vundle插件管理器下载到~/.vim/bundle/文件夹下,此后就需要使用配置.vimrc文件来安装插件
首先要在你的主目录下新建一个名为.vimrc的vim配置文件。在shell中运行以下命令
cd ~
touch .vimrc
这样.vimrc文件就新建好了,此时.vimrc文件里面是空的
然后将下面的Vundle配置添加到.vimrc文件的顶部
set nocompatible " 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'
" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
对于vim的配置文件,"
是注释符,上面的空白处是用来添加另外插件的。然后在shell中运行以下命令
vim +PluginInstall +qa
配置基本设置
将下面的配置加在~/.vimrc后面
" backspace settings, make the backspace behaving as in notepad
set backspace=indent,eol,start
" line number
set number
" utf-8 encoding
set encoding=utf-8
set fileencodings=utf-8,gb2312,gb18030,gbk,ucs-bom,cp936,latin1
set termencoding=utf-8
set fileformat=unix
" splite the window to below or right
set splitbelow
set splitright
" when typing in /what to find, highlight the results
set hlsearch
配置vim界面
安装文件树插件
给vim安装nerdtree,在~/.vimrc的插件段落插入
Plugin 'scrooloose/nerdtree' " file tree
然后在shell中运行以下命令
vim +PluginInstall +qa
如果将来要卸载这个插件,首先在~/.vimrc中删除
Plugin 'scrooloose/nerdtree'
那一行,
然后在shell中运行以下命令
vim +PluginClean +qa
即可卸载该插件
nerdtree具体的配置加在~/.vimrc后面,这里配置如下
"""""""""""""""""""""""""""
" nerdtree settings
"""""""""""""""""""""""""""
" Show line number
let g:NERDTreeShowlineNumber=1
" F7 to open/close nerdtreenode
map <F7> :NERDTreeToggle<CR>
" automatically open nerdtreenode when opening vim
autocmd vimenter * NERDTree
" Go to previous (file) window.
autocmd VimEnter * wincmd p
" if vim only has nerdtree, automatically close
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
" set file treenode symbol to space
let g:NERDTreeNodeDelimiter = "\u00a0"
let g:NERDTreeDirArrowExpandable = '|'
let g:NERDTreeDirArrowCollapsible = '/'
" Delete help information at the top, we can press u to up a dir
let NERDTreeMinimalUI=1
安装one dark主题
给vim安装one dark主题,在~/.vimrc的插件段落插入
Plugin 'rakr/vim-one' " colorscheme one
然后在shell中运行以下命令
vim +PluginInstall +qa
具体的配置加在~/.vimrc后面,这里配置如下
""""""""""""""""""""""""""
" Color scheme settings
""""""""""""""""""""""""""
if (has("termguicolors"))
set termguicolors
endif
syntax enable
set background=dark
colorscheme one
- 如果想让终端也用one dark主题,可以试试one-gnome-terminal:
- 首先打开gnome终端的preferences,在其中的color栏中取消勾选use colors from system theme
然后给gnome-terminal安装one-gnome-terminal主题,在终端输入以下命令
bash -c "$(curl -fsSL https://raw.githubusercontent.com/denysdovhan/gnome-terminal-one/master/one-dark.sh)"
- 重启终端生效
- 首先打开gnome终端的preferences,在其中的color栏中取消勾选use colors from system theme
安装状态栏
给vim安装airlilne,在~/.vimrc的插件段落插入
Plugin 'vim-airline/vim-airline' " status bar and label bar
然后在shell中运行以下命令
vim +PluginInstall +qa
具体的配置加在~/.vimrc后面,这里配置如下
""""""""""""""""""""""""""""
" airline settings
""""""""""""""""""""""""""""
" the tab only displays file name without path
let g:airline#extensions#tabline#formatter = 'unique_tail'
" define vertical tabs
let g:airline#extensions#tabline#left_sep = ' '
let g:airline#extensions#tabline#left_alt_sep = '|'
" enable the tabline
let g:airline#extensions#tabline#enabled=1
" in this window, tab to swtich buffer
nnoremap <tab> :bn!<cr>
" define no sep symbols
if !exists('g:airline_symbols')
let g:airline_symbols = {}
endif
let g:airline_left_sep = ''
let g:airline_left_alt_sep = ''
let g:airline_right_sep = ''
let g:airline_right_alt_sep = ''
let g:airline_symbols.branch = ''
let g:airline_symbols.readonly = ''
let g:airline_symbols.linenr = ''
let g:airline_symbols.maxlinenr = ''
配置实用工具
安装自动缩进插件
给vim安装indentpython.vim,这是一款专门针对python的自动缩进插件,在~/.vimrc的插件段落插入
Plugin 'vim-scripts/indentpython.vim' " enhanced indent tool for python
然后在shell中运行以下命令
vim +PluginInstall +qa
安装自动补全插件
给vim安装YouCompleteMe,这是一款强大的自动补全插件
首先安装依赖
sudo yum install -y gcc-c++ cmake python3-devel
sudo yum install -y centos-release-scl
sudo yum install -y devtoolset-8
临时设置C++编译环境(只在当前的终端进程内有效):
export CC=/opt/rh/devtoolset-8/root/usr/bin/gcc
export CPP=/opt/rh/devtoolset-8/root/usr/bin/cpp
export CXX=/opt/rh/devtoolset-8/root/usr/bin/c++
下载YouCompleteMe插件
git clone https://github.com/Valloric/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
进入YCM目录,下载子模块
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
安装
python3 ./install.py
在~/.vimrc的插件段落插入
Plugin 'Valloric/YouCompleteMe' " auto completer
然后在shell中运行以下命令
vim +PluginInstall +qa
YCM具体的配置加在~/.vimrc后面,这里配置如下
"""""""""""""""""""""""
" YCM settings
"""""""""""""""""""""""
let g:ycm_python_binary_path = '/usr/bin/python3'
" set <\>g to map to GoToDefinition
map <leader>g :YcmCompleter GoToDefinitionElseDeclaration<CR>
" Disable preview window
set completeopt=menu,menuone
let g:ycm_add_preview_to_completeopt=0
" ycm working with virtual environments
let g:ycm_python_interpreter_path = ''
let g:ycm_python_sys_path = []
let g:ycm_extra_conf_vim_data = [
\ 'g:ycm_python_interpreter_path',
\ 'g:ycm_python_sys_path'
\]
let g:ycm_global_ycm_extra_conf = '~/global_extra_conf.py'
要支持python虚拟环境,还需要新建一个文件~/global_extra_conf.py
,并在其中写入:
def Settings( **kwargs ):
client_data = kwargs[ 'client_data' ]
return {
'interpreter_path': client_data[ 'g:ycm_python_interpreter_path' ],
'sys_path': client_data[ 'g:ycm_python_sys_path' ]
}
安装代码检查插件
给vim安装ALE,在~/.vimrc的插件段落插入
Plugin 'dense-analysis/ale' " syntax check
然后在shell中运行以下命令
vim +PluginInstall +qa
具体的配置加在~/.vimrc后面,这里配置如下
"""""""""""""""""""""""""""
" ALE settings
"""""""""""""""""""""""""""
" Check Python files with flake8.
let g:ale_linters = {
\ 'python': ['flake8'],
\ }
" Fix Python files with yapf.
let g:ale_fixers = {
\ 'python': ['yapf'],
\ }
" Disable warnings about trailing whitespace for Python files.
let g:ale_warn_about_trailing_whitespace = 0
" fix on save
let g:ale_fix_on_save = 1
" ctrl+j ctrl+k to select the problems
nmap <silent> <C-k> <Plug>(ale_previous_wrap)
nmap <silent> <C-j> <Plug>(ale_next_wrap)
这里ALE依赖flake8和yapf,用pip安装它们
pip install flake8
pip install yapf
安装asyncrun插件
给vim安装asyncrun.vim,在~/.vimrc的插件段落插入
Plugin 'skywind3000/asyncrun.vim' " run tool
然后在shell中运行以下命令
vim +PluginInstall +qa
具体的配置加在~/.vimrc后面,这里配置如下
"""""""""""""""""""""""
" asyncrun settings
"""""""""""""""""""""""
let g:asyncrun_open = 8
" F6 to run this file
nnoremap <F6> :AsyncRun python %<cr>
inoremap <F6> <esc>:AsyncRun python %<cr>
安装debug插件
给vim安装REPL(交互式的编程环境),在~/.vimrc的插件段落插入
Plugin 'sillybun/vim-repl' " debug tool
然后在shell中运行以下命令
vim +PluginInstall +qa
具体的配置加在~/.vimrc后面,这里配置如下
"""""""""""""""""""""""""""
" REPL settings
"""""""""""""""""""""""""""
let g:repl_program = {
\ 'python': 'ipython',
\ 'default': 'zsh',
\ 'r': 'R',
\ 'lua': 'lua',
\ }
let g:repl_predefine_python = {
\ 'numpy': 'import numpy as np',
\ 'matplotlib': 'from matplotlib import pyplot as plt'
\ }
let g:repl_cursor_down = 1
let g:repl_python_automerge = 1
let g:repl_ipython_version = '7'
" <\><r> to open/close REPL
nnoremap <leader>r :REPLToggle<Cr>
" <F5> run to cursor
autocmd Filetype python nnoremap <F5> <Esc>:REPLDebugStopAtCurrentLine<Cr>
" <F4> continue running(step over)
autocmd Filetype python nnoremap <F4> <Esc>:REPLPDBN<Cr>
" <F3> continue running(step in)
autocmd Filetype python nnoremap <F3> <Esc>:REPLPDBS<Cr>
" set REPL window to the right
let g:repl_position = 3
REPL依赖ipython和ipdb,通过pip安装它们
pip install ipython
pip install ipdb
安装增强的搜索插件
给vim安装LeaderF,在~/.vimrc的插件段落插入
Plugin 'Yggdroot/LeaderF' " search tool
然后在shell中运行以下命令
vim +PluginInstall +qa
具体的配置加在~/.vimrc后面,这里配置如下
""""""""""""""""""""""""""""""""""
" LeaderF settings
""""""""""""""""""""""""""""""""""
let g:Lf_StlSeparator = { 'left': '', 'right': '' }
" search word under cursor literally in all listed buffers
noremap <C-F> :<C-U><C-R>=printf("Leaderf! rg -e %s ", expand("<cword>"))<CR>
" search visually selected text literally, don't quit LeaderF after accepting an entry
xnoremap <C-F> :<C-U><C-R>=printf("Leaderf! rg -e %s ", leaderf#Rg#visual())<CR>
" recall last search. If the result window is closed, reopen it.
noremap go :<C-U>Leaderf! rg --recall<CR>
上述配置使用rg搜索,所以需要安装rg
sudo yum-config-manager --add-repo=https://copr.fedorainfracloud.org/coprs/carlwgeorge/ripgrep/repo/epel-7/carlwgeorge-ripgrep-epel-7.repo
sudo yum install -y ripgrep
其他有用的插件
Plugin 'terryma/vim-expand-region' " expand tool
- 缩进线indentLine
Plugin 'Yggdroot/indentLine' " indent line display
- 自动括号auto-pairs
Plugin 'jiangmiao/auto-pairs' " auto pairs
- 自定义快捷键
" <\>c to close quickfix window
nnoremap <leader>c :cclose<cr>
" <F2> to save all
nnoremap <F2> :wa<cr>
inoremap <F2> <esc>:wa<cr>
使用说明
常见基本命令:
- v 进入可视模式
- y 复制到normal模式下的剪贴板,然后p粘贴
- "+y复制到系统剪贴板
- * 自动查找光标所在单词
- ctrl+f和ctrl+p翻页
对于python3,上述配置具有以下功能:
- 显示文件树
F7 打开/关闭文件树 - one dark的主题
- 状态栏和标签栏
tab 在当前窗口切换buffer - 自动缩进
- 自动补全
- 代码检查
>>
和--
指示所在行出现违反flake8检查的错误和警告 - 自动整理代码
输入:ALEFix
,使用ALE调用yapf对当前代码进行整理
每次修改保存后,ALE也会自动调用yapf对当前代码进行整理,如果要禁用保存时自动整理代码的功能,修改~/.vimrc中的g:ale_fix_on_save
:
let g:ale_fix_on_save = 0
- run
F6 一键运行python脚本,并在下方展示运行结果
此时按\+c关闭运行结果 - debug
F5 一键debug到当前行,并在右侧展示结果
继续按F4执行下一行(不跳入函数)
F3也是执行下一行(会跳入函数)
此时按\+r关闭debug窗口
\+r 打开REPL交互式编程环境 - 增强的搜索
基于LeaderF rg
ctrl+f再按回车,在当前目录下所有文件中查找光标所在单词(可视模式下是查找选中部分)
go 显示上一次查找结果 - 选中扩展
v 进入可视模式
+ 自动扩展选择(执行完后处于可视模式) - 缩进线
- 自动括号
- 其他快捷键
F2 一键保存所有文件
\+g 转到函数定义