How to Configure Vim as Your Main Python IDE
Tags: vim, programming
Published: 2022-01-16 15:02:04


![Vim Logo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9nqik4ok2hpmq5s8ujan.png)

IDE and text-editor is the essential tools for programmers. and, as you know, there are so many text editor out there. There are Notepad ++, Visual Studio Code, Sublime Text, Atom, Brackets, etc. But, i and many people out there choose Vim as our text editor, since it is highly customizable for our programming purpose.

## What Is Vim ?
For those of you who don't know, Vim is a text editor, not an IDE. but, don't get me wrong. Vim is __more than just a text editor__. We can make Vim works like an IDE. i wouldn't explain what makes Vim special here. you can watch about that in this video :

{% youtube 6GOqCU2VA0g %}

Altough Vim is very good for coding, it doesn't look like that at the first time. When you first installed vim and open it, it will look like this :

![default display of Vim](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxy8qh6ax0wb6flyi0t0.png)
not bad, right? but, since we will using Vim as our main text editor, we need a better view. this is my Vim text editor after configuring it :

![Vim display in my laptop after some configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2c981h6ek2zzzl3gypm.png)

So, how do we make vim looks better ? here is some configuration for vim that i think are important :

## Auto Indent
auto indenting feature is so important if you are writing code in programming languages that care about indenting, likes python. to enable auto-indent feature, you can type `:set ai` in normal mode. if you want this feature enable for every file you create, you can add `set ai` (without colon) in your `_vimrc` file (for windows user).

## Syntax Highlighting 
for some coder, syntax highlighting is an important feature. syntax highlighting feature makes us understand the code easier. to enable syntax highlighting in vim, open your `_vimrc` file, and add this text : `syntax on`.

## Turn Off The Bell
When you are using Vim for the first time, you will notice that there is a bell sound when something wrong happen. for me, this is so annoying. to turn off this sound, add `set belloff=all` to your `_vimrc` file.

## Line Numbering

![Line Number](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qrapnkjc2bvagw3fh3fc.png)

to display line number in Vim, add `set number` to your `_vimrc` file. if you want relative numbering, add `set relativenumber` to your `_vimrc` file.

## Matching Brackets and Quotation Marks 
when you use VS Code, Atoms, and other text editor, when you type `(`, you will see that `)` also appears automatically. But, when you use Vim, you wouldn't see this feature by default. to enable this feature, add `inoremap ( ()<Esc>i` to your `_vimrc` file. you can do the same thing with quotation marks. So, to enable this feature for every brackets and quotation marks, add this code to your `_vimrc` file :

```vim
inoremap ( ()<Esc>i
inoremap { {}<Esc>i
inoremap [ []<Esc>i
inoremap ' ''<Esc>i
inoremap " ""<Esc>i
```

## Change Theme In Vim
Themes in Vim are called as colorscheme. After installing vim, you will also get some Theme/Colorscheme by default. to change the colorscheme that applied in vim, type `:colors` an then press tab button. then you will see something like this :

![vim colorscheme menu](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/viabiauhhq9v96ngjtbp.png)

this is the menu for choosing vim colorscheme. you can use arrow keys to navigate between it and press enter to applied that colorscheme. to add new colorscheme, you can search it in github. the coloscheme that i personally use is [material colorscheme](https://github.com/hzchirs/vim-material). to use this colorscheme, download the file in colors folder and place it to `vim82 -> colors` in your vim folder. after that, you can applied that colorscheme like usual.

## Vim Built-in Autocompletion
do you think vim can't perform autocompletion? Vim have its built-in autocompletion named Omni Completion.
> _Omni completion provides smart autocompletion for programs. When invoked, the text before the cursor is inspected to guess what might follow. A popup menu offers word completion choices that may include struct and class members, system functions, and more. A similar feature in Microsoft Visual Studio is known as IntelliSense._ - [Vim Fandom](https://vim.fandom.com/wiki/Omni_completion) 

to enable this feature, open your `_vimrc` file, and write :

```vim
filetype plugin on
set omnifunc=syntaxcomplete#Complete
```

after this step, you can start typing your code. if you want the code suggestion to pop-up, press `ctrl-x` and `ctrl-o`.

## Cool Statusbar

![Vim statusline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0299lblm7jp63prx26eo.png)

the cool yellow line in the bottom of my Vim at the screenshot above is called _statusbar_. there are some plugins that you can use to make your statusbar looks better. for example, [vim airline](https://github.com/vim-airline/vim-airline) and [vim lightline](https://github.com/itchyny/lightline.vim). I myself use vim airline for my statusbar. to install this plugin, you can use some plugin-manager that you prefer, like vundle or vim-plug. but, if you use Vim 8, you can use **vim native package manager**. to install vim airline using vim native package manager, do this step :

1. go to [vim airline's repository](https://github.com/vim-airline/vim-airline)
2. download the entire repository in zip file format.
3. extract this zip file
4. go to you vim folder, and open `vimfiles` folder, and open `start` folder (if you don't see this folder, then create it).
5. place the extracted zip file to this `start` folder. 
6. done. you already have install vim airline.

after that step, your vim statusbar better. you can also add this code to your `_vimrc` file (optional). i don't know what exactly this code does, but it looks like this code make your statusbar display the file extension and current time.

```vim
let g:airline_section_b= '%{strftime("%c")}'
let g:airline_section_y= 'BN: %{bufnr("%")}'
let g:airline#extensions#tabline#enabled = 1
```
_read more about vim native plugin [here](https://medium.com/@paulodiovani/installing-vim-8-plugins-with-the-native-pack-system-39b71c351fea)_ .

## File Explorer
Actually, vim has file explorer by default. but, for some reason, i prefer to use *NERDTree*, a plugin for file explorer. 

{% github preservim/nerdtree %}

Installation for this plugin is the same to the installation vim airline plugin. after installing this plugin, you can open file explorer using `:NERDTree` command in normal mode. 


## Better Syntax Highlinghting for Python
As i said before, vim have built-in syntax highlighting feature. but, it is not too good. if you want a better syntax highlighting for python, you can use this plugin :

{% github vim-python/python-syntax %}

the installation of this plugin is the same to the installation vim airline plugin. after installing, you can use this plugin by add this code to your `_vimrc` file :

```vim
let g:python_highlight_all = 1
```

## Terminal
you can open your terminal directly in Vim text editor. you don't need to install anything. to open terminal in vim, type `:terminal` or `:term` in normal mode.

## Running Python Code 
actually, you can run python code in vim using terminal and type `python example.py`. but, if you want to run python code by pressing keyboard, then follow this step. first, open you `_vimrc` file. then, write this code:

```vim
nnoremap <F10> :! python %<CR> 
```

what this code does is mapping `f10` key to run `:! python %<CR>` command. in general, the command `:! python %<CR>` will take the name of your file using symbol `%` and then run it like you run it manually in the terminal. after this step, you can run python code by pressing `f10` on your keyboard. Do you know what to do if you want to run python code by pressing `r` key ? yes, just write `nnoremap r :! python %<CR>`. 



So, that is some configuration to make vim looks like an IDE. i know that there are other IDE features that vim doesn't have, like debugger. but, you can give a try for vim. Oh, wait. can this plugin be called a debugger? I've never tried it.

{% github puremourning/vimspector %}
 
if you want to see my `_vimrc` configuration, you can visit my github repository here:

{% github fikrinotes/Vim-Config %}

thanks for reading my blog so far. if you have some questions, fell free to ask in the comment section below.