Use Vim as a Python IDE

Use Vim as a Python IDE

I love vim and often use it to write Python code. Here are some useful plugins and tools for building a delightful vim python environment, escpecially for Vim8:

我喜欢vim,经常用它来编写Python代码。以下是一些有用的插件和工具,用于构建令人愉快的vim-python环境,尤其是vim8:

528269-20190501112817625-422410871.png

As you can see, tmux is also one of my favourite tools in terminal.

如您所见,tmux也是我在终端中最喜欢的工具之一。


Syntax Checking

If you use Vim8, w0rp/ale is a better option than syntastic, for it utilizes the async feature in Vim8, you will never get stuck due to the syntax checking. It’s similar to flycheck in emacs, which allows you to lint while you type.

如果您使用Vim8, w0rp/ale是比syntastic更好的选择,因为它利用了Vim8中的异步特性,您永远不会因为语法检查而被卡住。它类似于emacs中的flycheck,允许您在键入时进行lint。

528269-20190501112931231-1061270929.gif

(taken from ale)


Code Formatter

google/yapf can be used to format python code. Make a key mapping as bellow, then you can format your python code via <LocalLeader> =.

可以使用google/yapf格式化python代码。将键映射设置为bellow,然后可以通过' = '格式化python代码。

autocmd FileType python nnoremap <LocalLeader>= :0,$!yapf<CR>

You can also take a look at Chiel92/vim-autoformat.


Sort Import

timothycrosley/isort helps you sort imports alphabetically, and automatically separated into sections. For example, use <LocalLeader>i to run isort on your current python file:

timothycrosley/isort帮助您按字母顺序对导入进行排序,并自动将其分成几个部分。例如,使用' i '在当前python文件上运行isort:

autocmd FileType python nnoremap <LocalLeader>i :!isort %<CR><CR>

Or you can use its vim plugin: fisadev/vim-isort.

Update: ALE now has a command ALEFix for autofixing. Concerning code formatter and sort import, you could do that by merely configuring ALE properly. I’d love to put these in ftplugin/python.vim:

ALE现在有一个命令' ALEFix '用于自动修复。关于code formatter和sort import,您可以通过正确配置ALE来实现这一点。我想把这些放到ftplugin/python.vim:

let b:ale_linters = ['flake8']
let b:ale_fixers = [
\   'remove_trailing_lines',
\   'isort',
\   'ale#fixers#generic_python#BreakUpLongLines',
\   'yapf',
\]nnoremap <buffer> <silent> <LocalLeader>= :ALEFix<CR>

If you want to fix files automatically on save:

如果你想修复文件自动保存:

let g:ale_fix_on_save = 1

Now you have the support of syntax checking and autofixing with one ALE! As a matter of fact, ALE also has a plan to support auto-completion via LSP. Keep watching this amazing project if you are interested.

现在,您已经支持语法检查和自动修复与一个ALE!事实上,ALE还计划通过LSP支持自动完成。如果你感兴趣,请继续观看这个精彩的项目。


Auto Completion

Valloric/YouCompleteMe is a good way to provide code auto completion. It has several completion engines, aside from Python, C, C++, Rust, Go and Javascript are also supported. Whereas a bunch of people also think YCM is too huge and need to be compiled, then jedi-vim is an alternative. They all use jedi as their backend.

528269-20190501113037588-1894623416.png

(from jedi-vim)

What’s more, I know many people use Shougo/deoplete.nvim. Thanks to the async API, some more hopeful completion plugins are borned:

maralla/completor.vim is an code completion framework for Vim8, and support NeoVim too.

528269-20190501113103865-1283931725.gif

roxma/nvim-completion-manager also provides experimental support for Vim8.

roxma/nvim-completion-manager 还为Vim8提供了实验支持。

prabirshrestha/asyncomplete.vim is a fork of nvim-completion-manager in pure vim script with python dependency removed.

prabirshrestha/asyncomplete.vim是纯vim脚本中的一个nvim- completemanager分支,去掉了python依赖项。

nvim-completion-manager
(from NCM)

Update: Unfortunately, NCM is not maintained any more.

*更新:**不幸的是,NCM不再维护了。*

Update again: ncm2, the successor of NCM, comes out! coc.nvim is also promising.


Quick Run

If use Vim8, you can execute python file asynchronously by skywind3000/asyncrun.vim and output automatically the result to the quickfix window like this:

如果使用Vim8,您可以通过skywind3000/asyncrun.vim异步执行python文件,并将结果自动输出到quickfix窗口,如下所示:

" Quick run via <F5>
nnoremap <F5> :call <SID>compile_and_run()<CR>function! s:compile_and_run()exec 'w'if &filetype == 'c'exec "AsyncRun! gcc % -o %<; time ./%<"elseif &filetype == 'cpp'exec "AsyncRun! g++ -std=c++11 % -o %<; time ./%<"elseif &filetype == 'java'exec "AsyncRun! javac %; time java %<"elseif &filetype == 'sh'exec "AsyncRun! time bash %"elseif &filetype == 'python'exec "AsyncRun! time python %"endif
endfunction" Deprecated:
" augroup SPACEVIM_ASYNCRUN
"     autocmd!
"    " Automatically open the quickfix window
"     autocmd User AsyncRunStart call asyncrun#quickfix_toggle(15, 1)
" augroup END
"
" asyncrun now has an option for opening quickfix automatically
let g:asyncrun_open = 15

For neovim, neomake/neomake is worthy of trying. Here is the description from neomake’s README:

对于neovim, neomake/neomake值得一试。以下是neomake的自述:

It is intended to replace the built-in :make command and provides functionality similar to plugins like syntastic and dispatch.vim. It is primarily used to run code linters and compilers from within Vim, but can be used to run any program.

Another approach is to use TMUX. The idea is simple: it can split your terminal screen into two. Basically, you will have one side of your terminal using Vim and the other side will be where you run your scripts.

PS: 另一种方法是使用TMUX。这个想法很简单:它可以把你的终端屏幕一分为二。基本上,终端的一端使用Vim,另一端运行脚本。

528269-20190501113232548-2017909048.png


Enhance the default python syntax highlighting

python-mode/python-mode provides a more precise python syntax highlighting than the defaults. For example, you can add a highlighting for pythonSelf .

python-mode/python-mode提供了比默认值更精确的python语法高亮显示。例如,您可以为“pythonSelf”添加高亮显示。

hi pythonSelf  ctermfg=68  guifg=#5f87d7 cterm=bold gui=bold

528269-20190501113209346-1824963793.png

For more customized python syntax highlightings, please see space-vim-dark theme and syntax/python.vim in python-mode/python-mode . You can also put them after color command.

更多定制的python语法高亮显示,请参见space-vim-dark主题和syntax/python。vim in python-mode/python-mode。你也可以把它们放在颜色命令之后.

Actually, python-mode contains tons of stuff to develop python applications in Vim, e.g., static analysis, completion, documentation, and more. (But personally, I prefer to obtain the functionalities by some other better plugins.)

实际上,python模式包含了大量在Vim中开发python应用程序的内容,例如静态分析、完成、文档等等。(但就我个人而言,我更喜欢通过一些更好的插件来获得这些功能。)


Python text objects

vim-pythonsense provides text objects and motions for Python classes, methods, functions, and doc strings.

vim-pythonsense为Python类、方法、函数和doc字符串提供文本对象和运动。


LSP

The concept of Language Server Protocol has been around for quite a while, many languages already have a decent LSP support. So far LSP is the only way to bring in various features similar to IDE for the text editors in a standard way. To do that, you need to install the correspoding language server and a LSP client to interact with it.

*Language Server Protocol的概念已经存在很长一段时间了,许多语言已经有了不错的LSP支持。到目前为止,LSP是以标准方式为文本编辑器引入各种类似IDE的特性的惟一方法。为此,您需要安装correspoding语言服务器和一个LSP客户机来与之交互。*

Vim LSP clientImplementationSupport
LanguageClient-neovimRustvim/neovim
aleVimLvim/neovim
vim-lspVimLvim/neovim
neovim’s built-in LSP supportLuaneovim only

LCN implements the LSP client in Rust, so it obviously has an outstanding performance compared to others written in vimscript or lua. Most LSP clients are usable now, but far from perfect:

LCN在Rust中实现了LSP客户机,因此与其他使用vimscript或lua编写的客户机相比,LCN显然具有出色的性能。大多数LSP客户端现在都是可用的,但还远远不够完美:

  • simple and crude UI
  • poor performance

Still a long way to go :).


Summary

There are also some neccessary general programming plugins, e.g.

也有一些必要的通用编程插件,例如。

  • scrooloose/nerdcommenter for convenient commenter.
  • Yggdroot/indentLine or nathanaelkane/vim-indent-guides for visually displaying indent levels in Vim.
  • fzf and fzf.vim for fuzzy file searching, also vim-fz and fzy.
  • ……

Although vim is great and many plugins are productive, IDE is still my first choice when it comes to refactoring code and debugging:). Some useful links for debugging python:

  • python-debugging-tips
  • my-python-ipython-vim-debugging-workflow

For detailed vim configuration, please refer to space-vim. Enable ycmd/lsp, auto-completion, syntax-checking, python, programming Layer , then you could get a nice vim environment for python like the above screenshot. Enjoy!

有关vim的详细配置,请参阅space-vim。启用ycmd/lsp、自动完成、语法检查、python、编程层,然后您就可以得到一个适合python的vim环境,就像上面的截图一样。享受吧!

528269-20190501114308509-1891656612.png

转载于:https://www.cnblogs.com/maozhe/p/10799424.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/386903.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

RUNOOB python练习题 35 python print各色字体及背景

用来练手的python练习题&#xff0c;原题链接: python练习实例35 题干: 文本颜色设置 python中通过指令可以控制输出的背景颜色&#xff0c;前景颜色&#xff0c;以及显示方式。指令的语法如下: ’\033[显示方式&#xff1b;前景色&#xff1b;背景色m 输出字符 \033[0m’ 其…

for else语句小tips : RUNOOB python练习题36

用来练手的python练习题&#xff0c;原题链接: python练习实例36 题干: 求100之内的素数 求某个范围内的素数&#xff0c;和之前的一个例题其实是一样的&#xff0c;上次的同类例题链接如下: python练习实例12 在实现题目要求时&#xff0c;这次用了for else语句&#xff0c…

RUNOOB python练习题37 对一个序列的数进行排序

用来练手的Python练习题&#xff0c;原题链接: python练习实例37 题干: 对10个数进行排序 在我们使用Numpy模块时&#xff0c;这个问题是非常简单的&#xff0c;下面放出降序排列和升序排列的代码: 升序排列 import numpy as npresult np.zeros(10) for i in range(result…

逆向学习-IDApython(一)

背景 IDAPython 由三个分离的模块组成,他们分别是 idc,idautils 和 idaapi。 idc(注意大小写,不是 IDA 中的 IDC)是一个封装了 IDA 的 IDC 的兼容性模块,idautils 是 IDA 的高级实用功能模块,idaapi 允许了我们访问更加底层的数据。 基本操作 idc.Screen或者here()返回此时的地…

RUNOOB python练习题 39 数组排序

用来练手的python练习题&#xff0c;原题链接: python练习实例39 题干: 有一个已经排好序的数组。现输入一个数&#xff0c;要求按原来的规律将它插入数组中。 这个题目用列表List的sort&#xff0c;append方法非常简单&#xff0c;如下: 想要升序排列的话&#xff0c;就直接…

IIS 部署asp.net Provisional headers are shown 在VS2005返回值,部署不返回值

IIS 部署asp.net Provisional headers are shown 在VS2005调试返回值&#xff0c;部署到IIS不返回值 首先当时为了跨域的问题&#xff0c;在上面 后来把跨域的内容放在IIS去解决 其次&#xff0c;在每次返回结束的时候&#xff0c;加一句话 Response.End();

RUNOOB python练习题44

用来练手的python练习题&#xff0c;原题链接:python练习实例44 题干: 两个 3 行 3 列的矩阵&#xff0c;实现其对应位置的数据相加&#xff0c;并返回一个新矩阵 使用基本的List类写起来就比较麻烦&#xff0c;需要初始化一个3*3的全0矩阵&#xff0c;之后通过遍历将矩阵对应…

ImportError: No module named 'matplotlib'(python 安装各种 )

matplotlib 怎么弄啊 我应该是安装matplotlid了的 怎么还说我没有 编辑于&#xff1a;2017.03.22 12:28 0 分享 |评论0|收藏0|浏览7261 qq_37926784 声望&#xff1a; -4 3个回答 按赞数排序 用pip 也显示已经安装了matplotlib 编辑于&#xff1a;2018.03.26 19:43 分…

QBXT Day 5图论相关

图论是NOIP的一个非常重要的考点&#xff0c;换句话说&#xff0c;没有图论&#xff0c;NOIP的考纲就得少一大半&#xff08;虽然很NOIP没有考纲&#xff09; 图论这玩意吧&#xff0c;和数论一样是非常变态的东西&#xff0c;知识点又多又杂&#xff0c;但是好在一个事&#x…

RUNOOB python练习题47 交换两个变量值

用来练手的python练习题&#xff0c;原题链接: python练习实例47 题干: 两个变量值互换 在C语言C中我们要构造一个能交换两个变量值的函数很方便&#xff0c;我们可以使用指针&#xff0c;或者C中的引用。那么在没有指针的python中如何构造一个可以交换两个变量值的函数呢&am…

lambda函数 RUNOOB python练习题49

用来练手的python练习题&#xff0c;原题链接python练习实例49 该练习题主要是关于lambda函数的使用方法&#xff0c;本文就python中的lambda函数做出一点总结。 1. lambda函数的定义与调用 在python中&#xff0c;我们都知道使用def关键词来定义一个函数, 例如一个最简单的…

kubernetes(k8s)安装部署

Kubernetes是一个开源的&#xff0c;用于管理云平台中多个主机上的容器化的应用&#xff0c;Kubernetes的目标是让部署容器化的应用简单并且高效,Kubernetes提供了应用部署&#xff0c;规划&#xff0c;更新&#xff0c;维护的一种机制。 Kubernetes一个核心的特点就是能够自主…

python random随机数 RUNOOB python练习题50

用来练手的python练习题&#xff0c;原题链接: python练习实例50、 该练习题主要包含了random模块随机数的应用&#xff0c;下面给出几个常用的模块内函数。 1. 生成浮点型随机小数 最简单的&#xff0c;就是用random函数&#xff0c;生成 [0.0,1.0)[0.0, 1.0)[0.0,1.0)范围…

Spring Cloud Eureka Consul使用和对比

Spring Cloud简介 最大的区别是Eureka保证AP, Consul为CP。 Consul强一致性(C)带来的是&#xff1a; 服务注册相比Eureka会稍慢一些。因为Consul的raft协议要求必须过半数的节点都写入成功才认为注册成功 Leader挂掉时&#xff0c;重新选举期间整个consul不可用。保证了强一致…

符号 RUNOOB python练习题 51

用来练手的python练习题&#xff0c;原题链接: python练习实例51 python中的 & 和 | 使用过程中&#xff0c;变量类型不同&#xff0c;这两个符号的作用也不同。 1. 对于数字变量&#xff0c;&\&& 和 ∣|∣ 用于逐位运算 # 二进制逐位逻辑与门运算 a 0b110…

Python打印杨辉三角形 RUNOOB python练习题61

用来练手的python练习题&#xff0c;原题链接: python练习实例61 题干: 打印出杨辉三角形 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 实现代码如下: import numpy as nptable…

使用Docker快速搭建Tensorflow开发环境

当我刚开始学习使用scikit-learn时&#xff0c;总是会出现各种各样的包依赖问题&#xff0c;兜兜转转了一遍才全部安装好&#xff0c;现在的机器学习算法开发者大都使用tensorflow、pytorch来实现自己的想法&#xff0c;但依然会面临各种包版本和依赖的问题&#xff0c;有一段时…

RUNOOB python 67 数组的元素互换

用来练手的Python练习题&#xff0c;原题链接:python练习实例67 题干: 输入数组&#xff0c;最大的与第一个元素交换&#xff0c;最小的与最后一个元素交换&#xff0c;输出数组 代码如下: import numpy as nptable np.array([10,4,9,3,11,25,37,15,2,231,672,22]) #定义sw…

微信小程序、微信公众号、H5之间相互跳转

一、小程序和公众号 答案是&#xff1a;可以相互关联。 在微信公众号里可以添加小程序。 图片有点小&#xff0c;我把文字打出来吧&#xff1a; 可关联已有的小程序或快速创建小程序。已关联的小程序可被使用在自定义菜单和模版消息等场景中。 公众号可关联同主体的10个小程…