DEV Community

Cover image for Setting up Vim for Python
Meet Rajesh Gor
Meet Rajesh Gor

Posted on • Edited on • Originally published at meetgor.com

Setting up Vim for Python

Introduction

Vim is quite a powerful text editor which can add performance to the already fast typed language Python. Vim can be highly customizable and efficient to use as it has the power of adding custom plugins and plugins managers, key mappings, and the most critical weapon of vim - Access to the terminal straight away.
This is not a full-featured guide of using vim for python, it's just a quick setup for using python on vim blazingly fast!!

Plugin Managers

So let us start making Vim, the text editor suitable for a python programmer. Firstly we'll need the vim plugin manager. There are different plugin managers out there, each of them has the same purpose to install, upgrade and manage the plugins for vim. You can install any one of them and get up and running.

These are some of the finest and well-supported plugin managers in vim. You can use any of these plugin managers, and get started by installing some plugins.

JEDI-VIM- Auto completion

Firstly I will like to install Jedi for code completion in Python. The plugin can be simple and straightforward to install using any of the above plugin managers. Jedi-Vim provides some neat and clean** syntax analytics and autocompletion for Python in Vim**. You'll find the docs and installation process here JEDI-VIM

NERDTree-FIle manager

Next, It would be great if we install a file manager for managing the files and folders in the code directories. We can simply use the Nerdtree plugin for this. NerdTree is quite a fantastic plugin for file management in Vim. It simply makes Vim feel like VS Code. The installation and docs can be found here NERDTree.

Nerdtree commands can be longer to write, for that let's start mapping and for that, we can start editing our Vimrc.



set number
syntax enable
filetype indent on
set tabstop=4
set softtabstop=4
set autoindent 
set encoding=utf-8


Enter fullscreen mode Exit fullscreen mode

This can be some addition to your existing vimrc as you might have a setup for plugin managers. You can choose the color scheme of your choice, don't waste time selecting the color scheme. Feel free and modify the vimrc according to your knowledge and choice.

Keymappings

We move on to the Key mappings for NERDTree and other features. You can make mappings generally in the normal mode but there might be cases where you need to use maps for visual mode or insert mode as well, that entirely depends on the user :)

To map in normal mode, we'll its command to be specific:



nnoremap <C-n> :NERDTree<CR>


Enter fullscreen mode Exit fullscreen mode

This will map CTRL+n to open the NERDTree file manager to the left, saving a bit of time and avoiding frustration. Feel free to add any keymap of your choice, this is just for demonstration.
You can further automate NERDTree for switching between tabs because it makes you type CTRL+w twice, you can reduce that to just typing w.



nnoremap w:<C-w><C-w>


Enter fullscreen mode Exit fullscreen mode

Integrated Terminal Macros

We can open a terminal window like a split between the editor. We can simply use the command :terminal to split the window horizontally, where the upper split will be terminal and the down window will have the editor. This is quite a neat feature of Vim in that it blends with the terminal so well so that we can switch between the terminal and the editor very quickly. For that, you can create a macro if you need to fire up a terminal again and again.



nnoremap <C-t> :terminal<CR>


Enter fullscreen mode Exit fullscreen mode

If you place the above macro in your vimrc and then type Ctrl+t, the exact thing will happen to fire up a terminal split but with fewer keystrokes and without leaving the normal mode.
Also, the NERDTree macro can be also fruitful with this as it will make a full-blown IDE-like feeling inside of Vim.
Demonstrate macros for NERDTree and terminal split

Running the Code with a snap

We can automate the process of running python scripts inside of vim. Instead of typing out the entire command for executing python script from vim. We can use keymaps for it as they can significantly boost the time required to run and debug the code.



nnoremap py :!python %


Enter fullscreen mode Exit fullscreen mode

This is a small map but can save a lot of time and give some motivation to use vim as you run the code blazingly faster than other editors. I have used py, but it can cause some problems as p is already mapped for pasting. So it's better to use other key combinations such as ty, yh, or any other key combination of your choice. Try it out and add your own flavor that's how we all learn.

So, that's the basic set-up for python on vim, you can make more custom mappings, find more plugins and test out which work out the best for your workflow. Happy Coding and Viming ;)

Top comments (10)

Collapse
 
phantas0s profile image
Matthieu Cneude

I would also suggest coc.nvim. The big advantage: you can use any LSP (Language Server Protocol) to autocomplete any language you want. Visual Studio uses LSP too under the hood for its autocompletion.

Collapse
 
waylonwalker profile image
Waylon Walker

great suggestion. I recently dropped coc for built in lsp. It's significantly simpler to setup, but its still new (only available in nvim 0.5.0+ nightly) so there's far fewer resources on how to do it.

Collapse
 
phantas0s profile image
Matthieu Cneude

I'm waiting for the official release :)

Thread Thread
 
waylonwalker profile image
Waylon Walker

No worries, I've seen a lot of people running it with no issues, but I get it. Your text editor is kinda the last thing that you want to be so bleeding edge that its always broken.

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

ya sure, there are many options out there :)

Collapse
 
waylonwalker profile image
Waylon Walker

I'd suggest replacing all nmaps with nnoremap, its good practice. nmap is rarely what you want and can get you in trouble.

Its so interesting to see other's setups. I do python in vim every day and have a drastically different setup. It just shows how customizable it is. Vim is here to make it your own.

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

oh ya! Thanks for feedback👍I forgot to make it nnoremap, Even I prefer that. Noremap is used in most cases. That's why we love Vim;)

Collapse
 
adwaithrajesh profile image
Adwaith Rajesh

you should try vscode with vim, Its insane

Collapse
 
mr_destructive profile image
Meet Rajesh Gor

Ya, I agree. But there are some people who don't want slowness/ lag during programming. I personally have a middle-end laptop, Vim handles resources quite well. VS Code takes 2 minutes at minimum to open up, Vim not even 5 seconds.

Collapse
 
waylonwalker profile image
Waylon Walker

I often jump between a number of projects. I can have many instances of nvim running without my machine breaking a sweat. thinking about vscode makes it sweat.

That said, vscode has a fantastic out of the box experience.