Python
Vim for Python Programming

Procedure Manual: Setting Up Vim for Smooth Python Development with Autosuggestions

This guide walks through setting up a Vim configuration (.vimrc) to create an efficient Python scripting environment with autosuggestions, syntax highlighting, linting, and easy navigation.

Step 1: Install Vim-Plug (Plugin Manager for Vim)

  1. Download Vim-Plug by running the following command in your terminal:

    curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
        https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  2. This command installs Vim-Plug to manage plugins, allowing you to easily install, update, and remove plugins.

Step 2: Set Up the .vimrc Configuration File

Create or edit the .vimrc file in your home directory with this content. This configuration will set up Python-specific development features, including autocompletion, syntax highlighting, and fuzzy file navigation.

" Plugin Manager - vim-plug setup
call plug#begin('~/.vim/plugged')
 
" Syntax highlighting and language support
Plug 'sheerun/vim-polyglot'
 
" Python development support with autocompletion
Plug 'davidhalter/jedi-vim'
 
" Autocompletion and linting with coc.nvim (requires Node.js)
Plug 'neoclide/coc.nvim', {'branch': 'release'}
 
" Fuzzy finder for easy file navigation (optional but useful)
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
 
call plug#end()
 
" Basic Vim settings for Python development
syntax on
filetype plugin indent on
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set number
set relativenumber
set cursorline
set wrap
set showmatch
 
" coc.nvim setup for autocompletion
let g:coc_global_extensions = ['coc-pyright', 'coc-snippets', 'coc-pairs']
inoremap <silent><expr> <TAB> pumvisible() ? "\<C-n>" : "\<TAB>"
inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
 
" Format on save with Black
autocmd BufWritePre *.py execute ':Black'
 
" Linting with flake8
let g:python_host_prog = '/usr/bin/python3' " Adjust Python path
let g:coc_enable_lint_on_save = 1
 
" Fuzzy finder keybindings
nnoremap <leader>ff :Files<CR>
nnoremap <leader>fg :GFiles<CR>
nnoremap <leader>fb :Buffers<CR>
nnoremap <leader>fl :Lines<CR>
 
" Run Python script with <leader>r
nnoremap <leader>r :!python3 %<CR>
 
" Jedi-vim for Python autocompletion and navigation
let g:jedi#completions_enabled = 1
let g:jedi#use_tabs_not_buffers = 1
let g:jedi#goto_assignments_command = "<leader>d"
let g:jedi#usages_command = "<leader>u"
 
" General settings
set wildmenu
set ignorecase
set smartcase
set incsearch
set hlsearch

Step 3: Install Plugins

  1. Open Vim and run the following command to install plugins:

    :PlugInstall
  2. Vim-Plug will automatically download and set up the plugins specified in the .vimrc file.

Step 4: Additional Setup for coc.nvim

  1. Install Node.js: coc.nvim requires Node.js for the Language Server Protocol. If Node.js is not installed on your system, download it from nodejs.org (opens in a new tab) and follow the installation instructions.
  2. Configure coc.nvim Extensions: The configuration uses coc-pyright for Python Language Server support and coc-snippets for snippets, which provide autocompletion and code suggestions for Python.

Step 5: Customize Python Path (Optional)

If you have multiple Python versions or a virtual environment, set the Python path explicitly in the .vimrc:

let g:python_host_prog = '/path/to/your/python'

Step 6: Verify Installation and Start Coding

  1. Open a Python file in Vim to test your configuration.
  2. You should see syntax highlighting, and as you type, coc.nvim will show autocomplete suggestions.
  3. Use <TAB> for autocompletion, <leader>r to run Python files, and the fuzzy finder commands (e.g., <leader>ff) for quick file navigation.

This setup should provide a streamlined, efficient Python development environment within Vim. Enjoy coding!