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,一经查实,立即删除!

相关文章

sql2008“备份集中的数据库备份与现有的xx数据库不同”解决方法 因为是在另一台电脑对同名数据库做的备份,用常规方法还原,提示不是相同数据库,不让还原,在网上找到下面的方法解决了: 一、右击系

sql2008“备份集中的数据库备份与现有的xx数据库不同”解决方法 因为是在另一台电脑对同名数据库做的备份&#xff0c;用常规方法还原&#xff0c;提示不是相同数据库&#xff0c;不让还原&#xff0c;在网上找到下面的方法解决了&#xff1a; 一、右击系统数据库master&…

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

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

ubuntu18.04 qemu环境搭建【学习笔记】

一、准备工具   1.1 安装相关工具     sudo apt-get install qemu libncurses5-dev gcc-arm-linux-gnueabi build-essential 1.2 下载kernel(linux-4.0)与busybox(1.24)源码 https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/ https://busybox.net/downloads/busy…

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

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

Linux 下杀毒软件 clamav 的安装和使用

Linux 下杀毒软件 clamav 的安装和使用 安装依赖&#xff1a; 1 2 3 yum install -y pcre* zlib zlib-devel libssl-devel libssl yum install -y openssl yum install -y epel-release openssl version 0.9.8 or higher 1. yum 安装 clamav 安装后会自动生成服务文件&#…

列表,元组和range

内容大纲 列表的初识列表的索引切片列表的增删改查列表的嵌套元组的初识元组的简单应用range 昨日内容回顾以及作业讲解 int str boolstr 索引 s[x:y:z] 常用操作方法 upper lower startswith endswith split 分割:默认按照空格.将字符串分割成列表.可以知道分隔符 strip …

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

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

Linux服务器不停的向外发包,且CPU持续100%

服务器不停的向外发包&#xff0c;且CPU持续100%&#xff0c;远程登录后查看发现有一长度为10的随机字符串进程&#xff0c;kill掉&#xff0c;会重新生成另外长度为10的字符串进程。删除文件也会重复生成&#xff0c;非常痛苦。查阅crond相关日志&#xff0c;发现实际执行的内…

逆向学习-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();

背包

学习博客https://www.cnblogs.com/fengziwei/p/7750849.html //为了不误导初学者&#xff0c;和给老手diss的机会&#xff0c;我就把以前发的删了&#xff0c; 有兴趣的看看那个博客就行了 &#xff08;明明写好了&#xff0c;却被老师关机子...不开心转载于:https://www.cnblo…

POSIX线程

POSIX线程 标签&#xff08;空格分隔&#xff09;&#xff1a; Linux程序设计 什么是线程 线程是一个进程内部的一个控制序列。 当在进程中创建一个新线程时&#xff0c;新的执行线程将拥有自己的栈&#xff08;因此也有自己的局部变量&#xff09;&#xff0c;但与它的创建者共…

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…

tensorflow一元二次函数拟合

先看下要做的内容&#xff0c;创建一元二次函数yx平方-0.5&#xff0c;其中为了更符合散点图模拟需要&#xff0c;在方程加噪点&#xff0c;以标准方差0.05行驶&#xff0c;如图所示 折线图 散点图 下面我们要做的&#xff0c;是要计算机自动拟合出该散点图的函数&#xff0…

hibernate缓存机制与N+1问题

在项目中遇到的趣事 本文基于hibernate缓存机制与N1问题展开思考&#xff0c; 先介绍何为N1问题 再hibernate中用list()获得对象&#xff1a; 1 /**2 * 此时会发出一条sql&#xff0c;将30个学生全部查询出来3 */4 List<Student> …

lambda函数 RUNOOB python练习题49

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