vim configuration

How to effectively use Vim as Python IDE (Part III)

By the time you read this article, you should have a basic setup in place that consists of Vim, Tmux, and Zsh. If you don’t, take a look at previous post which will guide you set by set through entire procedure. Now, we can move to a more advanced part which is ~/.vimrc configuration. This is where the fun begins. Note that it’s perfectly fine to start with an empty file although I recommend copying someone’s ~/.vimrc as it will be easier for you to adjust it than write from scratch. Besides, you will get a lot of useful stuff for free.

Plugin manager – Vundle

No Vim tutorial can start without a selection of plugin manager. I’m using the simplest solution available – Vundle. Installation is plain simple, open shell and run.

take ~/.vim/bundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

Then enter vim and execute :PluginInstall.

If you decided to use mine ~/.vimrc it might be necessary to execute command few times as it happened to me that not all of the plugins were installed on the first run. For those of you who want to know more I recommend official tutorial from git repository, although, I believe that you should be able to figure it out just by reading ~/.vimrc.

Git support

For vim user, most of the git related stuff will be done in a console. Once you master it, it’s gonna be much easier and faster that anything else. In any case, there are still some things that most probably you would like to have inside IDE. For me, it’s an indicator that shows changed, not committed lines. Ability to quickly run blame, diff, revert a single file or make a commit. Fugitive provides support for basic commands:

  • :Gblame – used as equivalent of `git blame` for current buffer
  • :Gread – used as equivalent of `git checkout` for current buffer
  • :Gdiff – used as equivalent of `git diff` for current buffer
  • :Gwrite followed by :Gcommit – to `git add` and `git commit` current buffer

The rest is achieved thanks to Signify.

Project navigation

One may argue that project navigation is not important and should be avoided as it will force you to learn project structure. Although, I partially agree with this statement, from time to time I find it useful, especially when exploring new projects. For basic navigation similar to `Project Explorers` known from most of the IDEs – Nerdtree is a default answer. It’s brought up with `nt` shortcut. Once open

  • u – takes you one directory up
  • o – opens a dir / file under cursor in current tab
  • t – opens a dir / file in new tab
  • s – opens a dir / file in a veritical split
  • m – brings a menu that allows you to create, move, copy or delete files

Files lookup

Absolute must have – CtrlP. It allows you to quickly search for files by name, path, regex or switch between open buffers. You bring it up with <C+p>. With no surprise. :D. By default, it uses full path search, press

  • <C-d> to switch between filename and path search
  • <C-r> to switch to regexp search
  • <C-b> to browse open buffers
  • <C-j>, <C-k> or the arrow keys to navigate the result list
  • <C-t> or <C-v>, <C-x> to open the selected entry in a new tab or in a new split

Keywords lookup

This time heavy lifting is done by ack-grep. A modern replacement of Grep. In order to make usage of Ack a real pleasure, we need to customize it a bit. The goal is to have an ability to quickly lookup for any keyword, function definition, class definition or function usage in entire project or just production files.

  • <Leader>a – search for “<cword>” (word under cursor) in all files except test files
  • <Leader>A – search for “<cword>” in all files
  • <Leader>c – search for “class <cword>” in all files except test files
  • <Leader>C – search for “class <cword>” in all files
  • <Leader>m – search for “def <cword>” in all files except test files
  • <Leader>M – search for “def <cword>” in all files

It’s a matter of adjustment to make them work for other languages. Simple and effective.

In file navigation

Short story, use what vim has to offer itself. Master basic movements and then search for something more sophisticated. The same applies to selection and many more. Rodrigo Queiroz wrote an excellent artcile about basic movements that will help you make first steps.

If you want to have something extra I recommend Easymotion which allows you to jump to the desired keyword by typing first two letters and then pressing highlighted keys.

Templates

There is only one option. It’s Ultisnips. From time to time you may hear of people using different stuff but at the end of a day everyone ends up using the same, proven solution. The easiest way to get head around it is by watching Vimcasts. I encourage you to take your time with it as it may be your best friend or enemy. Personally, I use tones of snippets, almost for every repetitive piece of code that I write. Few examples are:

  • c – class declaration
  • m – method declaration
  • f – function declaration
  • p – propery declaration
  • t – test method declaration
  • ae – assert equals
  • af – assert false
  • fore – for loop with enumerate
  • etc etc…

You can get some basic snippets by installing separate plugin vim-snippets. That’s a good starting point. In practice, however, it’s easier to memorize your own snippets.

Powerline

Powerline – this is the thing at the bottom of the screen that shows basic properties of you session like name of currently opened buffer, number of lines, git branch, vim mode, position in the file etc. It plays really nice with Tmux which is a great news. Since it doesn’t cost you anything and makes experience with vim much nicer why not to give it a try?

Python programming

Finally, long expected missing piece. Adding support for a new language in vim is as easy as installing one more plugin. In fact, that’s exactly it. For Python I use a combination of two plugins – python-mode, which gives me basic syntax highlighting, and vim-jedi, which gives me autocompletion. Both plugins are tuned considerably – for instance, python-mode is fully disabled except syntax highlighting (most likely it can be removed).

Autocompletion it’s brought up only after a dot as I don’t like floating windows all over a console. You navigate through it with <C-j> and <C-k>. Once you are happy, accept by pressing Tab key.

Customized configuration

As promised, by using mine ~/.vimrc you get few things besides basic configuration for free. Some of them are listed below.

  • vim-surround – allows you to quickly change ( to { or surround entire sentences with “”
  • swap-parameters – swaps places of everything separated by ,. Mostly used for function parameters
  • nerdcommenter – select text and press <leader>c<space> to toggle a comment
  • :relativenumbers plus :numbers gives you a quick view on how many lines you need to jump or delete
  • gn, gp – moves you to next or previous buffer (read it `go next`, `go previous`)
  • gr – closes current buffer (read it `go and remove`)
  • gl – takes you to last edited location (read it `go last`)
  • <Leader>-q is equivalent of :q
  • <Leader>-w is equivalent of :w
  • <Leader>-wq is euqivalent of :wq

Besides:

  • python linters are run on write
  • incremental search is enabled by default
  • file is saved everytime you switch from insert to normal mode
  • leader key is set to `,` (leader key is used in many vim commands)

Summary

Oftentimes, when you want to play with a new programming language, you start with looking at which IDE will be the best. With Vim, that concern simply does not exist as you already know the answer. In most cases, it matter of installing one more plugin and you are good to go. I next post I’m going to cover Tmux configuration.



RELATED POSTS


LEAVE A COMMENT