Skip to content

Instantly share code, notes, and snippets.

@nikhilsaldanha
Last active March 26, 2019 09:31
Show Gist options
  • Select an option

  • Save nikhilsaldanha/d382d206f83d14c85e97ba1782e736c7 to your computer and use it in GitHub Desktop.

Select an option

Save nikhilsaldanha/d382d206f83d14c85e97ba1782e736c7 to your computer and use it in GitHub Desktop.
good vimrc config for python
set nocompatible " required
filetype off " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')
" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'
" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)
Plugin 'tmhedberg/SimpylFold'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'Valloric/YouCompleteMe'
Plugin 'vim-syntastic/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'
Plugin 'scrooloose/nerdtree'
Plugin 'Lokaltog/powerline', {'rtp': 'powerline/bindings/vim/'}
" All of your Plugins must be added before the following line
call vundle#end() " required
filetype plugin indent on " required
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>
" python specific vim config for text formatting
au BufNewFile,BufRead *.py
\ set tabstop=4 |
\ set softtabstop=4 |
\ set shiftwidth=4 |
\ set textwidth=79 |
\ set expandtab |
\ set autoindent |
\ set fileformat=unix
" flag unnecessary whitespace with red
highlight BadWhitespace ctermbg=red guibg=darkred
au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/
" set encoding
set encoding=utf-8
let g:ycm_autoclose_preview_window_after_completion=1
"python with virtualenv support
py3 << EOF
import os
import sys
import vim
if 'VIRTUAL_ENV' in os.environ:
project_base_dir = os.environ['VIRTUAL_ENV']
sys.path.insert(0, project_base_dir)
activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
exec(compile(open(activate_this, "rb").read(), activate_this, "exec"), dict(__file__=activate_this))
EOF
let python_highlight_all=1
syntax on
if has('gui_running')
set background=dark
colorscheme solarized
else
colorscheme zenburn
endif
call togglebg#map("<F5>")
map <C-n> :NERDTreeToggle<CR>
set nu

How to setup this config(on Ubuntu)

  1. Delete tiny vim and install vim version > 7

    sudo apt-get remove vim vim-tiny
    sudo apt-get update
    sudo apt-get install vim
    
    • Now vim, should display version > 7
  2. Install the Vundle plugin manager for vim git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

  3. Create a vimrc file with all the Plugins

    • replace your ~/.vimrc file with the one available with this gist and save the file.
    • Then open vim and run the command :PluginInstall. Vundle should install all the plugins which may take a while.
    • Once it is done, it will indicate this at the bottom of the screen. You can then go ahead and close all screens using :qall
  4. Compile YouCompleteMe(python auto-completer)

    • Now, if you open up any python file, you should get a warning saying that YouCompleteMe is not compiled and the server is down.
    • To compile we first need cmake sudo apt install build-essential cmake python3-dev
    • Then we cd into ~/.vim/bundle/YouCompleteMe and run python3 install.py --clang-completer
  5. To be able to use the flake8 linter plugin, make sure you have flake8 installed using pip install flake8.

Usage:

  1. Code Folding

    • The code is automatically folded on opening.
    • To open a section of the code, move the cursor to the folded text and press zo in command mode.
    • To close(fold) the code, press zc.
    • :help fold-commands for the full set of commands
  2. To run pep8 linting on the code, press F7. It lists the full set of issues with the code, use arrow keys to select and press enter to go to the issue.

  3. To bring up the directory tree, you can press ctrl+n. It brings up the current directory on the left of the current file. You can use arrows to select and press enter to choose a different file to open without closing the current file. To switch between choosing directory and editing file, you can press ctrl+h (to go left) and ctrl+l(to go right).

For more customizations and options, check out the plugins:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment