<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Eli Bendersky's website - Vim</title><link href="https://eli.thegreenplace.net/" rel="alternate"/><link href="https://eli.thegreenplace.net/feeds/vim.atom.xml" rel="self"/><id>https://eli.thegreenplace.net/</id><updated>2023-02-04T13:41:52.148303-08:00</updated><entry><title>A Vim plugin for pss</title><link href="https://eli.thegreenplace.net/2012/01/12/a-vim-plugin-for-pss" rel="alternate"/><published>2012-01-12T10:01:10-08:00</published><updated>2022-10-04T14:08:24.342531-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2012-01-12:/2012/01/12/a-vim-plugin-for-pss</id><summary type="html">
      &lt;p&gt;
If you enjoy using &lt;a href="https://github.com/eliben/pss/"&gt;pss&lt;/a&gt; and belong
to the cult of Vi, you may like the the &lt;a href="https://bitbucket.org/bernh/pss.vim"&gt;pss.vim plugin&lt;/a&gt; by Bernhard
Leiner. It's a very simple-to-use and functional plugin for running
&lt;strong&gt;pss&lt;/strong&gt; searches from inside Vim and showing the results in the
Quickfix window.
      &lt;/p&gt;&lt;p&gt;

If you find yourself running …&lt;/p&gt;</summary><content type="html">
      &lt;p&gt;
If you enjoy using &lt;a href="https://github.com/eliben/pss/"&gt;pss&lt;/a&gt; and belong
to the cult of Vi, you may like the the &lt;a href="https://bitbucket.org/bernh/pss.vim"&gt;pss.vim plugin&lt;/a&gt; by Bernhard
Leiner. It's a very simple-to-use and functional plugin for running
&lt;strong&gt;pss&lt;/strong&gt; searches from inside Vim and showing the results in the
Quickfix window.
      &lt;/p&gt;&lt;p&gt;

If you find yourself running greps a lot from inside Vi, and/or using plugins
like EasyGrep, give pss.vim a try - it's a much better searching experience for
source code.
      &lt;/p&gt;
    </content><category term="misc"/><category term="Software &amp; Tools"/><category term="Vim"/></entry><entry><title>pycscope with Vim</title><link href="https://eli.thegreenplace.net/2010/09/28/pycscope-with-vim" rel="alternate"/><published>2010-09-28T10:05:28-07:00</published><updated>2023-02-04T13:41:52.148303-08:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2010-09-28:/2010/09/28/pycscope-with-vim</id><summary type="html">
        &lt;p&gt;I've &lt;a class="reference external" href="https://eli.thegreenplace.net/2010/09/17/browsing-python-source-code-with-vim/"&gt;written earlier&lt;/a&gt; about some problems I was having with the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; tool for browsing Python code in Vim. Well, it turns out I was using an older version of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pycscope&lt;/span&gt;&lt;/tt&gt; for some reason, and the &lt;a class="reference external" href="http://pypi.python.org/pypi/pycscope/"&gt;latest version&lt;/a&gt; actually does play well with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Interestingly, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; wasn't really designed to …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;I've &lt;a class="reference external" href="https://eli.thegreenplace.net/2010/09/17/browsing-python-source-code-with-vim/"&gt;written earlier&lt;/a&gt; about some problems I was having with the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; tool for browsing Python code in Vim. Well, it turns out I was using an older version of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pycscope&lt;/span&gt;&lt;/tt&gt; for some reason, and the &lt;a class="reference external" href="http://pypi.python.org/pypi/pycscope/"&gt;latest version&lt;/a&gt; actually does play well with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Interestingly, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; wasn't really designed to support languages other than C. I've had an interesting email exchange with one of its maintainers, and I think the following quote from him is conclusive:&lt;/p&gt;
&lt;blockquote&gt;
The program's name is &amp;quot;cscope&amp;quot; for a reason. It's not called c++scope, not javacscope, not pythoncsope, and certainly not &amp;quot;every programming language under the sun&amp;quot;scope.&lt;/blockquote&gt;
&lt;p&gt;Asked about the database format of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;, he said that it's not documented &lt;em&gt;by design&lt;/em&gt;, to make sure all tools go through &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; itself. Fair enough, I guess.&lt;/p&gt;
&lt;p&gt;The author of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;pycscope&lt;/span&gt;&lt;/tt&gt;, however, perused the code of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; (open source FTW!) to understand the DB format and generate a compatible &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope.out&lt;/span&gt;&lt;/tt&gt; file from his script, which made it possible to use the integration of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; in Vim after all.&lt;/p&gt;
&lt;p&gt;That said, I'm still not sure whether &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; is more convenient for Python than &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;EasyGrep&lt;/span&gt;&lt;/tt&gt; - I need more time to decide.&lt;/p&gt;

    </content><category term="misc"/><category term="Vim"/></entry><entry><title>Browsing Python source code with Vim</title><link href="https://eli.thegreenplace.net/2010/09/17/browsing-python-source-code-with-vim" rel="alternate"/><published>2010-09-17T06:50:12-07:00</published><updated>2023-02-04T13:41:52.140303-08:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2010-09-17:/2010/09/17/browsing-python-source-code-with-vim</id><summary type="html">
        &lt;p&gt;Two tools I find very useful when navigating large C code-bases with Vim are &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; allows easily jumping to tags - function, macro and variable definitions. The excellent &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;taglist&lt;/span&gt;&lt;/tt&gt; plugin of Vim uses it to create an IDE-like listing of all the tags of your buffer in a …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;Two tools I find very useful when navigating large C code-bases with Vim are &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; allows easily jumping to tags - function, macro and variable definitions. The excellent &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;taglist&lt;/span&gt;&lt;/tt&gt; plugin of Vim uses it to create an IDE-like listing of all the tags of your buffer in a separate mini-window. It's also required for the implementation of omni-completion in Vim (kind-of intellisense).&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; is somewhat more versatile. In addition to jumping to tags, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; allows you to find where a function is called, where a variable is assigned, what functions the current one calls, and a useful general C symbol lookup. In browsing the 300+KLOC CPython code-base I found &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; to be extremely functional and helpful in untangling relations between functions and finding what calls what.&lt;/p&gt;
&lt;p&gt;While trying to use these tools for Python code, however, I ran into a few problems, some of which I didn't yet solve.&lt;/p&gt;
&lt;div class="section" id="ctags-for-python"&gt;
&lt;h3&gt;ctags for Python&lt;/h3&gt;
&lt;p&gt;At first &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; appears to work find for Python code, managing to find symbols in a large code-base. However, very quickly one notices an annoying problem: tags are created for imports, not only function / class definitions. Suppose you define a class named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MyType&lt;/span&gt;&lt;/tt&gt; in a file &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;mytype.py&lt;/span&gt;&lt;/tt&gt;. Other files import it by running:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #00007f; font-weight: bold"&gt;from&lt;/span&gt; &lt;span style="color: #00007f"&gt;mytype&lt;/span&gt; &lt;span style="color: #00007f; font-weight: bold"&gt;import&lt;/span&gt; MyType
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now, when your cursor is on &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;MyType&lt;/span&gt;&lt;/tt&gt; you'd expect &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Ctrl+]&lt;/span&gt;&lt;/tt&gt; to jump just to the class definition, but it actually asks you to which of several tags you want to jump, and apart from the definition, there's a tag for each import. This can be very annoying...&lt;/p&gt;
&lt;p&gt;Fortunately, there's a way around it, which took some googling to find. Create a file named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;~/.ctags&lt;/span&gt;&lt;/tt&gt; (if you don't already have it) and place the following line into it:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;--python-kinds=-i
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Rerun &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt;. This tells &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; to stop marking imports as tags. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ctags&lt;/span&gt;&lt;/tt&gt; allows convenient configuration of such things, and you can examine these settings by using the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;--list-kinds&lt;/span&gt;&lt;/tt&gt; argument. For example after adding the import exclusion line, I get:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;src$ ctags --list-kinds=python
c  classes
f  functions
m  class members
v  variables
i  imports [off]
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class="section" id="cscope-for-python"&gt;
&lt;h3&gt;cscope for Python&lt;/h3&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;(Edit: 28.09.2010)&lt;/strong&gt; Note: this is no longer accurate. See my &lt;a href="https://eli.thegreenplace.net/2010/09/28/pycscope-with-vim/"&gt;newer post&lt;/a&gt; on this issue.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; is a harder problem. Although its Wikipedia page explicitly states it supports Python and googling around you find some people suggesting to use it for Python, I just couldn't make it work.&lt;/p&gt;
&lt;p&gt;Jumping to Python symbols with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; is always shifted by a few lines. A short investigation led to the discovery of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;'s inability to grok Python comments, and lines starting with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;#&lt;/span&gt;&lt;/tt&gt; confuse it and create the shift.&lt;/p&gt;
&lt;p&gt;There's a tool online called &lt;a class="reference external" href="http://pypi.python.org/pypi/pycscope/0.2"&gt;pycscope&lt;/a&gt;, which claims to create an accurate &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; database for Python code, but it appears to be incompatible with the latest &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;I've written to one of the maintainers of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt;, who was surprised I'm using it for Python code at all, since the tool is designed for C code.&lt;/p&gt;
&lt;p&gt;So if you've managed to run &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; on Python code, do let me know please.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="grep-and-easygrep"&gt;
&lt;h3&gt;grep and EasyGrep&lt;/h3&gt;
&lt;p&gt;So without &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cscope&lt;/span&gt;&lt;/tt&gt; I was left with one of the oldest and truest friends of the programmer - &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;grep&lt;/span&gt;&lt;/tt&gt;. Fortunately Vim has a very nice plugin named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;EasyGrep&lt;/span&gt;&lt;/tt&gt; that wraps the functionality of Vim's &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;grep&lt;/span&gt;&lt;/tt&gt; and provide simple means for searching source code.&lt;/p&gt;
&lt;p&gt;A couple of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;EasyGrep&lt;/span&gt;&lt;/tt&gt;'s most useful features are:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Creating key bindings for searching the symbol under the cursor.&lt;/li&gt;
&lt;li&gt;Automatically configuring grep to search recursively on certain file types, depending on the type of the current file. This functionality is fully configurable.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
&lt;div class="section" id="anything-else"&gt;
&lt;h3&gt;Anything else?&lt;/h3&gt;
&lt;p&gt;Any other plugins and tools you're using to make your Python coding in Vim more pleasant? Please let me know.&lt;/p&gt;
&lt;/div&gt;

    </content><category term="misc"/><category term="Vim"/></entry><entry><title>Commenting out a block of code with Vim</title><link href="https://eli.thegreenplace.net/2010/08/25/commenting-out-a-block-of-code-with-vim" rel="alternate"/><published>2010-08-25T05:17:56-07:00</published><updated>2022-10-04T14:08:24.330531-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2010-08-25:/2010/08/25/commenting-out-a-block-of-code-with-vim</id><summary type="html">
        &lt;p&gt;After some looking-around I've found two interesting techniques to comment (or comment out) a block of code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Substitution on a range of lines&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Given a range of lines, a simple substitution command may add or remove comments. For example, for Python code &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;s/^/#/&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;s/^#//&lt;/span&gt;&lt;/tt&gt; will add or remove a …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;After some looking-around I've found two interesting techniques to comment (or comment out) a block of code:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Substitution on a range of lines&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Given a range of lines, a simple substitution command may add or remove comments. For example, for Python code &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;s/^/#/&lt;/span&gt;&lt;/tt&gt; and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;s/^#//&lt;/span&gt;&lt;/tt&gt; will add or remove a comment from the beginning of the line, respectively.&lt;/p&gt;
&lt;p&gt;To make a range of lines for this operation you can use any Vim technique, like for example explicitly specifying the range:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;:M,N s/^/#/
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Will comment out lines in the inclusive range &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;[M:N]&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;A simpler way is to use the visual selection mode, by pressing &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;V&lt;/span&gt;&lt;/tt&gt; (capital &lt;em&gt;v&lt;/em&gt; for line selection), selecting the needed lines and then executing the substitution command.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Using block-mode visual selection&lt;/strong&gt;&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;Move to the first column in the first line you want to comment-out.&lt;/li&gt;
&lt;li&gt;Press &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;Ctrl-V&lt;/span&gt;&lt;/tt&gt; to start block-mode selection.&lt;/li&gt;
&lt;li&gt;Move down to select the first column of a block of lines.&lt;/li&gt;
&lt;li&gt;Press &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;I&lt;/span&gt;&lt;/tt&gt; and then the desired comment starter (i.e. &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;#&lt;/span&gt;&lt;/tt&gt;)&lt;/li&gt;
&lt;li&gt;Press &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;ESC&lt;/span&gt;&lt;/tt&gt; and the insertion will be applied to the whole block.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;To uncomment with this techniques follow the directions but instead of &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;I&lt;/span&gt;&lt;/tt&gt; use &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;x&lt;/span&gt;&lt;/tt&gt; to delete the first char.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Others...&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you have other techniques to suggest, please let me know. Also if you're familiar with a good plugin that makes this really easy and also knows which types of source code require which comment chars, I'd like to hear about it.&lt;/p&gt;

    </content><category term="misc"/><category term="Vim"/></entry></feed>