7/27/2016

Help for Vi or Vim in Python

Help for Vi or Vim in Python

Using vi or vim can be tricky to be sure! It has syntax color coding for Python, but the way it handles tabs and spaces can be problematic. It's also nice to be able to control the indentation of the text in the editor. The following settings can help with these issues.

Hidden Tabs and Spaces

Using :set list can display the hidden tabs and spaces that might cause you problems.

Custom Vim Configuration

If you want to make vi or vim your primary editor, then you can use a custom resource ~/.vimrc file to store preferences that will make using it easier with Python.


~/.vimrc Comments

One thing to note is that comments in a vi or vim resource file are different from most configuration files. To add a comment to the custom file you use the quote (") as shown below.

Python ~/.vimrc Example

As someone who writes Python frequently, I like my tab key to indent four spaces, and convert the tabs into a spaces. I like to also be able to select text to indent or un-indent it for Python levels of scope. Here are the settings I add to my ~/.vimrc file:

set expandtab " expand tabs to spaces
set tabstop=4 " use four spaces for each tab
set shiftwidth=4 " select with V and use  < or > to shift for spaces

In order to take advantage of the shiftwidth, you can press V and then select the lines to indent, or un-indent. Pressing < or > will un-indent or indent the selected lines.

No comments:

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.