Skip to content

Instantly share code, notes, and snippets.

@rochacbruno
Last active September 10, 2025 09:23
Show Gist options
  • Select an option

  • Save rochacbruno/88d7b7b02a16c6971c4bbdda7023f3a1 to your computer and use it in GitHub Desktop.

Select an option

Save rochacbruno/88d7b7b02a16c6971c4bbdda7023f3a1 to your computer and use it in GitHub Desktop.
Minimal Possible VIM COnfig for Python with AutoComplete, Help, Syntax Check
let mapleader=' '|set nu rnu hid bs=2 ts=4 sw=4 et ai si nocp wmnu tw=79 cc=80 stal=2 cot=menuone,longest,preview ph=10 ruler statusline=%f%m%r%h%w%=%y\ %l,%c\ %p%%
let g:p=executable('uv')?'uv run python3':'python3'|let g:d=g:p.' -m pydoc'|let g:c=g:p.' -m py_compile'
filetype plugin indent on|sy on|nn <leader>b :ls<cr>:b<space>|nn <leader><Tab> <C-^>
au FileType python setl ofu=python3complete#Complete cot=menuone,longest,preview inc=^\s*\(from\|import\) def=^\s*\(def\|class\)|exe 'setl kp='.escape(g:d,' ')|nn <buffer> <leader>k :exe 'vert term ++close '.g:d.' '.expand('<cword>')<CR>|nn <buffer> <leader>r :w<CR>:exe '!'.g:p.' %'<CR>
au BufWritePost *.py silent! exe '!'.g:c.' %'
au CompleteDone * pc

Minimal vim for Python

This .vimrc has extremely compacted lines of config to make vim work for python

Warning

If you have an existing ~/.vimrc backup it first mv ~/.vimrc ~/.vimrc_backup

curl -LsSf https://gist.githubusercontent.com/rochacbruno/88d7b7b02a16c6971c4bbdda7023f3a1/raw/bafb1ad84ebc06ab8536ef0b8c9a94333167773d/.vimrc -o ~/.vimrc

Features

  • Space + B List buffers
  • Space + Tab Previous File
  • Space + R Run Script
  • Space + K Help for Keyword
  • Control + X + O Trigger auto completion

Normal mode

  • Buffer List Space + B
    • then type number of the buffer 1 or part of the filename scrip and then enter
  • Switch to previously edited buffer Space + Tab
  • Run file Space + R
  • Open help for WORD under cursor Space + K
  • Syntax Check :w (use Control + L to clean messages)

Insert mode

  • Auto Complete with preview
    • Assume | is the cursor position
    • Start typing and object and dot sys.| and press Control + X + O
    • Popup with auto completion shows
    • Arrows to navigate
      • OR use Control + N and Control + P
    • Control + O to accept selection
    • Control + E to cancel selection

Requirements

  • Vim9 with +python3
    • packages: vim-nox, gvim or compile your own vim with Python suppport
    • Check: vim --version | grep python IF you see +python or +python3 that's ok
      you may need to change python3complete to pythoncomplete on the script.
  • Python3 or uv

Normal mode

  • Shift–K → Python help for word under cursor
  • Space + K → Vertical Python help for word under cursor
  • Space + R → Run file

Insert mode

  • Ctrl–X Ctrl–O → Omnifunc completion (Python-aware)
  • Ctrl–X Ctrl–N → Keyword completion from current file
  • Ctrl–X Ctrl–I → Keyword completion from included files
  • Ctrl–X Ctrl–K → Dictionary completion
  • Ctrl–N → Next match
  • Ctrl–P → Previous match
  • Ctrl–O → Accept match
  • Ctrl–L → Clear error message when saving with failure
  • Ctrl-w z → Close the preview window
alias uvim="vim -u <(cat << 'EOF'
let mapleader=' '|set nu rnu hid bs=2 ts=4 sw=4 et ai si nocp wmnu tw=79 cc=80 stal=2 cot=menuone,longest,preview ph=10 ruler statusline=%f%m%r%h%w%=%y\ %l,%c\ %p%%
let g:p=executable('uv')?'uv run python3':'python3'|let g:d=g:p.' -m pydoc'|let g:c=g:p.' -m py_compile'
filetype plugin indent on|sy on|nn <leader>b :ls<cr>:b<space>|nn <leader><Tab> <C-^>
au FileType python setl ofu=python3complete#Complete cot=menuone,longest,preview inc=^\s*\(from\|import\) def=^\s*\(def\|class\)|exe 'setl kp='.escape(g:d,' ')|nn <buffer> <leader>k :exe 'vert term ++close '.g:d.' '.expand('<cWORD>')<CR>|nn <buffer> <leader>r :w<CR>:exe '!'.g:p.' %'<CR>
au BufWritePost *.py silent! exe '!'.g:c.' %'
au CompleteDone * pc
EOF
)"
" Simple defaults
let mapleader = ' '
set number relativenumber
set tabstop=4
set shiftwidth=4
set expandtab autoindent smartindent nocompatible wildmenu
set textwidth=79
set colorcolumn=80
set showtabline=2
set backspace=2
set hidden
set ruler
set statusline=%f%m%r%h%w%=%y\ %l,%c\ %p%%
" Python executable detection
if executable('uv')
let g:py_exec = 'uv run python3'
let g:pydoc = 'uv run python3 -m pydoc'
let g:pycomp = 'uv run python3 -m py_compile'
else
let g:py_exec = 'python3'
let g:pydoc = 'python3 -m pydoc'
let g:pycomp = 'python3 -m py_compile'
endif
" Enable filetype detection and plugins
filetype plugin indent on
syntax enable
" Better completion behavior
set completeopt=menuone,longest,preview
set pumheight=10 " Limit popup menu height
nnoremap <leader>b :ls<cr>:b<space>
nnoremap <leader><Tab> <C-^>
" Python-specific settings and mappings
augroup python_settings
autocmd!
autocmd FileType python setlocal omnifunc=python3complete#Complete
autocmd FileType python setlocal completeopt=menuone,longest,preview
autocmd FileType python let &l:keywordprg = g:pydoc
autocmd FileType python setlocal include=^\s*\(from\|import\)
autocmd FileType python setlocal define=^\s*\(def\|class\)
autocmd FileType python nnoremap <buffer> <leader>k :execute 'vert term ++close ' . g:pydoc . ' ' . expand('<cword>')<CR>
autocmd FileType python nnoremap <buffer> <leader>r :w<CR>:execute '!' . g:py_exec . ' %'<CR>
autocmd BufWritePost *.py execute 'silent! !' . g:pycomp . ' %'
autocmd CompleteDone * pclose
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment