This will be a quick guide on how to setup neovim as your python IDE. It might invovle some concepts, but they shouldn’t block your setup as long as you follow these steps (hopefully).

Here are the tools/software that we need:

  • neovim
  • python3
  • git

Install

You can install neovim according to the offical document here. If you are using windows, you can always use the pre-built latest stable release provided by the neovim team.

I am using neovim v0.3.1 at the time of writing showing as following:

Python3 and Neovim package

You need to have python3 installed and also install neovim package using pip

1
pip install neovim

and then we can check if we have successfully configured python component for neovim by running the following command

1
:echo has('python3')

and check if it outputs 1 like the following,

Plugin Manager

There are many neovim/vim plugin managers like dein.vim and vim-plug. Here I am showing vim-plug in the below figures. If you are using windows and don’t have PowerShell, then you can download the plug.vim (as suggested by the vim-plug document) and put it at

~\AppData\Local\nvim\autoload

Configuration file

Create the following configuration file based on the system you use:

  • Unix: ~/.config/nvim/init.vim
  • Windows: ~\AppData\Local\nvim\init.vim

this file will store all of our configurations about neovim such as:

  • UI related settings
  • certain behaviors you want neovim to behave
  • plugins and related settings

Create the init.vim file in the above location if it’s not there yet.

Plugin Manager

There are many neovim/vim plugin managers like dein.vim and vim-plug. Here I am showing vim-plug in the below figures. If you are using windows and don’t have PowerShell, then you can download the plug.vim (as suggested by the vim-plug document) and put it at

~\AppData\Local\nvim\autoload

After we created the init.vim file, paste the following contents in it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
if has('win32') || has('win64')
let g:plugged_home = '~/AppData/Local/nvim/plugged'
else
let g:plugged_home = '~/.vim/plugged'
endif
" Plugins List
call plug#begin(g:plugged_home)
" UI related
Plug 'chriskempson/base16-vim'
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
" Better Visual Guide
Plug 'Yggdroot/indentLine'
" syntax check
Plug 'w0rp/ale'
" Autocomplete
Plug 'ncm2/ncm2'
Plug 'roxma/nvim-yarp'
Plug 'ncm2/ncm2-bufword'
Plug 'ncm2/ncm2-path'
Plug 'ncm2/ncm2-jedi'
" Formater
Plug 'Chiel92/vim-autoformat'
call plug#end()
filetype plugin indent on

The lines above tell neovim and vim-plug which plugins (and their github repo name) that we need for our vim. And vim-plug will do a git clone for each of them.

Then, close your neovim and reopen it, now neovim will load the setting above. And then we run the following command, neovim will install the plugins we put into init.vim above

:PlugInstall

After this, we paste the following content into our configuration file below the content showing above.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
" Configurations Part
" UI configuration
syntax on
syntax enable
" colorscheme
let base16colorspace=256
colorscheme base16-gruvbox-dark-hard
set background=dark
" True Color Support if it's avaiable in terminal
if has("termguicolors")
set termguicolors
endif
if has("gui_running")
set guicursor=n-v-c-sm:block,i-ci-ve:block,r-cr-o:blocks
endif
set number
set relativenumber
set hidden
set mouse=a
set noshowmode
set noshowmatch
set nolazyredraw
" Turn off backup
set nobackup
set noswapfile
set nowritebackup
" Search configuration
set ignorecase " ignore case when searching
set smartcase " turn on smartcase
" Tab and Indent configuration
set expandtab
set tabstop=4
set shiftwidth=4
" vim-autoformat
noremap <F3> :Autoformat<CR>
" NCM2
augroup NCM2
autocmd!
" enable ncm2 for all buffers
autocmd BufEnter * call ncm2#enable_for_buffer()
" :help Ncm2PopupOpen for more information
set completeopt=noinsert,menuone,noselect
" When the <Enter> key is pressed while the popup menu is visible, it only
" hides the menu. Use this mapping to close the menu and also start a new line.
inoremap <expr> <CR> (pumvisible() ? "\<c-y>\<cr>" : "\<CR>")
" uncomment this block if you use vimtex for LaTex
" autocmd Filetype tex call ncm2#register_source({
" \ 'name': 'vimtex',
" \ 'priority': 8,
" \ 'scope': ['tex'],
" \ 'mark': 'tex',
" \ 'word_pattern': '\w+',
" \ 'complete_pattern': g:vimtex#re#ncm2,
" \ 'on_complete': ['ncm2#on_complete#omni', 'vimtex#complete#omnifunc'],
" \ })
augroup END
" Ale
let g:ale_lint_on_enter = 0
let g:ale_lint_on_text_changed = 'never'
let g:ale_echo_msg_error_str = 'E'
let g:ale_echo_msg_warning_str = 'W'
let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
let g:ale_linters = {'python': ['flake8']}
" Airline
let g:airline_left_sep = ''
let g:airline_right_sep = ''
let g:airline#extensions#ale#enabled = 1
let airline#extensions#ale#error_symbol = 'E:'
let airline#extensions#ale#warning_symbol = 'W:'

Close neovim and reopen it again, then we have this

Code completion and code lint/formatting

In order to have code completion, syntax checking, and code formatting, we need the following python packages

  • jedi for code completion: pip install jedi
  • flake8 for code linting: pip install flake8
  • autopep8 for code formatting: pip install autopep8

Then, we have our python coding environment in our neovim. Suppose we have the following python code,

1
2
3
4
5
from numpy.random import normal
def simple_function(a, b, c=0):
d = a + b
print(a + b + c)

And neovim will display the following two errors/warnings:

So let’s fix them… note that we also have the autocompletion too!

Notice that we have another error/warning showing stating that

E231: missing whitespace after ‘,’

Now if we run the following command

:Autoformat or hit <F3> since we mapped it in our configuration file

we will notice that the error E231 is resolved after code reformatting.

since code linting and formatting rely on separate python uilities (flake8 and autopep8), which are isolated from our editor. So we can set the behavior of them such as