centos7系统下vim8配置python3的IDE

对于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)"
    • 重启终端生效

安装状态栏

给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

其他有用的插件

  1. 选中扩展vim-expand-region
Plugin 'terryma/vim-expand-region'        " expand tool 
  1. 缩进线indentLine
Plugin 'Yggdroot/indentLine'              " indent line display
  1. 自动括号auto-pairs
Plugin 'jiangmiao/auto-pairs'             " auto pairs
  1. 自定义快捷键
" <\>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+fctrl+p翻页

对于python3,上述配置具有以下功能:

  1. 显示文件树
    F7 打开/关闭文件树
  2. one dark的主题
  3. 状态栏和标签栏
    tab 在当前窗口切换buffer
  4. 自动缩进
  5. 自动补全
  6. 代码检查
    >>--指示所在行出现违反flake8检查的错误和警告
  7. 自动整理代码
    输入:ALEFix,使用ALE调用yapf对当前代码进行整理
    每次修改保存后,ALE也会自动调用yapf对当前代码进行整理,如果要禁用保存时自动整理代码的功能,修改~/.vimrc中的g:ale_fix_on_save
let g:ale_fix_on_save = 0
  1. run
    F6 一键运行python脚本,并在下方展示运行结果
    此时按\+c关闭运行结果
  2. debug
    F5 一键debug到当前行,并在右侧展示结果
    继续按F4执行下一行(不跳入函数)
    F3也是执行下一行(会跳入函数)
    此时按\+r关闭debug窗口
    \+r 打开REPL交互式编程环境
  3. 增强的搜索
    基于LeaderF rg
    ctrl+f再按回车,在当前目录下所有文件中查找光标所在单词(可视模式下是查找选中部分)
    go 显示上一次查找结果
  4. 选中扩展
    v 进入可视模式
    + 自动扩展选择(执行完后处于可视模式)
  5. 缩进线
  6. 自动括号
  7. 其他快捷键
    F2 一键保存所有文件
    \+g 转到函数定义
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值