DEV Community

Cover image for How to Make Vim a Python IDE - Best IDE for Python.
Hamdi Jr
Hamdi Jr

Posted on • Updated on

How to Make Vim a Python IDE - Best IDE for Python.

Hello everyone,
I'm back with another Linux tutorial. We are going to talk about something which is also my personal favorite - VIM.
We are all fed up with iDE's and text editors which takes years to open up and setting up these things takes more time than writing the whole code. That's why I have come up with the best solution.

What's Vim?

Before we go any further, let's talk about what vim is. Vim is a very lightweight text editor that can be customized however we want. It normally runs in the terminal and is very easy to learn.

Why Vim?

As I mentioned earlier, Vim is very fast and highly customizable. Also, who doesn't want to work on an IDE which opens up within a second? Another reason to choose vim is because of its keyboard shortcuts. While using vim you don't need to touch the mouse. This will reduce your coding time by half. I'm sure by the end of the day, you will be a fan. Let's get started now.

After this tutorial, our vim will look like :
Alt Text

Installing Vim

Vim installation is a very easy step. For now, I'm going to focus on Linux installation but I'll provide the links for other operating systems.
Windows - Vim in windows

Mac - We are going to install vim using Homebrew. Open your terminal and type:

$ brew update
$ brew install vim
Enter fullscreen mode Exit fullscreen mode

This will install vim on your machine.

Linux - Installing vim in Linux is very easy. Open your terminal and type:

  • For Debian based distros:
$ apt-get remove vim-tiny
$ apt-get update
$ apt-get install vim
Enter fullscreen mode Exit fullscreen mode
  • For Arch based distros:
pacman -S vim
Enter fullscreen mode Exit fullscreen mode
  • For fedora:
dnf install vim-enhanced
Enter fullscreen mode Exit fullscreen mode

Installing Plugin Manager

Vim comes with a native plugin manager but we have something better than that. We are going to install Vundle. It makes installing and updating packages trivial.
Let's install vundle:

  • Open your terminal and type :
$ git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/Vundle.vim
Enter fullscreen mode Exit fullscreen mode

This will clone the vundle repository in your machine.

  • Next step is the vim configuration file(vimrc). You will need this file throughout the tutorial. Do the following in your terminal:
$ cd ~
$ touch ~/.vimrc
Enter fullscreen mode Exit fullscreen mode
  • Add vundle to your vim configuration. Go to your terminal and type:
$ nano ~/.vimrc
Enter fullscreen mode Exit fullscreen mode

this will open the vimrc file. Now copy the code below and paste it there.

filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" All of your Plugins must be added before the following line
call vundle#end()
filetype plugin indent on
Enter fullscreen mode Exit fullscreen mode

Now ctrl+o to write the file then press Enter and finally ctrl+x to exit the editor.

  • Now let's install vundle plugin inside vim. fire up your terminal and type vim and now vim will load up. Now type :PluginInstall and press Enter.

Making Vim an IDE

Now comes the best part - Installing all plugins to make vim an IDE. This step is very easy, we are going to add a line in our vimrc and go to vim and install it. Dont worry this may look a bit confusing but it's very easy. Let's get started :

  • Open your terminal type sudo nano ~/.vimrc and clear everything there and then paste the code:
set nocompatible              " required
filetype off                  " required
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'

" All Plugins

Plugin 'mhartington/oceanic-next'
Plugin 'tmhedberg/SimpylFold'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'vim-syntastic/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'
Plugin 'kien/ctrlp.vim'
Plugin 'tpope/vim-fugitive'
Plugin 'zxqfl/tabnine-vim'
Plugin 'frazrepo/vim-rainbow'
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'
Plugin 'davidhalter/jedi-vim'
Plugin 'ycm-core/YouCompleteMe'


call vundle#end()            " required
filetype plugin indent on    " required


" setting horizontal and vertical splits
set splitbelow
set splitright

"split navigations
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

" Enable folding
set foldmethod=indent
set foldlevel=99

" Enable folding with the spacebar
nnoremap <space> za


" Setting up indendation

au BufNewFile, BufRead *.py
    \ set tabstop=4 |
    \ set softtabstop=4 |
    \ set shiftwidth=4 |
    \ set textwidth=79 |
    \ set expandtab |
    \ set autoindent |
    \ set fileformat=unix

au BufNewFile, BufRead *.js, *.html, *.css
    \ set tabstop=2 |
    \ set softtabstop=2 |
    \ set shiftwidth=2

highlight BadWhitespace ctermbg=red guibg=darkred
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/

let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>

" setting up pyflakes

let python_highlight_all=1
syntax on

" nerd tree settings
let NERDTreeIgnore=['\.pyc$', '\~$'] "ignore files in NERDTree

" setting up line numbering
set nu

" Rainbow bracket settings
let g:rainbow_active = 1
Enter fullscreen mode Exit fullscreen mode

Just like we did before press ctrl+o to write and Enter to save. Then press Ctrl+x to exit.

  • The next step is to install everything using the plugin manager. Open the terminal and type vim. Now, install the plugin by typing :PluginInstall. This will take some time but it will install all the plugins.

  • The ycmserver error - when you open vim you may encounter some errors but don't worry, it's very easy to fix it. open your terminal and type:

$ pip install cmake
$ cd ~/.vim/bundle/YouCompleteMe
$ ./install.py
Enter fullscreen mode Exit fullscreen mode
  • Let's set up a color scheme for our IDE. We are going to install the oceanic theme for now. fire up your terminal and type:
$ git clone https://github.com/mhartington/oceanic-next.git
$ cd oceanic-next
$ cp colors/OceanicNext.vim ~/.vim/colors
Enter fullscreen mode Exit fullscreen mode

Now open your vimrc and add the color schemes. To do this open your terminal and type :

$ nano ~/.vimrc
Enter fullscreen mode Exit fullscreen mode

this will open your vimrc file. Now paste the code below there:

" Theme
 syntax enable
" for vim 7
 set t_Co=256

" for vim 8
 if (has("termguicolors"))
  set termguicolors
 endif

colorscheme OceanicNext
Enter fullscreen mode Exit fullscreen mode

Now Ctrl+o to write and press Enter to save it. Press Ctrl+x to exit.

Alt Text

Alt Text

Alt Text

Nicely done guys. We made vim a powerful yet lightweight ide.

Features of our new Vim IDE

We did so many things but I did not explain what we were doing or what features we added to our Vim-Ide. Trust me, you guys are going to be surprised when you hear the features of your new ide. Let's get into it now.

  • Split layout - If you open a file with :sp , you split the layout vertically (opening the new file below the current file). If you reverse the keys to :vs , you get a horizontal split (opening the new file to the right of your current file).Key combos: Ctrl + j move to the split below, Ctrl + kmove to the split above, Ctrl + l move to the split to the right,Ctrl + h move to the split to the left.

Alt Text

  • Code Folding - Modern IDEs can fold the codes at functions and import statements, by showing the first line only. You can also do it here by pressing the Space key.

Alt Text

  • Autocomplete - Just like any other IDE, vim can predict what you are going to do next.

Alt Text

  • Super Searching - You can navigate to any file or any tag just by pressing Ctrl + p. This is one of my favorite features of vim.

Alt Text

  • Syntax Checking/Highlighting - After you write the code in vim. Our flake-8 and syntastic plugin will highlight all the errors in your code and also tell you how to fix it.

Alt Text

We have added almost every feature of ide to our vim. You can now code in a feature-rich ide without having to wait an hour to open up.

Small Intro to Vim

I believe some of you have experience using vim. It's okay even if you don't have any, I will give you a small intro of the basics.

  • Modes - Vim is usually opened in command mode, this means that you can write anything but you do all other commands here like we installed the plugins. Commands usually start with :. In order to change from command mode to writing mode press i. You are now in insert mode, you can write everything here and go back to command mode by pressing Esc.

  • Writing, saving, and exiting - When we change to insert mode from command mode(i) we can edit the code. After we finish, we need to save this and exit, but how?
    To do this, you need to go back to command mode by pressing Esc and press :w to write everything. you can exit the editor by using the command :q. If we need to exit without saving, use the command :q!.

  • Additional resource - If you need to learn more about vim and its features, go to vimcasts.

Conclusion

Vim is something every programmer should try at least once. I hope this blog helped someone or encouraged someone to try it out. There are tons of plugins out there, go find yours and try them out. If you find any difficulties, please let me know in the comments. That's it for today and I will be back with another tutorial soon.

Top comments (18)

Collapse
 
nicolus profile image
Nicolus

I haven't tested your setup but it looks more like a nice editor than an IDE (as in "Integrated Development Environment", as in you have all the tools you need to write, test and compile your project). In my opinion here's what you should add to have a true IDE :

  • The ability to run tests and get the results from the IDE
  • The ability to run tests automatically when you change something
  • The ability to place breakpoints and run the debugger from the IDE
  • Git/SVN integration that shows you which lines were changed/added/removed in the gutter, can quickly "git blame" the line you're editing, and of course commit/push from the IDE.
  • An SQL and NoSQL client that integrates with your code (by completing table / column names when you type them in your code for eg.)
  • An HTTP Client to test your APIs right from the IDE.
  • Refactoring features : if you change the name of a variable/function, will it change all the calls automatically ? If you rename a file will it change the rest of your code automatically ? Can you extract a part of code to a new function ? Move some methods to a new class ?
Collapse
 
tacsiazuma profile image
Krisztian Papp
  • vim-test plugin
  • simple autocommand
  • vim-vebugger or vimspector
  • vim-fugitive for commands and vim-signify for gutter.
  • vim-dadbod and vim-dadbod-ui and vim-sql-suggest
  • thats what automated tests for but if you really want it then vim-http plugin
  • that is where it lacks some features, even with coc.nvim, extract features are limited.
Collapse
 
jrhamdi profile image
Hamdi Jr

Thank you for your suggestions.

Collapse
 
nicolus profile image
Nicolus

Great suggestions, thanks !

Collapse
 
daandl profile image
Daan De Lombaert

By the time you've installed and configured all that, you could've just installed PyCharm and be done with it..

Also the more plugins you install, the slower it'll get.

Thread Thread
 
tacsiazuma profile image
Krisztian Papp

And I'd have an IDE which also has a bunch of plugins in order to operate, generally slow and requires a bunch of resources, less configurable, and works for python mostly. Also this configuration is like sharpening an axe. In order to excel in PyCharm you also need to memorize/customize keybindings, etc. The only difference is Vim just requires some of that work upfront.

Collapse
 
jrhamdi profile image
Hamdi Jr

Thank you for your response. I will try to add these features and write another blog soon.

Collapse
 
tacsiazuma profile image
Krisztian Papp

First of all, Vim is anything but easy to learn. That's why the internet is filled with memes about it's steep learning curve. Also, why do you use nano for editing the .vimrc file in a VIM tutorial? And why do you need sudo for that?

Collapse
 
jrhamdi profile image
Hamdi Jr

I used nano because it's beginner-friendly and adding sudo won't cause any errors too.

Collapse
 
tacsiazuma profile image
Krisztian Papp

Yeah, nano is beginner friendly and in any other tutorials I get it why people are using it. But this one is a VIM tutorial :) You could've shown how to copy paste in vim, how to exit it and so much. And your argument "won't cause any errors too" still not justifies the using of sudo.

Thread Thread
 
jrhamdi profile image
Hamdi Jr

Its your personal preference if you want you can use vim. I had troubles using vim before finishing the tutorial.

Collapse
 
ianturton profile image
Ian Turton

It's a bad habit to get into, you should only use sudo when you absolutely need to escalate to root and you should think before doing it.

Thread Thread
 
tooblippe profile image
Tobie

He who plays in root, will eventualy kill tree.

Collapse
 
morggoth profile image
Konstantin Yakovlev

$ cd ~
$ touch ~/.vimrc
$ sudo nano ~/.vimrc

Why use SO many useless commands? That would be enough:

$ vim ~/.vimrc
or
$ nano ~/.vimrc

Collapse
 
sq5rix profile image
Tom

Good job! I use less plugins, and the most important is the mighty coc, which has, basically, everything.

Collapse
 
jrhamdi profile image
Hamdi Jr

Thank you.

Collapse
 
anikethsdeshpande profile image
Aniketh Deshpande

Nice 👍

Collapse
 
jrhamdi profile image
Hamdi Jr

Thank you.