m (moved Outline editing of Python programs to Syntax folding of Python files: Better fit the content of the tip.) |
JohnBeckett (talk | contribs) (some rewording referring to other tips) |
||
Line 3: | Line 3: | ||
|previous=739 |
|previous=739 |
||
|next=742 |
|next=742 |
||
− | |created= |
+ | |created=2004 |
|complexity=intermediate |
|complexity=intermediate |
||
|author=Lee Chun Kin |
|author=Lee Chun Kin |
||
Line 11: | Line 11: | ||
|category2=Python |
|category2=Python |
||
}} |
}} |
||
+ | The default files provided with Vim do not provide a way to fold Python programs, however entering the command {{tt|1=:setlocal foldmethod=indent}} while editing a Python program enables [[folding]] based on the indent level, and that may be all that is needed. However, this tip provides the extra feature of folding <tt>def</tt> and <tt>class</tt> regions. |
||
− | If the <tt>foldmethod=indent</tt> {{help|prefix=no|'foldmethod'}} for your Python files do not work for you and you would like to fold <tt>def</tt> and <tt>class</tt> regions in your Vim, you can set up this using a custom syntax script. |
||
+ | ==Modifying python.vim== |
||
− | Unfortunately, the syntax file |
+ | Unfortunately, the Python syntax file provided with Vim does not contain folding information. You can however create a custom Python syntax script that folds <tt>def</tt> and <tt>class</tt> regions. |
− | To make this work, copy |
+ | To make this work, copy file <tt>python.vim</tt> provided with Vim, and modify the copy. In Vim, the file that needs to be copied is <tt>$VIMRUNTIME/syntax/python.vim</tt>, and the directory <tt>$VIMRUNTIME</tt> can be determined by entering the following command in Vim: {{tt|:echo $VIMRUNTIME}} |
+ | For example, on a Unix system the source file might be <tt>/usr/share/vim/vim73/syntax/python.vim</tt>, while on a Windows system it might be <tt>C:\Program Files\Vim\Vim73\syntax\python.vim</tt>. |
||
⚫ | |||
+ | |||
+ | The <tt>python.vim</tt> file should be copied to the following directory (you need to create any directories that are not present on your system): |
||
+ | *<tt>~/.vim/after/syntax</tt> on Unix-based systems; or |
||
+ | *<tt>$HOME/vimfiles/after/syntax</tt> on Windows systems (use <tt>:echo $HOME</tt> to determine your <tt>$HOME</tt> directory) |
||
+ | |||
+ | Open the copied file in Vim and change it like this: |
||
+ | |||
⚫ | |||
<pre> |
<pre> |
||
syn keyword pythonStatement nextgroup=pythonFunction skipwhite |
syn keyword pythonStatement nextgroup=pythonFunction skipwhite |
||
Line 23: | Line 32: | ||
and delete it. And also find these lines: |
and delete it. And also find these lines: |
||
<pre> |
<pre> |
||
− | if version < 600 |
+ | if version < 600 |
+ | syntax clear |
||
− | syntax clear |
||
− | elseif exists("b:current_syntax") |
+ | elseif exists("b:current_syntax") |
+ | finish |
||
− | finish |
||
endif |
endif |
||
</pre> |
</pre> |
||
Line 34: | Line 43: | ||
</pre> |
</pre> |
||
− | Additionally, you should create |
+ | Additionally, you should create file <tt>after/ftplugin/python.vim</tt> which sets up folding for Python files. In that file, write: |
<pre> |
<pre> |
||
setlocal foldmethod=syntax |
setlocal foldmethod=syntax |
||
setlocal foldtext=substitute(getline(v:foldstart),'\\t','\ \ \ \ ','g') |
setlocal foldtext=substitute(getline(v:foldstart),'\\t','\ \ \ \ ','g') |
||
</pre> |
</pre> |
||
− | This will enable syntax folding for Python scripts. If you like additional settings for python files, you can put these options in there (e.g. {{help|prefix=no|'tabstop'}}, {{help|prefix=no|'autoindent'}} and other settings). |
||
+ | This will enable syntax folding for Python scripts. Additional settings for Python files can also be in <tt>after/ftplugin/python.vim</tt> (see [[indenting source code]] for an example). |
||
⚫ | |||
+ | |||
⚫ | |||
− | ==Drawback== |
+ | ===Drawback=== |
− | The changed syntax script |
+ | The changed syntax script removes the syntax highlighting for keywords <tt>class</tt> and <tt>def</tt>. |
− | <tt>class</tt> and <tt>def</tt> |
||
==See also== |
==See also== |
||
− | [[Syntax folding of Vim scripts]] contains a complete but complex example on how to syntax fold Vim |
+ | *[[Syntax folding of Vim scripts]] contains a complete but complex example on how to syntax fold Vim scripts |
Folding Plugins for Python: |
Folding Plugins for Python: |
||
− | * |
+ | *{{Script|id=2527|text=jpythonfold}} |
− | * |
+ | *{{Script|id=515|text=python_fold}} |
==Comments== |
==Comments== |
Revision as of 08:38, 4 February 2012
created 2004 · complexity intermediate · author Lee Chun Kin · version 6.0
The default files provided with Vim do not provide a way to fold Python programs, however entering the command :setlocal foldmethod=indent while editing a Python program enables folding based on the indent level, and that may be all that is needed. However, this tip provides the extra feature of folding def and class regions.
Modifying python.vim
Unfortunately, the Python syntax file provided with Vim does not contain folding information. You can however create a custom Python syntax script that folds def and class regions.
To make this work, copy file python.vim provided with Vim, and modify the copy. In Vim, the file that needs to be copied is $VIMRUNTIME/syntax/python.vim, and the directory $VIMRUNTIME can be determined by entering the following command in Vim: :echo $VIMRUNTIME
For example, on a Unix system the source file might be /usr/share/vim/vim73/syntax/python.vim, while on a Windows system it might be C:\Program Files\Vim\Vim73\syntax\python.vim.
The python.vim file should be copied to the following directory (you need to create any directories that are not present on your system):
- ~/.vim/after/syntax on Unix-based systems; or
- $HOME/vimfiles/after/syntax on Windows systems (use :echo $HOME to determine your $HOME directory)
Open the copied file in Vim and change it like this:
Find the line that looks like:
syn keyword pythonStatement nextgroup=pythonFunction skipwhite
and delete it. And also find these lines:
if version < 600 syntax clear elseif exists("b:current_syntax") finish endif
and change them to:
syntax clear
Additionally, you should create file after/ftplugin/python.vim which sets up folding for Python files. In that file, write:
setlocal foldmethod=syntax setlocal foldtext=substitute(getline(v:foldstart),'\\t','\ \ \ \ ','g')
This will enable syntax folding for Python scripts. Additional settings for Python files can also be in after/ftplugin/python.vim (see indenting source code for an example).
If the settings from this file do not work, you need to set up the filetype settings in your vimrc (see :help :filetype-plugin-on).
Drawback
The changed syntax script removes the syntax highlighting for keywords class and def.
See also
- Syntax folding of Vim scripts contains a complete but complex example on how to syntax fold Vim scripts
Folding Plugins for Python: