使用vim搭建python开发环境
最近开始经常需要用vim写代码,这里记录一下自己利用vim搭建基本的python开发环境所使用的插件和一些基本配置 vim基本配置
在 ~/.vimrc 文件中,添加如下的信息,完成vim的一些基本配置。 " 设置编码 set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 set nocompatible " 关闭 vi 兼容模式 syntax on " 自动语法高亮 set ruler " 打开状态栏标尺 set number "显示行号 autocmd FileType python set expandtab "在Python文件中将Tab展开成空格, set tabstop=4 "设置Tab长度为4空格 set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4 set autoindent "自动缩进 set paste "防止粘贴时缩进混乱的问题 set cursorline " 突出显示当前行 set hlsearch " 搜索时高亮显示 "设置按5直接运行python代码 filetype plugin on " 按 F5 执行当前 Python 代码" map :call PRUN() func! PRUN() exec "w" if &filetype == "python" exec "!python %" endif endfunc 「Vim插件管理器 Vundle」
从 GitHub clone Vundle.vim 文件到 ~/.vim/bundle 文件夹下,完成Vundle的安装。 git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
然后修改 ~/.vimrc 文件,添加如下的信息 filetype off "设置包括vundle和初始化相关的运行时路径" set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() "启用vundle管理插件,必须" Plugin "VundleVim/Vundle.vim" "在此增加其他插件 call vundle#end() filetype plugin indent on "加载vim自带和插件相应的语法和文件类型相关脚本 「nerdcommenter 代码注释插件」
若使用 Vundle 安装 nerdcommenter 插件,需要中 Vim 的配置文件中添加 Plugin "preservim/nerdcommenter" "添加在call vundle#begin()与call vundle#end() 之间"
然后在 Vim 的命令行模式下执行 :BundleInstall 命令完成插件的安装
nerdcommenter的主要使用方式: numcc 注释接下来的num行 numcu 取消接下来几行的注释 numci 更换接下来几行的注释状态 「Nerdtree树形目录管理器」
然后在 Vim 的命令行模式下执行 :BundleInstall 命令完成插件的安装 call vundle#begin() Plugin "preservim/nerdtree" call vundle#end()
通过vundle安装好NERDTree插件后,在vim命令行模式输入命令:NERDTree就可以看到NERDTree的显示界面。
使用组合按键 Ctrl + w,可将光标自动在左右侧窗口进行切换。
如果想要每次启动vim的时候都默认开启NERDTree。可以在vimrc文件中添加: autocmd VimEnter * NERDTree 「python代码补全 jedi-vim」
jedi-vim插件依赖python开源库 「jedi」 ,可以使用pip安装: pip install jedi # 然后在vimrc中的call vundle#begin()与call vundle#end()之间添加: Plugin "davidhalter/jedi-vim" 「python代码检查插件vim-flake8」pip3 install flake8 Plugin "nvie/vim-flake8"
Vim 普通模式下按 F7 便可以直接启动 flake8 对当前文件进行语法和格式检查。 最终的vimrc文件if has("syntax") syntax on endif if filereadable("/etc/vim/vimrc.local") source /etc/vim/vimrc.local endif " 设置编码 set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 set nocompatible " 关闭 vi 兼容模式 syntax on " 自动语法高亮 set number "显示行号 autocmd FileType python set expandtab "在Python文件中将Tab展开成空格, set tabstop=4 "设置Tab长度为4空格 set shiftwidth=4 " 设定 << 和 >> 命令移动时的宽度为 4 set autoindent "继承前一行的缩进方式,适用于多行注释 set paste "粘贴时防止出现缩进错误 set cursorline " 突出显示当前行 set ignorecase smartcase " 搜索时忽略大小写,但在有一个或以上大写字母时仍保持对大小写敏感 set hlsearch " 搜索时高亮显示被找到的文本 filetype off "必须" "设置包括vundle和初始化相关的运行时路径" set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() "启用vundle管理插件,必须" Plugin "VundleVim/Vundle.vim" Plugin "preservim/nerdtree" Plugin "preservim/nerdcommenter" Plugin "davidhalter/jedi-vim" Plugin "nvie/vim-flake8" call vundle#end() filetype plugin indent on "加载vim自带和插件相应的语法和文件类型相关脚本,必须" autocmd VimEnter * NERDTree "vim启动自动打开NERDTree filetype plugin on " 按 F5 执行当前 Python 代码" map :call PRUN() func! PRUN() exec "w" if &filetype == "python" exec "!python %" endif endfunc
以上是我自己用的vim工具配置,如果大家有其他的好用的插件或者配置插件,欢迎一起交流分享呀 如果本文对你有丝毫的帮助,请帮忙 「点个免费的赞」 ,你的鼓励是对我莫大的支持!!!
原文首发于 「小哲AI」 公众号,公众号主要分享人工智能前沿算法解读,AI项目代码解析,以及编程、互联网求职等技术资料文章,偶尔也会分享个人读书笔记、工作学习心得,欢迎关注,一起学习。