Vim Tips Wiki
No edit summary
(→‎See also: separate out plugins to own section)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=741
 
|id=741
 
|previous=739
 
|previous=739
 
|next=742
 
|next=742
|created=June 7, 2004
+
|created=2004
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Lee Chun Kin
 
|author=Lee Chun Kin
Line 12: 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 <code>:setlocal foldmethod=indent</code> 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 <code>def</code> and <code>class</code> regions.
{{Dodgy|This tip advocates removing/replacing the existing python syntax highlighting entirely with lines in the .vimrc, which is a bad idea.}}
 
'''Even if this was a good idea, it should be done in the after/syntax directory instead. Try using foldmethod=indent, or adding folding to existing groups rather than trying to replace the whole syntax file in your .vimrc.''' See [[Syntax folding of Vim scripts]] for a complete but complex example. As mentioned, foldmethod=indent will probably suffice for python.
 
   
  +
==Modifying python.vim==
With the folding function of Vim6 (+folding), we can edit the Python programs similar to outline program editors such as SciTE.
 
  +
Unfortunately, the Python syntax file provided with Vim does not contain folding information. You can however create a custom Python syntax script that folds <code>def</code> and <code>class</code> regions.
   
  +
To make this work, copy file <code>python.vim</code> provided with Vim, and modify the copy. In Vim, the file that needs to be copied is <code>$VIMRUNTIME/syntax/python.vim</code>, and the directory <code>$VIMRUNTIME</code> can be determined by entering the following command in Vim: <code>:echo $VIMRUNTIME</code>
Add the following lines to vimrc:
 
   
  +
For example, on a Unix system the source file might be <code>/usr/share/vim/vim73/syntax/python.vim</code>, while on a Windows system it might be <code>C:\Program Files\Vim\Vim73\syntax\python.vim</code>.
  +
  +
The <code>python.vim</code> file should be copied to the following directory (you need to create any directories that are not present on your system):
  +
*<code>~/.vim/after/syntax</code> on Unix-based systems; or
  +
*<code>$HOME/vimfiles/after/syntax</code> on Windows systems (use <code>:echo $HOME</code> to determine your <code>$HOME</code> directory)
  +
  +
Open the copied file in Vim and change it like this:
  +
  +
Find the line that looks like:
 
<pre>
 
<pre>
  +
syn keyword pythonStatement nextgroup=pythonFunction skipwhite
augroup python_prog
 
au!
 
fun! Python_fold()
 
execute 'syntax clear pythonStatement'
 
execute 'syntax keyword pythonStatement break continue del'
 
execute 'syntax keyword pythonStatement except exec finally'
 
execute 'syntax keyword pythonStatement pass print raise'
 
execute 'syntax keyword pythonStatement return try'
 
execute 'syntax keyword pythonStatement global assert'
 
execute 'syntax keyword pythonStatement lambda yield'
 
execute 'syntax match pythonStatement /\<def\>/ nextgroup=pythonFunction skipwhite'
 
execute 'syntax match pythonStatement /\<class\>/ nextgroup=pythonFunction skipwhite'
 
execute 'syntax region pythonFold start="^\z(\s*\)\%(class\|def\)" end="^\%(\n*\z1\s\)\@!" transparent fold'
 
execute 'syntax sync minlines=2000 maxlines=4000'
 
set autoindent
 
set foldmethod=syntax
 
" set foldopen=all foldclose=all
 
set foldtext=substitute(getline(v:foldstart),'\\t','\ \ \ \ ','g')
 
set fillchars=vert:\|,fold:\
 
set tabstop=4 shiftwidth=4 nowrap guioptions+=b
 
endfun
 
autocmd FileType python call Python_fold()
 
augroup END
 
 
</pre>
 
</pre>
  +
and delete it. And also find these lines:
  +
<pre>
  +
if version < 600
  +
syntax clear
  +
elseif exists("b:current_syntax")
  +
finish
  +
endif
  +
</pre>
  +
and change them to:
  +
<pre>
  +
syntax clear
  +
</pre>
  +
  +
Additionally, you should create file <code>after/ftplugin/python.vim</code> which sets up folding for Python files. In that file, write:
  +
<pre>
 
setlocal foldmethod=syntax
 
setlocal foldtext=substitute(getline(v:foldstart),'\\t','\ \ \ \ ','g')
  +
</pre>
  +
  +
This will enable syntax folding for Python scripts. Additional settings for Python files can also be in <code>after/ftplugin/python.vim</code> (see [[indenting source code]] for an example).
  +
  +
If the settings from this file do not work, you need to set up the <code>filetype</code> settings in your [[vimrc]] (see {{help|:filetype-plugin-on}}).
   
  +
===Drawback===
You can use the folding functions (zm, zM, zr, zR, xa, zo, zc, zx...) in editing Python programs.
 
  +
The changed syntax script removes the syntax highlighting for keywords <code>class</code> and <code>def</code>.
   
==Related scripts==
+
==See also==
  +
*[[Syntax folding of Vim scripts]] contains a complete but complex example on how to syntax fold Vim scripts
*jpythonfold: {{Script|id=2527}}
 
  +
==Plugins==
*python_fold: {{Script|id=515}}
 
  +
*{{Script|id=2527|text=jpythonfold}}
  +
*{{Script|id=515|text=python_fold}}
   
 
==Comments==
 
==Comments==
<div style="margin-left:-9999px;">[http://www.vipsexshop.com.br Sex Shop][http://www.requintesexshop.com.br Sex Shop][http://www.sexshop.com.vc Sex Shop][http://www.belasexshop.com.br Sex Shop][http://www.belasexshop.com Sex Shop][http://blog.vipsexshop.com.br Dicas de Sexo][http://www.viplingerie.com.br Lingerie][http://www.vipcalcinhas.com.br Calcinhas][http://www.uniformesbr.com Uniformes Profissionais][http://www.uniformesbr.com Uniformes]</div>
 

Latest revision as of 15:58, 11 February 2015

Tip 741 Printable Monobook Previous Next

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[]

Plugins[]

Comments[]