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.pswp.cn/news/386903.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/386903.shtml
英文地址,請注明出處:http://en.pswp.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關鍵詞來定義一個函數, 例如一個最簡單的…