500字范文,内容丰富有趣,生活中的好帮手!
500字范文 > python vim天作之合_Python+Vim:天作之合

python vim天作之合_Python+Vim:天作之合

时间:2023-08-18 01:16:43

相关推荐

python vim天作之合_Python+Vim:天作之合

1.与Vim的缘分

7年的Java开发生涯,一年半Java和Python混合的研究生学习,到现在最近全职Python开发,不知不觉已经半年没碰Java了。从开始从Eclipse转到Intellij,完全习惯了用JetBrain的产品做各种语言的IDE。现在Python开发也不例外,依旧沿用Intellij IDEA外加Python插件。虽然一直用Intellij这种“重型”IDE,但文本编辑方面一直用Vim,所以在Intellij里也继续使用Vim插件。

多年前尝试过使用Vim作为Java的IDE,但最后还是放弃了。最近又重新思考发现:转向Python开发后,Vim与Intellij之间的差距越来越小了。可能对于所有脚本语言大家都有同样的感觉,于是最近又重新尝试将Vim作为IDE来使用。

本文主要参考了这篇《VIM and Python - a Match Made in Heaven》。很多其他的Vim环境搭建文章都提到了这篇文章,内容确实非常不错,而且名字也非常美,Vim和Python,天作之合!

2.Vim作为IDE的好处

首先,相比其他IDE,Vim更加轻量级。如果不装过多插件或插件性能没问题的话,使用Vim开发对硬件的要求是比较低的。其二,Vim能在本地和远程服务器上获得一致的编程体验,简单说就是一个vimrc个性化的配置文件走天下,非常便携。最后,也是最最主要的原因就是Vim本身的特点:所思即所得的编辑速度。这也是在接触了解Vim之后便一发不可收拾,使用Intellij、Sublime等各种编辑器都要加装Vim插件的原因。

3.打造现代化的IDE

作为开胃菜,先给大家看一下最终的效果,这样也许能更有动力去鼓捣后面那些配置。

3.1 环境准备

首先,检查Vim的版本以及Vim能否定位到Python,这里跳过Vim的安装说明了。

:version

:python import sys; print(sys.version)

其次,因为我们要安装很多插件,所以就需要一个现代化的插件管理器,而不是一个个地手动下载安装。目前流行的Vim插件管理器有Vundle、Pathogen、Vim-plugin。其中Vim-plugin号称速度最快最小巧,但本文还是使用老牌的Vundle,安装命令非常简单。

$ git clone /gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim

3.2 开箱即用

下面就是本文的主菜了——最终摸索出的一个比较好的Vim配置。先列在这里给心急的同学,后面会慢慢介绍涉及到的每个插件的用处和基本用法。

set nocompatible " required filetype off " required

" set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required

Plugin 'gmarik/Vundle.vim'

" ===== Add all your plugins here ===== " (note older versions of Vundle used Bundle instead of Plugin)

Plugin 'Valloric/YouCompleteMe'

" Class structure viewer Plugin 'majutsushi/tagbar' " Move like AceJump in Emacs

Plugin 'easymotion/vim-easymotion'

" Color scheme Plugin 'jnurmine/Zenburn' Plugin 'altercation/vim-colors-solarized' " File/folder navigation

Plugin 'scrooloose/nerdtree'

Plugin 'jistr/vim-nerdtree-tabs'

" Super searching Plugin 'kien/ctrlp.vim' " Powerful status bar

Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'}

" Out-of-box python development "Plugin 'klen/python-mode'

"Plugin 'vim-syntastic/syntastic' "Plugin 'nvie/vim-flake8'

" Git integration "Plugin 'tpope/vim-fugitive'

Plugin 'airblade/vim-gitgutter'

" Navigate between tmux and vim seamlessly Plugin 'christoomey/vim-tmux-navigator' " ===== All of your Plugins must be added before the following line =====

call vundle#end() " required filetype plugin indent on " required

set encoding=utf-8

" give you the standard four spaces when you hit tab, ensure your line length " doesn’t go beyond 80 characters

au BufNewFile,BufRead *.py

\ set tabstop=4 |

\ set softtabstop=4 |

\ set shiftwidth=4 |

"\ set textwidth=79 | \ set expandtab | \ set autoindent "\ set fileformat=unix

" Autocomplete window goes away when you’re done with it let g:ycm_autoclose_preview_window_after_completion=1 " F8 key will toggle the Tagbar window

nmap :TagbarToggle

let g:tagbar_width=50

" Easymotion: enhance default vim search by named jump and highlighting ":nmap w

let g:EasyMotion_smartcase = 1

map / (easymotion-sn)

omap / (easymotion-tn)

map n (easymotion-next)

map N (easymotion-prev)

map j (easymotion-j)

map k (easymotion-k)

" Color. Fix scheme missing problem in tmux+iterm2 colorscheme zenburn set background=dark set t_Co=256 " Ignore files in NERDTree

let NERDTreeIgnore=['\.pyc$', '\~$']"ignore files in NERDTree " Always use the directory you started vim in to search let g:ctrlp_working_path_mode = 0 " For pymode or other syntatic plugin? let python_highlight_all=1 syntax on set nu set autoread

现在启动Vim,执行Vundle的安装命令将所有插件都安装上,后续使用时不用再执行这条命令。

:PluginInstall

以后再启动Vim时,进入项目所在的根文件夹,然后执行下面命令。因为这样会自动开启NERDTree导航栏,打开Tagbar边栏,一个IDE就这样准备就绪了。

$vim +':NERDTree' +':TagbarToggle'

3.3 插件使用

编辑部分是Vim的强项,所以其实插件主要增强的是智能提示、窗口UI、项目内查找以及与其他开发工具结合(如Git)等方面:

智能提示:YouCompleteMe即插即用,如果需要进一步的符号跳转,则需要额外的ctags支持。

浏览:NERDTree和tagbar提供文件目录和结构的浏览。而vim-tmux-navigator则是窗口内跳转的神器,用ctrl+H/J/K/L就可以无差别的在所有vim和tmux内部窗口(pane)间跳转。

查找:easymotion提供很多功能,但最简单方便的就是与Vim本身的”/”查找集成,直接提供AceJump功能。j和k提供行跳转。ctrlp用Ctrl+P激活,输入文件名进行模糊查找,或可以水平或垂直分割窗口来打开选中的文件进行编辑。

Git:powerline显示文件路径和当前文件所在的Git分支。vim-gitgutter可以像IDE一样在行号位置标记出新增、删除、修改的行,同时]c和[c可以跳转到修改位置,hp和hu可以方便地预览和回滚某一块的更改(leader默认是”\”)。

还有一些很不错的插件还没有用起来,例如vim-fugitive还没用上,vim-flake8不支持与Git结合只提示修改过部分的代码格式问题,syntasitc官方推荐配置出现性能问题导致Vim经常性的卡死。

4.碰到的问题

4.1 YCM报错

YCM安装后报错:YouCompleteMe unavailable: requires Vim 7.4.1578+.

Info: You appear to be running the default system Vim on macOS. It reports as patch 8056, but it is really older than 1578. Please consider MacVim, homebrew Vim or a self-built Vim that satisfies the minimum requirement.

The ycmd server SHUT DOWN (restart with ‘:YcmRestartServer’)

解决方法如下:

$cd ~/.vim/bundle/YouCompleteMe

$./install.py (cmake required)

4.2 Vim颜色在tmux中丢失

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。