Your ultimate VIM setup for Python

Your ultimate VIM setup for Python

As a fellow developer, I have at least 4 different IDEs, VSCode, Visual Studio, and Jupyter Lab, among others.

Nevertheless, there is one IDE that has been there always for me regardless of my approach to new and sexy development environments. I'm referring to my old friend VIM.

I'll be completely honest with you, when I started using VIM I really panic, I was wondering who in heaven would build such painful IDE. I really suffered, like many of you as newcomers. But when I landed my first IT job, I realized that I didn't have many options but to use VI (The old pal) to create and edit files on servers. That's why I decided to go all-in with VI/VIM and give it a chance of becoming my main IDE.

I faced many challenges, such as how to create files, insert text, add new lines, remove lines, etc. all those tasks that you may find as part of the VIM learning curve.

do you really think that can exit VIM?

The goal of this post is not to teach you how to use VIM (yet) but to share with you my VIM setup for Python development, along with some items that you must check if you are working on *Nix environments (Unix / Linux / Mac)

Checking on VIM

First things first, let's check your current VIM deployment. To be honest, I've never found a macOS without VIM, but, there is a catch. We need to ensure that VIM is installed with Python 3 support. You will get messages like the one below when using certain plugins otherwise.

Warning: vim: this formula has no --with-python3 option so it will be ignored!

If that's the case, let's check whether you have Python 3 support.

If you can't see a + next to the python3 option, you need to completely remove VIM and re-install it following the following recipe.

vim --version | grep python

Installing VIM

You must run the following command to check whether VIM is installed in your OS first.

which vim

If you get an output similar to the one below, that means that VIM is not installed.

vim not found

To deploy VIM, and several other macOS Packages, I recommend Brew. To install it you must run the following command.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"

Once installed, let's make a few tweaks to your environment profile to ensure that we can install VIM with Python 3 support.

Open your environment profile file (~/.bash or ~/.zshrc) and add the next alias.

alias python="python3"

Save the file and reload your profile by running

source ~/.bashrc

or the below one for those using ZSH

source ~/.zshrc

Now, it's time to install VIM.

brew install vim

Follow Brew instructions to complete the installation process.

Once installed, checked whether you have Python 3 support.

vim --version | grep python

If you see the + next to python3, you are done. Now we can move to the next step.

VIM Python plugins and setup

I don't consider myself a VIM guru, so, I set up my environment based on my needs, so keep in mind that what may be a good fit for me, may not be the tool you need to carry your tasks. Anywho, I will be sharing my current VIM configuration.

Before we proceed, We assume that you have some experience managing plugins with VIM. I particularly use VIM-PLUG , so if you use another tool, like Vundle, you may need to tweak the below settings.

First, the obvious, backup your current ~/.vimrc file. Once you have a safe copy of your current settings, just edit the file, erase everything, and copy and paste the below lines.

Here you have many plugins, we can highlight Jedi-Vim for autocompletion, Vim-Polyglot a collection of language packs for Vim, or NerdTree, a file manager for VIM, among others, as well as settings for auto-indent, line numbers, and additional configurations that will make your life easier.

" Options
set encoding=utf8
set clipboard=unnamedplus " Enables the clipboard between Vim/Neovim and other applications.
set completeopt=noinsert,menuone,noselect " Modifies the auto-complete menu to behave more like an IDE.
set cursorline " Highlights the current line in the editor
set hidden " Hide unused buffers
set autoindent " Indent a new line
set mouse=a " Allow to use the mouse in the editor
set number " Shows the line numbers
set splitbelow splitright " Change the split screen behavior
set title " Show file title
set wildmenu " Show a more advance menu
set guifont=hack_nerd_font:h11
"set cc=100 " Show at 80 column a border for good code style                      
filetype plugin indent on   " Allow auto-indenting depending on file type
syntax on
set spell " enable spell check (may need to download language package)
set ttyfast " Speed up scrolling in Vim`:wq

let g:kite_supported_languages = ['python', 'javascript']

call plug#begin('~/.vim/plugged')
Plug 'morhetz/gruvbox'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'ryanoasis/vim-devicons'
Plug 'scrooloose/nerdcommenter'
Plug 'sheerun/vim-polyglot'
Plug 'jiangmiao/auto-pairs'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'tpope/vim-fugitive'
Plug 'davidhalter/jedi-vim'
Plug 'vim-scripts/indentpython.vim'
Plug 'Xuyuanp/nerdtree-git-plugin'
Plug 'tiagofumo/vim-nerdtree-syntax-highlight'
Plug 'PhilRunninger/nerdtree-visual-selection'
" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'
" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'
" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'
" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'tpope/vim-fireplace', { 'for': 'clojure' }
call plug#end()

"colorscheme gruvbox
let g:bargreybars_auto=0
let g:airline_solorized_bg='dark'
let g:airline_powerline_fonts=1
let g:airline#extension#tabline#enable=1
let g:airline#extension#tabline#left_sep=' '
let g:airline#extension#tabline#left_alt_sep='|'
let g:airline#extension#tabline#formatter='unique_tail'
let NERDTreeQuitOnOpen=1

let g:WebDevIconsUnicodeDecorateFolderNodes = 1
let g:WebDevIconsUnicodeDecorateFolderNodeDefaultSymbol = '#'
let g:WebDevIconsUnicodeDecorateFileNodesExtensionSymbols = {}
let g:WebDevIconsUnicodeDecorateFileNodesExtensionSymbols['nerdtree'] = '#'


autocmd vimenter * NERDTree
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
map <C-n> :NERDTreeToggle<CR>

Save the file and open VIM. Within the main page press the : key and type

source%

press again : and type

PlugInstall

This command will download and install all the plugins listed above.

Finally, if you reopen and edit a .py file, you should be able to see something like the screen capture below. File Manager, Line Numbers, Code Autocomplete, and recommendations. SWEET! isn't?

VIM Set up

Summary

Having the one-and-for-all IDE is quite a challenge, but I think that VIM is getting closed. Not only is blasting fast, but you have many plugins and customizations that can make your life easier.

VIM has quite a learning curve, I get it, but if you commit for a week, to using it on a daily basis, I can certainly tell you that you will eventually love it. That doesn't mean that you will stop using other IDEs, but many of the modern solutions have a VIM keyboard shortcuts plugin that can help you to boost your proficiency with VIM.

Hope you have found this post informative. Feel free to share it, we want to reach as many people as we can because knowledge must be shared, right?

If you reach this point, Thank you!

<AL34N!X>