4974
Comment: Add FileType configuration
|
← Revision 39 as of 2013-06-18 05:14:05 ⇥
5089
|
Deletions are marked like this. | Additions are marked like this. |
Line 17: | Line 17: |
autocmd BufRead,BufNewFile *.py syntax on autocmd BufRead,BufNewFile *.py set ai autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,with,try,except,finally,def,class |
syntax on filetype indent plugin on |
Line 29: | Line 28: |
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |
Line 38: | Line 37: |
(In some versions of Ubuntu, for example, the modeline option has apparently been disabled for security reasons.) | (In Debian and Ubuntu, for example, the modeline option has been disabled for security reasons.) |
Line 43: | Line 42: |
:set tabstop=4 expandtab shiftwidth=4 softtabstop=4 | :set tabstop=8 expandtab shiftwidth=4 softtabstop=4 |
Line 49: | Line 48: |
:set ts=4 et sw=4 sts=4 | :set ts=8 et sw=4 sts=4 |
Line 52: | Line 51: |
If you want to do this automatically for all files identified as Python add this: | If you want to do this automatically for all files identified as Python, add the following to {{{~/.vim/ftplugin/python.vim}}}. Create the directory and/or file if it does not already exist. |
Line 55: | Line 54: |
au FileType python setl autoindent tabstop=4 expandtab shiftwidth=4 softtabstop=4 | set tabstop=8 set expandtab set shiftwidth=4 set softtabstop=4 |
Line 60: | Line 62: |
You may be lucky enough to have syntax highlighting already switched on in your version of Vim. If not, edit a `vimrc` file (either `/etc/vimrc` or `.vimrc` in your home directory) and add the following: | You may be lucky enough to have syntax highlighting already switched on in your version of Vim. If not, edit a `vimrc` file (either `/etc/vimrc` or, preferably,`.vimrc` in your home directory) and add the following: |
Line 66: | Line 68: |
If you use a dark background, this command may help adjust the colours: | If you use a dark background, this command may help adjust the default colours for better contrast: |
Line 74: | Line 76: |
Some find that the methods described above does not work. An alternative method is adding... | Some find that the methods described above do not work. An alternative method is adding... |
Line 77: | Line 79: |
set autoindent set tabstop=4 |
set tabstop=8 |
Line 80: | Line 81: |
set softtabstop=4 | |
Line 81: | Line 83: |
filetype indent on | filetype indent on |
Line 84: | Line 86: |
...to your `~/.vimrc` file. The first rule sets automatic tab indentation. The second rule sets tab stops to four characters wide. The third converts tabs to white space. `set shiftwidth` sets the width for autoindents. Finally, the last rule allows auto-indenting depending on file type. With this method, tab settings do not need to be set in your python file and the `# vim: ...` line in the template below is not needed. | ...to your `~/.vimrc` file. The first rule sets tab stops to [[http://docs.python.org/reference/lexical_analysis.html#indentation|eight characters wide]]. The second converts tabs to white space. The third makes the Tab key indent by four spaces. `set shiftwidth` sets the width for autoindents. Finally, the last rule allows auto-indenting depending on file type. With this method, tab settings do not need to be set in your python file and the `# vim: ...` line in the template below is not needed. |
Line 97: | Line 99: |
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |
Line 109: | Line 111: |
* Python-mode for Vim: [[http://www.youtube.com/watch?v=67OZNp9Z0CQ&feature=share|Screencast]] | |
Line 110: | Line 113: |
* [[http://www.sontek.net/post/Python-with-a-modular-IDE-(Vim).aspx|Python with a modular IDE (Vim)]], June 24, 2009 23:48 by John Anderson (sontek) | * [[http://sontek.net/turning-vim-into-a-modern-python-ide|Turning Vim into a modern Python IDE]], 2011, Anderson (sontek) |
Line 113: | Line 116: |
Vi Improved
VI Improved (Vim) is an improved version of the editor "vi", one of the standard text editors on UNIX systems. It has all the features you'll ever need from an editor, and probably three times that many more that you'll never use The newer versions also include a 'vimdiff' mode that you can use to diff and merge file(s). Oh, I didn't mention it's also scriptable in Python, and there's a graphical version: GVIM. Get it from http://www.vim.org/.
Vim is also available in your favourite OS. Since version 6.0 it has folding. Folding makes your life easy when you have some long files.
You can download many scripts from http://www.vim.org/ and learn new tips from the site http://vim.wikia.com/wiki/Main_Page
Vim 7.0 (released mid-2006) includes the Intellisense-like omni-completion for several languages. Here is the latest version of pythoncomplete.
Configuring Vim
You can automatically enable syntax coloring and automatic indentation for Python code by adding the following lines to your ~/.vimrc file:
syntax on filetype indent plugin on
The following sections correspond to the guidelines from the HowToEditPythonCode page.
Indentation
A useful addition to Python source files is this comment:
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
This may need the modeline option enabled in your ~/.vimrc file:
set modeline
(In Debian and Ubuntu, for example, the modeline option has been disabled for security reasons.)
The above # vim: ... text, when embedded in a source file, tells Vim that when the file is loaded, tabs are always expanded to spaces and that the width of each tab is four characters. Type the following in command mode to achieve the same effect:
:set tabstop=8 expandtab shiftwidth=4 softtabstop=4
Or:
:set ts=8 et sw=4 sts=4
If you want to do this automatically for all files identified as Python, add the following to ~/.vim/ftplugin/python.vim. Create the directory and/or file if it does not already exist.
set tabstop=8 set expandtab set shiftwidth=4 set softtabstop=4
Syntax Highlighting
You may be lucky enough to have syntax highlighting already switched on in your version of Vim. If not, edit a vimrc file (either /etc/vimrc or, preferably,.vimrc in your home directory) and add the following:
syntax on
If you use a dark background, this command may help adjust the default colours for better contrast:
set background=dark
Alternative
Some find that the methods described above do not work. An alternative method is adding...
set tabstop=8 set expandtab set softtabstop=4 set shiftwidth=4 filetype indent on
...to your ~/.vimrc file. The first rule sets tab stops to eight characters wide. The second converts tabs to white space. The third makes the Tab key indent by four spaces. set shiftwidth sets the width for autoindents. Finally, the last rule allows auto-indenting depending on file type. With this method, tab settings do not need to be set in your python file and the # vim: ... line in the template below is not needed.
A Simple Template
You could copy the following simple template and save it to a file somewhere. Then, when you need to make a new source file, just copy it to the intended location with a name of your choice.
#!/usr/bin/env python
"""
Python source code - replace this with a description of the code and write the code below this text.
"""
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
This contains useful UNIX-related information on the first line, and a docstring which can be used to describe what your program or module is about. As noted above, to work this requires modeline support to be enabled.
Scripting Vim with Python
There is a presentation given by Sean Reifschneider about scripting Vim with Python: Vim and Python: Two Great Tastes that Taste Great Together If you want to access the visual selection from Vim in Python read http://www.tummy.com/journals/entries/jafo_20070301_035949
Links
Python-mode for Vim: Screencast
Some hints about configuring Vim: Notes on using Vim with Python
Turning Vim into a modern Python IDE, 2011, Anderson (sontek)
VIM as Python IDE, 2009-05-24, Alain M. Lafon
Python and vim: Make your own IDE, February 16, 2009 14:28, Samuel Huckins