Posts: 242
Joined: Wed Mar 07, 2012 8:35 am

Setting up Vim for Python

Sun Apr 28, 2013 11:07 am

John Anderson has done a detailed guide to doing this here. However, there's quite a lot in that I just don't understand :D

I really just wanted syntax highlighting and sensible indentation. This looked doable:
0) install a bigger version of vim (eg sudo apt-get install vim-gtk; without a GUI version the colours don't seem to work.)
1) get script for syntax highlighting (see this)
2) get sensible indentation (see section on ~/.vim/ftplugin/python.vim , and here).

1 & 2 leave you with ~.vim/ like this:
pi@rp1 ~ $ ls -R .vim

Code: Select all

ftplugin  indent  syntax



3) Uncomment these lines in /etc/vim/vimrc

Code: Select all

if has("autocmd")
  filetype plugin indent on
This worked fine on my (Ubuntu 12.04) laptop, but on the Pi, only the indenting works (still very useful) even though the setup seems near-identical*.

Has anyone got syntax highlighting (esp Python) working on vi on a Pi?

*The Vi versions are 7.3.429 vs 7.3.547 on the Pi). But this seems too close to matter. I've mainly been SSHing into the Pi, but checked vi's behaviour under VNC is the same.

Posts: 1
Joined: Fri Mar 27, 2015 12:47 pm

Re: Setting up Vim for Python

Fri Mar 27, 2015 12:49 pm

It might be just a matter of turning on syntax highlighting with:

:syntax on

Posts: 53
Joined: Fri Aug 09, 2013 2:20 pm

Re: Setting up Vim for Python

Sun Mar 29, 2015 5:08 am

Open up ~/.vimrc, and take a look at it. Here's what I have in mine on my Linux laptop for basic, comfortable Python coding. The next step would be some sort of code intelligence / auto-complete, and some flavor of linting using pylint, flake8, rope, something along those lines. But thats when the plugins start getting kind of hairy. This is enough to get the job done.



Code: Select all

set number          " Turn on line numbers
set ruler           " Turns on ruler
set tabstop=8       " Leaving this set at 8 for compatibility
set smarttab        " Use shiftwidth setting for <tab>
set shiftwidth=4    " Controls the depth of auto-indentation
set expandtab       " Converts all tabs to spaces
set autoindent      " Makes new line indentation match previous line
syntax on           " Turn on syntax highlightin
filetype plugin indent on  " Turns on plugins and automatic indentation
" Turn on certain features specifically for python files
au FileType py set autoindent
au FileType py set smartindent
au FileType py set textwidth=79  " PEP-8 friendly 

User avatar
Posts: 40123
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Setting up Vim for Python

Sun Mar 29, 2015 9:19 am

I had to turn off autoindent, because it drives me insane.

I also upgraded from Vim Tiny to the full-fat version of Vim.

I have these packages installed

Code: Select all

ii  vim                                                         2:7.3.547-7                             armhf        Vi IMproved - enhanced vi editor
ii  vim-addon-manager                                           0.5.2                                   all          manager of addons for the Vim editor
ii  vim-common                                                  2:7.3.547-7                             armhf        Vi IMproved - Common files
ii  vim-runtime                                                 2:7.3.547-7                             all          Vi IMproved - Runtime files
ii  vim-scripts                                                 20121007                                all          plugins for vim, adding bells and whistles
ii  vim-tiny                                                    2:7.3.547-7                             armhf        Vi IMproved - enhanced vi editor - compact version
ii  vim-vimerl                                                  1.4.1+git20120509.89111c7-2             all          Erlang plugin for Vim
ii  vim-vimerl-syntax                                           1.4.1+git20120509.89111c7-2    
Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
Posts: 168
Joined: Mon Dec 30, 2013 6:43 pm

Re: Setting up Vim for Python

Mon Mar 30, 2015 11:36 am

I'd also recommend Syntastic [1] and flake8 [2] for vim

[1] https://github.com/scrooloose/syntastic
[2] https://github.com/PyCQA/flake8

Return to “Python”