DEV Community

Fikri Mulyana Setiawan
Fikri Mulyana Setiawan

Posted on • Updated on

How to Configure Vim as Your Main Python IDE

Vim Logo

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 :

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
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

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

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 :

inoremap ( ()<Esc>i
inoremap { {}<Esc>i
inoremap [ []<Esc>i
inoremap ' ''<Esc>i
inoremap " ""<Esc>i
Enter fullscreen mode Exit fullscreen mode

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

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. 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

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

filetype plugin on
set omnifunc=syntaxcomplete#Complete
Enter fullscreen mode Exit fullscreen mode

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

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 and vim lightline. 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
  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.

let g:airline_section_b= '%{strftime("%c")}'
let g:airline_section_y= 'BN: %{bufnr("%")}'
let g:airline#extensions#tabline#enabled = 1
Enter fullscreen mode Exit fullscreen mode

read more about vim native plugin here .

File Explorer

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

GitHub logo preservim / nerdtree

A tree explorer plugin for vim.

Help Wanted

NERDTree is on the lookout for a new maintainer. See issue #1280 to submit your name for consideration.


The NERDTree Vint

Introduction

The NERDTree is a file system explorer for the Vim editor. Using this plugin, users can visually browse complex directory hierarchies, quickly open files for reading or editing, and perform basic file system operations.

NERDTree Screenshot

Installation

Use your favorite plugin manager to install this plugin. tpope/vim-pathogen, VundleVim/Vundle.vim, junegunn/vim-plug, and Shougo/dein.vim are some of the more popular ones. A lengthy discussion of these and other managers can be found on vi.stackexchange.com. Basic instructions are provided below, but please be sure to read, understand, and follow all the safety rules that come with your power tools plugin manager.

If you have no favorite, or want to manage your plugins without 3rd-party dependencies, consider using Vim 8+ packages, as described in Greg Hurrell's excellent Youtube video: Vim screencast #75:

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 logo vim-python / python-syntax

Python syntax highlighting for Vim

Python syntax highlighting for Vim

This is an enhanced version of the original Vim 6.1 Python syntax highlighting python.vim by Neil Schemenauer.

Features

  • Enhanced highlighting for
    • Strings
    • Special symbols inside strings
    • Numeric constants
  • Added support for
    • Python 3
    • Numbers with underscores
    • String %-formatting and f-strings
    • Magic comments: source code encoding and shebangs
    • New exceptions and builtins
    • Doctests
    • @decorator syntax
    • Class variables such as self, cls, and mcs
    • Operators
  • Highlighting of the following errors
    • Invalid symbols in source file
    • Invalid numeric constants
    • Invalid %-formatting inside strings
    • Invalid variable names
    • Invalid operators
    • Mixing spaces and tabs
    • Trailing spaces (Enabled with g:python_highlight_space_errors)
  • Commands for easy switching between versions

Folding is done by the plugin SimpylFold.

How to install

Use one of the following plugin managers:

Configuration

Option variables

Set variable to 1 to enable or 0 to disable.

For example to enable all syntax highlighting…

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 :

let g:python_highlight_all = 1
Enter fullscreen mode Exit fullscreen mode

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:

nnoremap <F10> :! python %<CR> 
Enter fullscreen mode Exit fullscreen mode

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.

if you want to see my _vimrc configuration, you can visit my github repository here:

GitHub logo fikrinotes / Vim-Config

Configuration for my text editor - Vim

Vim-Config

Configuration for my text editor - Vim




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

Top comments (0)