Background Link to heading

This article explains how to optimise vim for Python development. I explored this route because recently I was looking for a lightweight alternative to the Python IDE I currently use (PyCharm). I use vim for general system admin tasks so it seemed like the best way forward was to tweak my favourite text editor into a simple IDE!

Optimise Vim Link to heading

  • Added following to ~/.vimrc (to ensure highlighting enabled)
syntax on
filetype indent plugin on
  • Created Python-specific vim settings file to ensure tabs function correctly
[stuart@asuka ~]$ mkdir -p ~/.vim/ftplugin/
[stuart@asuka ~]$ vim ~/.vim/ftplugin/python.vim
    set tabstop=8
    set expandtab
    set shiftwidth=4
    set softtabstop=4
    set autoindent
  • Installed additional packages (for correct syntax & graphical implementation)
[stuart@asuka ~]$ sudo dnf install vim-syntastic-python.noarch vim-X11.x86_64 vim-nerdtree.noarch
  • Add NERDTree aliases to ~/.vimrc for quicker toggle & showing hidden files (now, the vim command :NT will toggle whether the Nerd Tree directory tree is shown)
command NT NERDTreeToggle
let NERDTreeToggleHidden=1
  • Followed instructions on github for Pretty-Vim-Python
  • At the end of this configuration, my ~/.vimrc looks as follows
syntax on
filetype indent plugin on
command NT NERDTreeToggle
let NERDTreeShowHidden=1
colorscheme molokai
highlight Comment cterm=bold

New Functionality Link to heading

In conclusion, the above changes allow for:

  • Tabs to be replaced with spaces (due to PEP-8, the recommended/most common indentation format is with spaces, not tabs)
  • Syntax highlighting (for multiple languages)
  • GUI application that can be launched with the command gvim (it can also be launched from the display manager application menu)

Using NerdTree Link to heading

While there are tons of useful features in NerdTree, I will give a crash course of its usage below:

  • Toggle NerdTree with :NT
  • Open/Close directories with [Enter]
  • Set cwd to selected directory with cd
  • Set directory tree root to directory with C
  • Open file in new tab with t

Furthermore,  you can access additional usage info for NerdTree with :help NERD_tree.txt.

NerdTree Bookmarks Link to heading

One of the most noteworthy features of NerdTree is the bookmarks

  • Bookmark the selected file or directory (while browsing directories with NerdTree) with the command :Bookmark **BookmarkName** (so the file/directory will be aliased to BookmarkName)
  • If the bookmark is a directory you can navigate, and set the document root, to the existing bookmark with :BookmarkToRoot **BookmarkName**
  • If the bookmark is a file then it can be opened with :OpenBookmark **BookmarkName**

References Link to heading

https://wiki.python.org/moin/Vim http://www.vim.org/scripts/script.php?script_id=1658 https://github.com/sentientmachine/Pretty-Vim-Python/