-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecent-python-workflow-in-vim.html
150 lines (130 loc) · 10 KB
/
decent-python-workflow-in-vim.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Decent Python workflow in Vim</title>
<link rel="stylesheet" href="./theme/css/main.css" />
</head>
<body id="index" class="home">
<header id="banner" class="body">
<h1><a href="./">The Train Station</a></h1>
<nav><ul>
<li class="active"><a href="./category/development.html">Development</a></li>
<li><a href="./category/general.html">General</a></li>
</ul></nav>
</header><!-- /#banner -->
<section id="content" class="body">
<article>
<header>
<h1 class="entry-title">
<a href="./decent-python-workflow-in-vim.html" rel="bookmark"
title="Permalink to Decent Python workflow in Vim">Decent Python workflow in Vim</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2019-06-14T20:00:00-05:00">
Published: Fri 14 June 2019
</abbr>
<address class="vcard author">
By <a class="url fn" href="./author/seth-buntin.html">Seth Buntin</a>
</address>
<p>In <a href="./category/development.html">Development</a>.</p>
<p>tags: <a href="./tag/python.html">python</a> <a href="./tag/vim.html">vim</a> <a href="./tag/tmux.html">tmux</a> <a href="./tag/lsp.html">lsp</a> </p>
</footer><!-- /.post-info --> <p>Like many others, I've been using IntelliJ's Python plugin to do Python development. One would be hardpressed to find
a better environment to develop within. I've often told people that Ideavim is the best Vim I've ever used. Not because
it is more powerful than Vim proper but because of the ecosystem (IntelliJ) from which Ideavim runs.</p>
<p>In recent years, we've seen tools like Microsoft's Visual Studio Code become more and more useful and powerful. It feels like
with the introduction of these new tools we seen the introduction of supporting tool, such as language servers, come to light.
While these systems aren't new I've never used them until recently and I have been pleasantly surprised with the power they provide.</p>
<p>All of this to say, I still find Vim the place I want to work. Other vim emulators are decent but their is still a plugin
system within the Vim ecosystem that I find valuable in my workflows and wanted to see if I could come up with an acceptable
alternative. Let's take a ride and see where we stop...</p>
<h2>Disclaimer</h2>
<p>Software engineers are quite opinionated about their development environments, me included. Hopefully this article and tools
mentioned within gives you the ability to choose the bits you like and lets you throw away and replace the items you don't.
My workflow is just that, my workflow. It isn't perfect but it is within the realm of what makes me feel comfortable. I'll
just quickly mention here some base tools that I use to make this workflow happen. I use <code>pyenv</code>, <code>pipenv</code>, <code>vim</code> (version 8)
and <code>tmux</code> with <code>tmuxp</code> to quickly and easily get me in the zone.</p>
<h2>My goal</h2>
<p>I feel like there are a couple different tasks that make IntelliJ an optimal environment for Python. The indexing used within
the tool provides the ability for us to jump to various places within our code whenever we want. We can actually hop into Flask
and see the implementation of a <code>Blueprint</code>. This is a powerful ability and the first on the list to be able to replicate. The
next was the ability to rename functions and variables and that rename successfully propagate throughout the application. All
the other pieces of functionality can be wrapped up in various tools, as you will see, so I won't make those a part of my initial
goal.</p>
<h2>Vim</h2>
<p>I've found these plugins are the ideal plugins to lay the foundation for my main goals. I use <code>vim-plug</code> as my plugin manager
but you should be able to use most of the current plugin managers.</p>
<div class="highlight"><pre><span></span><code><span class="nt">Plug</span> <span class="s1">'prabirshrestha/async.vim'</span>
<span class="nt">Plug</span> <span class="s1">'prabirshrestha/vim-lsp'</span>
<span class="nt">Plug</span> <span class="s1">'w0rp/ale'</span>
<span class="nt">Plug</span> <span class="s1">'davidhalter/jedi-vim'</span>
<span class="s2">" ------------------------------------------------------------------------------</span>
<span class="s2">"</span> <span class="nt">LSP</span>
<span class="err">"</span> <span class="nt">------------------------------------------------------------------------------</span>
<span class="nt">if</span> <span class="nt">executable</span><span class="o">(</span><span class="s1">'pyls'</span><span class="o">)</span>
<span class="nt">au</span> <span class="nt">User</span> <span class="nt">lsp_setup</span> <span class="nt">call</span> <span class="nt">lsp</span><span class="p">#</span><span class="nn">register_server</span><span class="o">(</span><span class="p">{</span>
<span class="err">\</span> <span class="err">'name':</span> <span class="err">'pyls',</span>
<span class="err">\</span> <span class="err">'cmd':</span> <span class="err">{server_info-></span><span class="cp">[</span><span class="s1">'pyls'</span><span class="cp">]</span><span class="p">}</span><span class="o">,</span>
<span class="err">\</span> <span class="s1">'whitelist'</span><span class="o">:</span> <span class="cp">[</span><span class="s1">'python'</span><span class="cp">]</span><span class="o">,</span>
<span class="err">\</span> <span class="err">}</span><span class="o">)</span>
<span class="nt">endif</span>
<span class="nt">let</span> <span class="nt">g</span><span class="p">:</span><span class="nd">lsp_diagnostics_enabled</span> <span class="o">=</span> <span class="nt">0</span> <span class="o">;</span> <span class="nt">turn</span> <span class="nt">off</span> <span class="nt">diagnosis</span> <span class="nt">because</span> <span class="nt">I</span> <span class="nt">feel</span> <span class="nt">w0rp</span><span class="o">/</span><span class="nt">ale</span> <span class="nt">does</span> <span class="nt">a</span> <span class="nt">better</span> <span class="nt">job</span>
</code></pre></div>
<p>Once you have these installed and configured you just need the <code>python-language-server</code>.</p>
<h2>Python requirements</h2>
<div class="highlight"><pre><span></span><code><span class="k">[[source]]</span>
<span class="na">url</span> <span class="o">=</span> <span class="s">"https://pypi.org/simple"</span>
<span class="na">verify_ssl</span> <span class="o">=</span> <span class="s">true</span>
<span class="na">name</span> <span class="o">=</span> <span class="s">"pypi"</span>
<span class="k">[packages]</span>
<span class="k">[dev-packages]</span>
<span class="na">python-language-server</span> <span class="o">=</span> <span class="s">"~=0.26"</span>
<span class="k">[scripts]</span>
<span class="na">pyls</span> <span class="o">=</span> <span class="s">"pyls"</span>
<span class="k">[requires]</span>
<span class="na">python_version</span> <span class="o">=</span> <span class="s">"3.7.3"</span>
</code></pre></div>
<p>Now how do we put these together to accomplish our goals?</p>
<h2>Tmux and Tmuxp</h2>
<p>I'm definitely not here to sell you on <code>tmux</code> or <code>tmuxp</code> just show you how I use them to glue it all up. Here is an example <code>.tmuxp.yml</code>
file:</p>
<div class="highlight"><pre><span></span><code>start_directory: "<span class="cp">${</span><span class="n">PWD</span><span class="cp">}</span>"
shell_command_before: "pyenv local 3.7.3"
session_name: project_name
environment:
VIRTUAL_ENV: "change-me"
windows:
- window_name: vim
panes:
- vim
- window_name: execs
panes:
- pipenv run pyls
</code></pre></div>
<p>There is definitely one special thing to note here. <code>vim-jedi</code> still doesn't easily work with <code>pipenv</code> so I accomplish the
loading of the <code>virtualenv</code> by setting the <code>VIRTUAL_ENV</code> environment variable. Without it <code>vim-jedi</code> will try to find the definitions
within the system python.</p>
<p>Once this is established a simple <code>tmuxp load .</code> will load of the session and you are on your way.</p>
<h2>See it in action</h2>
<p>Let's see it in action...</p>
<iframe src="https://player.vimeo.com/video/342394889" width="640" height="400" frameborder="0" allow="autoplay; fullscreen" allowfullscreen></iframe>
<p><a href="https://vimeo.com/342394889">vim-python-workflow</a> from <a href="https://vimeo.com/user394719">Seth Buntin</a> on <a href="https://vimeo.com">Vimeo</a>.</p>
<h2>Notable mentions</h2>
<p>While we just went over a couple of tasks to potentially replace my IntelliJ workflow it definitely isn't everything. Some notable mentions
are: <code>Tagbar</code>, <code>vim-fugitive</code>, <code>dispatch</code> (for running quick tasks while in the editor), <code>NERDTree</code>, <code>vimux</code> and <code>fzf</code> to name a few more.</p>
<p>I hope you have found these useful and now feel empowered to embark on the journey to use the tools that we have all loved for many years
now with some of the more modern abilities of today's IDE's and tools.</p>
</div><!-- /.entry-content -->
</article>
</section>
<section id="extras" class="body">
</section><!-- /#extras -->
<footer id="contentinfo" class="body">
<address id="about" class="vcard body">
Proudly powered by <a href="http://getpelican.com/">Pelican</a>, which takes great advantage of <a href="http://python.org">Python</a>.
</address><!-- /#about -->
<p>The theme is by <a href="http://coding.smashingmagazine.com/2009/08/04/designing-a-html-5-layout-from-scratch/">Smashing Magazine</a>, thanks!</p>
</footer><!-- /#contentinfo -->
</body>
</html>