代码拉取完成,页面将自动刷新
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>if_pyth - Vim Documentation</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim8.0">
<meta name="syntax" content="help">
<meta name="settings" content="no_pre,use_css,expand_tabs">
<link rel="stylesheet" href="style.css" type="text/css" />
<script src="jquery.min.js" type="text/javascript"></script>
<script src="mark-current-page.js" type="text/javascript"></script>
</head>
<body>
<header>
<div class="header">
<a href="http://vim-jp.org/">vim-jp</a>
/ <a href="http://vim-jp.org/vimdoc-en/">vimdoc-en</a>
/ if_pyth<br />
<a name="top"></a><h1>if_pyth - Vim Documentation</h1>
<a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
<span class="CurrentLanguage">English</span>
</span>
</div>
</header>
<nav>
<dl>
<dt>BASIC</dt>
<dd><ul>
<li><a href="quickref.html">quickref</a></li>
<li><a href="sponsor.html">sponsor</a></li>
</ul></dd>
<dt>USER MANUAL</dt>
<dd><ul>
<li><a href="usr_toc.html">usr_toc</a></li>
</ul></dd>
<dt>Getting Started</dt>
<dd><ul>
<li><a href="usr_01.html">usr_01</a></li>
<li><a href="usr_02.html">usr_02</a></li>
<li><a href="usr_03.html">usr_03</a></li>
<li><a href="usr_04.html">usr_04</a></li>
<li><a href="usr_05.html">usr_05</a></li>
<li><a href="usr_06.html">usr_06</a></li>
<li><a href="usr_07.html">usr_07</a></li>
<li><a href="usr_08.html">usr_08</a></li>
<li><a href="usr_09.html">usr_09</a></li>
<li><a href="usr_10.html">usr_10</a></li>
<li><a href="usr_11.html">usr_11</a></li>
<li><a href="usr_12.html">usr_12</a></li>
</ul></dd>
<dt>Editing Effectively</dt>
<dd><ul>
<li><a href="usr_20.html">usr_20</a></li>
<li><a href="usr_21.html">usr_21</a></li>
<li><a href="usr_22.html">usr_22</a></li>
<li><a href="usr_23.html">usr_23</a></li>
<li><a href="usr_24.html">usr_24</a></li>
<li><a href="usr_25.html">usr_25</a></li>
<li><a href="usr_26.html">usr_26</a></li>
<li><a href="usr_27.html">usr_27</a></li>
<li><a href="usr_28.html">usr_28</a></li>
<li><a href="usr_29.html">usr_29</a></li>
<li><a href="usr_30.html">usr_30</a></li>
<li><a href="usr_31.html">usr_31</a></li>
<li><a href="usr_32.html">usr_32</a></li>
</ul></dd>
<dt>Tuning Vim</dt>
<dd><ul>
<li><a href="usr_40.html">usr_40</a></li>
<li><a href="usr_41.html">usr_41</a></li>
<li><a href="usr_42.html">usr_42</a></li>
<li><a href="usr_43.html">usr_43</a></li>
<li><a href="usr_44.html">usr_44</a></li>
<li><a href="usr_45.html">usr_45</a></li>
</ul></dd>
<dt>Making Vim Run</dt>
<dd><ul>
<li><a href="usr_90.html">usr_90</a></li>
</ul></dd>
<dt>General subjects</dt>
<dd><ul>
<li><a href="intro.html">intro</a></li>
<li><a href="index.html">help</a></li>
<li><a href="helphelp.html">helphelp</a></li>
<li><a href="vimindex.html">index</a></li>
<li><a href="tags.html">tags</a></li>
<li><a href="howto.html">howto</a></li>
<li><a href="tips.html">tips</a></li>
<li><a href="message.html">message</a></li>
<li><a href="quotes.html">quotes</a></li>
<li><a href="todo.html">todo</a></li>
<li><a href="debug.html">debug</a></li>
<li><a href="develop.html">develop</a></li>
<li><a href="uganda.html">uganda</a></li>
</ul></dd>
<dt>Basic editing</dt>
<dd><ul>
<li><a href="starting.html">starting</a></li>
<li><a href="editing.html">editing</a></li>
<li><a href="motion.html">motion</a></li>
<li><a href="scroll.html">scroll</a></li>
<li><a href="insert.html">insert</a></li>
<li><a href="change.html">change</a></li>
<li><a href="indent.html">indent</a></li>
<li><a href="undo.html">undo</a></li>
<li><a href="repeat.html">repeat</a></li>
<li><a href="visual.html">visual</a></li>
<li><a href="various.html">various</a></li>
<li><a href="recover.html">recover</a></li>
</ul></dd>
<dt>Advanced editing</dt>
<dd><ul>
<li><a href="cmdline.html">cmdline</a></li>
<li><a href="options.html">options</a></li>
<li><a href="pattern.html">pattern</a></li>
<li><a href="map.html">map</a></li>
<li><a href="tagsrch.html">tagsrch</a></li>
<li><a href="quickfix.html">quickfix</a></li>
<li><a href="windows.html">windows</a></li>
<li><a href="tabpage.html">tabpage</a></li>
<li><a href="syntax.html">syntax</a></li>
<li><a href="spell.html">spell</a></li>
<li><a href="diff.html">diff</a></li>
<li><a href="autocmd.html">autocmd</a></li>
<li><a href="filetype.html">filetype</a></li>
<li><a href="eval.html">eval</a></li>
<li><a href="channel.html">channel</a></li>
<li><a href="fold.html">fold</a></li>
</ul></dd>
<dt>Special issues</dt>
<dd><ul>
<li><a href="print.html">print</a></li>
<li><a href="remote.html">remote</a></li>
<li><a href="term.html">term</a></li>
<li><a href="digraph.html">digraph</a></li>
<li><a href="mbyte.html">mbyte</a></li>
<li><a href="mlang.html">mlang</a></li>
<li><a href="arabic.html">arabic</a></li>
<li><a href="farsi.html">farsi</a></li>
<li><a href="hebrew.html">hebrew</a></li>
<li><a href="russian.html">russian</a></li>
<li><a href="ft_ada.html">ft_ada</a></li>
<li><a href="ft_sql.html">ft_sql</a></li>
<li><a href="hangulin.html">hangulin</a></li>
<li><a href="rileft.html">rileft</a></li>
</ul></dd>
<dt>GUI</dt>
<dd><ul>
<li><a href="gui.html">gui</a></li>
<li><a href="gui_w32.html">gui_w32</a></li>
<li><a href="gui_x11.html">gui_x11</a></li>
</ul></dd>
<dt>Interfaces</dt>
<dd><ul>
<li><a href="if_cscop.html">if_cscop</a></li>
<li><a href="if_lua.html">if_lua</a></li>
<li><a href="if_mzsch.html">if_mzsch</a></li>
<li><a href="if_perl.html">if_perl</a></li>
<li><a href="if_pyth.html">if_pyth</a></li>
<li><a href="if_tcl.html">if_tcl</a></li>
<li><a href="if_ole.html">if_ole</a></li>
<li><a href="if_ruby.html">if_ruby</a></li>
<li><a href="debugger.html">debugger</a></li>
<li><a href="workshop.html">workshop</a></li>
<li><a href="netbeans.html">netbeans</a></li>
<li><a href="sign.html">sign</a></li>
</ul></dd>
<dt>Versions</dt>
<dd><ul>
<li><a href="vi_diff.html">vi_diff</a></li>
<li><a href="version4.html">version4</a></li>
<li><a href="version5.html">version5</a></li>
<li><a href="version6.html">version6</a></li>
<li><a href="version7.html">version7</a></li>
<li><a href="version8.html">version8</a></li>
</ul></dd>
<dt>Remarks about specific systems</dt>
<dd><ul>
<li><a href="os_390.html">os_390</a></li>
<li><a href="os_amiga.html">os_amiga</a></li>
<li><a href="os_beos.html">os_beos</a></li>
<li><a href="os_dos.html">os_dos</a></li>
<li><a href="os_mac.html">os_mac</a></li>
<li><a href="os_mint.html">os_mint</a></li>
<li><a href="os_msdos.html">os_msdos</a></li>
<li><a href="os_os2.html">os_os2</a></li>
<li><a href="os_qnx.html">os_qnx</a></li>
<li><a href="os_risc.html">os_risc</a></li>
<li><a href="os_unix.html">os_unix</a></li>
<li><a href="os_vms.html">os_vms</a></li>
<li><a href="os_win32.html">os_win32</a></li>
</ul></dd>
<dt>Standard plugins</dt>
<dd><ul>
<li><a href="pi_getscript.html">pi_getscript</a></li>
<li><a href="pi_gzip.html">pi_gzip</a></li>
<li><a href="pi_logipat.html">pi_logipat</a></li>
<li><a href="pi_netrw.html">pi_netrw</a></li>
<li><a href="pi_paren.html">pi_paren</a></li>
<li><a href="pi_tar.html">pi_tar</a></li>
<li><a href="pi_vimball.html">pi_vimball</a></li>
<li><a href="pi_zip.html">pi_zip</a></li>
</ul></dd>
<dt>Filetype plugins</dt>
<dd><ul>
<li><a href="pi_spec.html">pi_spec</a></li>
</ul></dd>
<dt>Others</dt>
<dd><ul>
<li><a href="vim_faq.html">vim_faq</a></li>
</ul></dd>
</dl>
</nav>
<article class="Vimdoc VimdocJa">
<div id='vimCodeElement'>
<a class="Constant" href="if_pyth.html" name="if_pyth.txt">if_pyth.txt</a> For <span class="Identifier">Vim version 8.0.</span> Last change: 2017 Mar 09<br>
<br>
<br>
<span class="Identifier">VIM REFERENCE MANUAL by Paul Moore</span><br>
<br>
<br>
The Python Interface to Vim <a class="Constant" href="if_pyth.html#python" name="python">python</a> <a class="Constant" href="if_pyth.html#Python" name="Python">Python</a><br>
<br>
1. Commands <a class="Identifier" href="if_pyth.html#python-commands">python-commands</a><br>
2. The vim module <a class="Identifier" href="if_pyth.html#python-vim">python-vim</a><br>
3. Buffer objects <a class="Identifier" href="if_pyth.html#python-buffer">python-buffer</a><br>
4. Range objects <a class="Identifier" href="if_pyth.html#python-range">python-range</a><br>
5. Window objects <a class="Identifier" href="if_pyth.html#python-window">python-window</a><br>
6. Tab page objects <a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a><br>
7. vim.bindeval objects <a class="Identifier" href="if_pyth.html#python-bindeval-objects">python-bindeval-objects</a><br>
8. pyeval(), py3eval() Vim functions <a class="Identifier" href="if_pyth.html#python-pyeval">python-pyeval</a><br>
9. Dynamic loading <a class="Identifier" href="if_pyth.html#python-dynamic">python-dynamic</a><br>
10. Python 3 <a class="Identifier" href="if_pyth.html#python3">python3</a><br>
11. Python X <a class="Identifier" href="if_pyth.html#python_x">python_x</a><br>
12. Building with Python support <a class="Identifier" href="if_pyth.html#python-building">python-building</a><br>
<br>
<span class="Special">{Vi does not have any of these commands}</span><br>
<br>
The Python 2.x interface is available only when Vim was compiled with the<br>
<a class="Identifier" href="various.html#+python">+python</a> feature.<br>
The Python 3 interface is available only when Vim was compiled with the<br>
<a class="Identifier" href="various.html#+python3">+python3</a> feature.<br>
Both can be available at the same time, but read <a class="Identifier" href="if_pyth.html#python-2-and-3">python-2-and-3</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
1. Commands <a class="Constant" href="if_pyth.html#python-commands" name="python-commands">python-commands</a><br>
<br>
<a class="Constant" href="if_pyth.html#:python" name=":python">:python</a> <a class="Constant" href="if_pyth.html#:py" name=":py">:py</a> <a class="Constant" href="if_pyth.html#E263" name="E263">E263</a> <a class="Constant" href="if_pyth.html#E264" name="E264">E264</a> <a class="Constant" href="if_pyth.html#E887" name="E887">E887</a><br>
:<span class="Special">[range]</span>py[thon] <span class="Special">{stmt}</span><br>
Execute Python statement <span class="Special">{stmt}</span>. A simple check if<br>
the <a class="Comment" href="if_pyth.html#:python">:python</a> command is working:<br>
<div class="helpExample"> :python print "Hello"</div>
<br>
:<span class="Special">[range]</span>py[thon] << <span class="Special">{endmarker}</span><br>
<span class="Special">{script}</span><br>
<span class="Special">{endmarker}</span><br>
Execute Python script <span class="Special">{script}</span>.<br>
<span class="Todo">Note</span>: This command doesn't work when the Python<br>
feature wasn't compiled in. To avoid errors, see<br>
<a class="Identifier" href="if_perl.html#script-here">script-here</a>.<br>
<br>
<span class="Special">{endmarker}</span> must NOT be preceded by any white space. If <span class="Special">{endmarker}</span> is<br>
omitted from after the "<<", a dot '.' must be used after <span class="Special">{script}</span>, like<br>
for the <a class="Identifier" href="insert.html#:append">:append</a> and <a class="Identifier" href="insert.html#:insert">:insert</a> commands.<br>
This form of the <a class="Identifier" href="if_pyth.html#:python">:python</a> command is mainly useful for including python code<br>
in Vim scripts.<br>
<br>
Example:<br>
<div class="helpExample"> function! IcecreamInitialize()<br>
python << EOF<br>
class StrawberryIcecream:<br>
def __call__(self):<br>
print 'EAT ME'<br>
EOF<br>
endfunction</div>
<br>
To see what version of Python you have:<br>
<div class="helpExample"> :python import sys<br>
:python print(sys.version)</div>
<br>
<span class="Todo">Note</span>: Python is very sensitive to the indenting. Make sure the "class" line<br>
and "EOF" do not have any indent.<br>
<br>
<a class="Constant" href="if_pyth.html#:pydo" name=":pydo">:pydo</a><br>
:<span class="Special">[range]</span>pydo <span class="Special">{body}</span> Execute Python function "def _vim_pydo(line, linenr):<br>
<span class="Special">{body}</span>" for each line in the <span class="Special">[range]</span>, with the<br>
function arguments being set to the text of each line<br>
in turn, without a trailing <span class="Special"><EOL></span>, and the current<br>
line number. The function should return a string or<br>
None. If a string is returned, it becomes the text of<br>
the line in the current turn. The default for <span class="Special">[range]</span><br>
is the whole file: "1,$".<br>
<span class="Special">{not in Vi}</span><br>
<br>
Examples:<br>
<br>
<div class="helpExample"> :pydo return "%s\t%d" % (line[::-1], len(line))<br>
:pydo if line: return "%4d: %s" % (linenr, line)</div>
<br>
<a class="Constant" href="if_pyth.html#:pyfile" name=":pyfile">:pyfile</a> <a class="Constant" href="if_pyth.html#:pyf" name=":pyf">:pyf</a><br>
:<span class="Special">[range]</span>pyf[ile] <span class="Special">{file}</span><br>
Execute the Python script in <span class="Special">{file}</span>. The whole<br>
argument is used as a single file name. <span class="Special">{not in Vi}</span><br>
<br>
Both of these commands do essentially the same thing - they execute a piece of<br>
Python code, with the "current range" <a class="Identifier" href="if_pyth.html#python-range">python-range</a> set to the given line<br>
range.<br>
<br>
In the case of :python, the code to execute is in the command-line.<br>
In the case of :pyfile, the code to execute is the contents of the given file.<br>
<br>
Python commands cannot be used in the <a class="Identifier" href="eval.html#sandbox">sandbox</a>.<br>
<br>
To pass arguments you need to set sys.argv[] explicitly. Example:<br>
<br>
<div class="helpExample"> :python import sys<br>
:python sys.argv = ["foo", "bar"]<br>
:pyfile myscript.py</div>
<br>
Here are some examples <a class="Constant" href="if_pyth.html#python-examples" name="python-examples">python-examples</a> <br>
<br>
<div class="helpExample"> :python from vim import *<br>
:python from string import upper<br>
:python current.line = upper(current.line)<br>
:python print "Hello"<br>
:python str = current.buffer[42]</div>
<br>
(<span class="Todo">Note</span> that changes - like the imports - persist from one command to the next,<br>
just like in the Python interpreter.)<br>
<br>
<span class="PreProc">==============================================================================</span><br>
2. The vim module <a class="Constant" href="if_pyth.html#python-vim" name="python-vim">python-vim</a><br>
<br>
Python code gets all of its access to vim (with one exception - see<br>
<a class="Identifier" href="if_pyth.html#python-output">python-output</a> below) via the "vim" module. The vim module implements two<br>
methods, three constants, and one error object. You need to import the vim<br>
module before using it:<br>
<div class="helpExample"> :python import vim</div>
<br>
Overview<br>
<div class="helpExample"> :py print "Hello" # displays a message<br>
:py vim.command(cmd) # execute an Ex command<br>
:py w = vim.windows[n] # gets window "n"<br>
:py cw = vim.current.window # gets the current window<br>
:py b = vim.buffers[n] # gets buffer "n"<br>
:py cb = vim.current.buffer # gets the current buffer<br>
:py w.height = lines # sets the window height<br>
:py w.cursor = (row, col) # sets the window cursor position<br>
:py pos = w.cursor # gets a tuple (row, col)<br>
:py name = b.name # gets the buffer file name<br>
:py line = b[n] # gets a line from the buffer<br>
:py lines = b[n:m] # gets a list of lines<br>
:py num = len(b) # gets the number of lines<br>
:py b[n] = str # sets a line in the buffer<br>
:py b[n:m] = [str1, str2, str3] # sets a number of lines at once<br>
:py del b[n] # deletes a line<br>
:py del b[n:m] # deletes a number of lines</div>
<br>
<br>
Methods of the "vim" module<br>
<br>
vim.command(str) <a class="Constant" href="if_pyth.html#python-command" name="python-command">python-command</a><br>
Executes the vim (ex-mode) command str. Returns None.<br>
Examples:<br>
<div class="helpExample"> :py vim.command("set tw=72")<br>
:py vim.command("%s/aaa/bbb/g")</div>
The following definition executes Normal mode commands:<br>
<div class="helpExample"> def normal(str):<br>
vim.command("normal "+str)<br>
# Note the use of single quotes to delimit a string containing<br>
# double quotes<br>
normal('"a2dd"aP')</div>
<a class="Constant" href="if_pyth.html#E659" name="E659">E659</a><br>
The ":python" command cannot be used recursively with Python 2.2 and<br>
older. This only works with Python 2.3 and later:<br>
<div class="helpExample"> :py vim.command("python print 'Hello again Python'")</div>
<br>
vim.eval(str) <a class="Constant" href="if_pyth.html#python-eval" name="python-eval">python-eval</a><br>
Evaluates the expression str using the vim internal expression<br>
evaluator (see <a class="Identifier" href="eval.html#expression">expression</a>). Returns the expression result as:<br>
- a string if the Vim expression evaluates to a string or number<br>
- a list if the Vim expression evaluates to a Vim list<br>
- a dictionary if the Vim expression evaluates to a Vim dictionary<br>
Dictionaries and lists are recursively expanded.<br>
Examples:<br>
<div class="helpExample"> :py text_width = vim.eval("&tw")<br>
:py str = vim.eval("12+12") # NB result is a string! Use<br>
# string.atoi() to convert to<br>
# a number.</div>
<br>
<div class="helpExample"> :py tagList = vim.eval('taglist("eval_expr")')</div>
The latter will return a python list of python dicts, for instance:<br>
<span class="PreProc">[{'cmd': '/^eval_expr(arg, nextcmd)$/', 'static': 0, 'name':</span><br>
<span class="PreProc">'eval_expr', 'kind': 'f', 'filename': './src/eval.c'}]</span><br>
<br>
vim.bindeval(str) <a class="Constant" href="if_pyth.html#python-bindeval" name="python-bindeval">python-bindeval</a><br>
Like <a class="Identifier" href="if_pyth.html#python-eval">python-eval</a>, but returns special objects described in <br>
<a class="Identifier" href="if_pyth.html#python-bindeval-objects">python-bindeval-objects</a>. These python objects let you modify (<a class="Identifier" href="eval.html#List">List</a> <br>
or <a class="Identifier" href="eval.html#Dictionary">Dictionary</a>) or call (<a class="Identifier" href="eval.html#Funcref">Funcref</a>) vim objects.<br>
<br>
vim.strwidth(str) <a class="Constant" href="if_pyth.html#python-strwidth" name="python-strwidth">python-strwidth</a><br>
Like <a class="Identifier" href="eval.html#strwidth()">strwidth()</a>: returns number of display cells str occupies, tab <br>
is counted as one cell.<br>
<br>
vim.foreach_rtp(callable) <a class="Constant" href="if_pyth.html#python-foreach_rtp" name="python-foreach_rtp">python-foreach_rtp</a><br>
Call the given callable for each path in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a> until either <br>
callable returns something but None, the exception is raised or there <br>
are no longer paths. If stopped in case callable returned non-None, <br>
vim.foreach_rtp function returns the value returned by callable.<br>
<br>
vim.chdir(*args, **kwargs) <a class="Constant" href="if_pyth.html#python-chdir" name="python-chdir">python-chdir</a><br>
vim.fchdir(*args, **kwargs) <a class="Constant" href="if_pyth.html#python-fchdir" name="python-fchdir">python-fchdir</a><br>
Run os.chdir or os.fchdir, then all appropriate vim stuff.<br>
<span class="Todo">Note</span>: you should not use these functions directly, use os.chdir and <br>
os.fchdir instead. Behavior of vim.fchdir is undefined in case <br>
os.fchdir does not exist.<br>
<br>
Error object of the "vim" module<br>
<br>
vim.error <a class="Constant" href="if_pyth.html#python-error" name="python-error">python-error</a><br>
Upon encountering a Vim error, Python raises an exception of type<br>
vim.error.<br>
Example:<br>
<div class="helpExample"> try:<br>
vim.command("put a")<br>
except vim.error:<br>
# nothing in register a</div>
<br>
Constants of the "vim" module<br>
<br>
<span class="Todo">Note</span> that these are not actually constants - you could reassign them.<br>
But this is silly, as you would then lose access to the vim objects<br>
to which the variables referred.<br>
<br>
vim.buffers <a class="Constant" href="if_pyth.html#python-buffers" name="python-buffers">python-buffers</a><br>
A mapping object providing access to the list of vim buffers. The<br>
object supports the following operations:<br>
<div class="helpExample"> :py b = vim.buffers[i] # Indexing (read-only)<br>
:py b in vim.buffers # Membership test<br>
:py n = len(vim.buffers) # Number of elements<br>
:py for b in vim.buffers: # Iterating over buffer list</div>
<br>
vim.windows <a class="Constant" href="if_pyth.html#python-windows" name="python-windows">python-windows</a><br>
A sequence object providing access to the list of vim windows. The<br>
object supports the following operations:<br>
<div class="helpExample"> :py w = vim.windows[i] # Indexing (read-only)<br>
:py w in vim.windows # Membership test<br>
:py n = len(vim.windows) # Number of elements<br>
:py for w in vim.windows: # Sequential access</div>
<span class="Todo">Note</span>: vim.windows object always accesses current tab page. <br>
<a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a>.windows objects are bound to parent <a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a> <br>
object and always use windows from that tab page (or throw vim.error <br>
in case tab page was deleted). You can keep a reference to both <br>
without keeping a reference to vim module object or <a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a>, <br>
they will not lose their properties in this case.<br>
<br>
vim.tabpages <a class="Constant" href="if_pyth.html#python-tabpages" name="python-tabpages">python-tabpages</a><br>
A sequence object providing access to the list of vim tab pages. The <br>
object supports the following operations:<br>
<div class="helpExample"> :py t = vim.tabpages[i] # Indexing (read-only)<br>
:py t in vim.tabpages # Membership test<br>
:py n = len(vim.tabpages) # Number of elements<br>
:py for t in vim.tabpages: # Sequential access</div>
<br>
vim.current <a class="Constant" href="if_pyth.html#python-current" name="python-current">python-current</a><br>
An object providing access (via specific attributes) to various<br>
"current" objects available in vim:<br>
vim.current.line The current line (RW) String<br>
vim.current.buffer The current buffer (RW) Buffer<br>
vim.current.window The current window (RW) Window<br>
vim.current.tabpage The current tab page (RW) TabPage<br>
vim.current.range The current line range (RO) Range<br>
<br>
The last case deserves a little explanation. When the :python or<br>
:pyfile command specifies a range, this range of lines becomes the<br>
"current range". A range is a bit like a buffer, but with all access<br>
restricted to a subset of lines. See <a class="Identifier" href="if_pyth.html#python-range">python-range</a> for more details.<br>
<br>
<span class="Todo">Note</span>: When assigning to vim.current.<span class="Special">{buffer,window,tabpage}</span> it expects <br>
valid <a class="Identifier" href="if_pyth.html#python-buffer">python-buffer</a>, <a class="Identifier" href="if_pyth.html#python-window">python-window</a> or <a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a> objects <br>
respectively. Assigning triggers normal (with <a class="Identifier" href="autocmd.html#autocommand">autocommand</a>s) <br>
switching to given buffer, window or tab page. It is the only way to <br>
switch UI objects in python: you can't assign to <br>
<a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a>.window attribute. To switch without triggering <br>
autocommands use<br>
<div class="helpExample"> py << EOF<br>
saved_eventignore = vim.options['eventignore']<br>
vim.options['eventignore'] = 'all'<br>
try:<br>
vim.current.buffer = vim.buffers[2] # Switch to buffer 2<br>
finally:<br>
vim.options['eventignore'] = saved_eventignore<br>
EOF</div>
<br>
vim.vars <a class="Constant" href="if_pyth.html#python-vars" name="python-vars">python-vars</a><br>
vim.vvars <a class="Constant" href="if_pyth.html#python-vvars" name="python-vvars">python-vvars</a><br>
Dictionary-like objects holding dictionaries with global (<a class="Identifier" href="eval.html#g:">g:</a>) and <br>
vim (<a class="Identifier" href="eval.html#v:">v:</a>) variables respectively. Identical to <span class="Comment">vim.bindeval("g:")</span>, <br>
but faster.<br>
<br>
vim.options <a class="Constant" href="if_pyth.html#python-options" name="python-options">python-options</a><br>
Object partly supporting mapping protocol (supports setting and <br>
getting items) providing a read-write access to global options.<br>
<span class="Todo">Note</span>: unlike <a class="Identifier" href="options.html#:set">:set</a> this provides access only to global options. You<br>
cannot use this object to obtain or set local options' values or<br>
access local-only options in any fashion. Raises KeyError if no global<br>
option with such name exists (i.e. does not raise KeyError for<br>
<a class="Identifier" href="options.html#global-local">global-local</a> options and global only options, but does for window-<br>
and buffer-local ones). Use <a class="Identifier" href="if_pyth.html#python-buffer">python-buffer</a> objects to access to<br>
buffer-local options and <a class="Identifier" href="if_pyth.html#python-window">python-window</a> objects to access to<br>
window-local options.<br>
<br>
Type of this object is available via "Options" attribute of vim <br>
module.<br>
<br>
Output from Python <a class="Constant" href="if_pyth.html#python-output" name="python-output">python-output</a><br>
Vim displays all Python code output in the Vim message area. Normal<br>
output appears as information messages, and error output appears as<br>
error messages.<br>
<br>
In implementation terms, this means that all output to sys.stdout<br>
(including the output from print statements) appears as information<br>
messages, and all output to sys.stderr (including error tracebacks)<br>
appears as error messages.<br>
<br>
<a class="Constant" href="if_pyth.html#python-input" name="python-input">python-input</a><br>
Input (via sys.stdin, including input() and raw_input()) is not<br>
supported, and may cause the program to crash. This should probably be<br>
fixed.<br>
<br>
<a class="Constant" href="if_pyth.html#python2-directory" name="python2-directory">python2-directory</a> <a class="Constant" href="if_pyth.html#python3-directory" name="python3-directory">python3-directory</a> <a class="Constant" href="if_pyth.html#pythonx-directory" name="pythonx-directory">pythonx-directory</a><br>
Python <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a> handling <a class="Constant" href="if_pyth.html#python-special-path" name="python-special-path">python-special-path</a><br>
<br>
In python vim.VIM_SPECIAL_PATH special directory is used as a replacement for <br>
the list of paths found in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a>: with this directory in sys.path and <br>
vim.path_hooks in sys.path_hooks python will try to load module from <br>
<span class="Special">{rtp}</span>/python2 (or python3) and <span class="Special">{rtp}</span>/pythonx (for both python versions) for <br>
each <span class="Special">{rtp}</span> found in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a>.<br>
<br>
Implementation is similar to the following, but written in C:<br>
<br>
<div class="helpExample"> from imp import find_module, load_module<br>
import vim<br>
import sys</div>
<br>
<div class="helpExample"> class VimModuleLoader(object):<br>
def __init__(self, module):<br>
self.module = module</div>
<br>
<div class="helpExample"> def load_module(self, fullname, path=None):<br>
return self.module</div>
<br>
<div class="helpExample"> def _find_module(fullname, oldtail, path):<br>
idx = oldtail.find('.')<br>
if idx > 0:<br>
name = oldtail[:idx]<br>
tail = oldtail[idx+1:]<br>
fmr = find_module(name, path)<br>
module = load_module(fullname[:-len(oldtail)] + name, *fmr)<br>
return _find_module(fullname, tail, module.__path__)<br>
else:<br>
fmr = find_module(fullname, path)<br>
return load_module(fullname, *fmr)</div>
<br>
<div class="helpExample"> # It uses vim module itself in place of VimPathFinder class: it does not <br>
# matter for python which object has find_module function attached to as <br>
# an attribute.<br>
class VimPathFinder(object):<br>
@classmethod<br>
def find_module(cls, fullname, path=None):<br>
try:<br>
return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths()))<br>
except ImportError:<br>
return None</div>
<br>
<div class="helpExample"> @classmethod<br>
def load_module(cls, fullname, path=None):<br>
return _find_module(fullname, fullname, path or vim._get_paths())</div>
<br>
<div class="helpExample"> def hook(path):<br>
if path == vim.VIM_SPECIAL_PATH:<br>
return VimPathFinder<br>
else:<br>
raise ImportError</div>
<br>
<div class="helpExample"> sys.path_hooks.append(hook)</div>
<br>
vim.VIM_SPECIAL_PATH <a class="Constant" href="if_pyth.html#python-VIM_SPECIAL_PATH" name="python-VIM_SPECIAL_PATH">python-VIM_SPECIAL_PATH</a><br>
String constant used in conjunction with vim path hook. If path hook <br>
installed by vim is requested to handle anything but path equal to <br>
vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other <br>
case it uses special loader.<br>
<br>
<span class="Todo">Note</span>: you must not use value of this constant directly, always use <br>
vim.VIM_SPECIAL_PATH object.<br>
<br>
vim.find_module(...) <a class="Constant" href="if_pyth.html#python-find_module" name="python-find_module">python-find_module</a><br>
vim.path_hook(path) <a class="Constant" href="if_pyth.html#python-path_hook" name="python-path_hook">python-path_hook</a><br>
Methods or objects used to implement path loading as described above. <br>
You should not be using any of these directly except for vim.path_hook <br>
in case you need to do something with sys.meta_path. It is not <br>
guaranteed that any of the objects will exist in the future vim <br>
versions.<br>
<br>
vim._get_paths <a class="Constant" href="if_pyth.html#python-_get_paths" name="python-_get_paths">python-_get_paths</a><br>
Methods returning a list of paths which will be searched for by path <br>
hook. You should not rely on this method being present in future <br>
versions, but can use it for debugging.<br>
<br>
It returns a list of <span class="Special">{rtp}</span>/python2 (or <span class="Special">{rtp}</span>/python3) and <br>
<span class="Special">{rtp}</span>/pythonx directories for each <span class="Special">{rtp}</span> in <a class="Type" href="options.html#'runtimepath'">'runtimepath'</a>.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
3. Buffer objects <a class="Constant" href="if_pyth.html#python-buffer" name="python-buffer">python-buffer</a><br>
<br>
Buffer objects represent vim buffers. You can obtain them in a number of ways:<br>
- via vim.current.buffer (<a class="Identifier" href="if_pyth.html#python-current">python-current</a>)<br>
- from indexing vim.buffers (<a class="Identifier" href="if_pyth.html#python-buffers">python-buffers</a>)<br>
- from the "buffer" attribute of a window (<a class="Identifier" href="if_pyth.html#python-window">python-window</a>)<br>
<br>
Buffer objects have two read-only attributes - name - the full file name for<br>
the buffer, and number - the buffer number. They also have three methods<br>
(append, mark, and range; see below).<br>
<br>
You can also treat buffer objects as sequence objects. In this context, they<br>
act as if they were lists (yes, they are mutable) of strings, with each<br>
element being a line of the buffer. All of the usual sequence operations,<br>
including indexing, index assignment, slicing and slice assignment, work as<br>
you would expect. <span class="Todo">Note</span> that the result of indexing (slicing) a buffer is a<br>
string (list of strings). This has one unusual consequence - b[:] is different<br>
from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas<br>
"b = None" merely updates the variable b, with no effect on the buffer.<br>
<br>
Buffer indexes start at zero, as is normal in Python. This differs from vim<br>
line numbers, which start from 1. This is particularly relevant when dealing<br>
with marks (see below) which use vim line numbers.<br>
<br>
The buffer object attributes are:<br>
b.vars Dictionary-like object used to access <br>
<a class="Identifier" href="eval.html#buffer-variable">buffer-variable</a>s.<br>
b.options Mapping object (supports item getting, setting and <br>
deleting) that provides access to buffer-local options <br>
and buffer-local values of <a class="Identifier" href="options.html#global-local">global-local</a> options. Use <br>
<a class="Identifier" href="if_pyth.html#python-window">python-window</a>.options if option is window-local, <br>
this object will raise KeyError. If option is <br>
<a class="Identifier" href="options.html#global-local">global-local</a> and local value is missing getting it <br>
will return None.<br>
b.name String, RW. Contains buffer name (full path).<br>
<span class="Todo">Note</span>: when assigning to b.name <a class="Identifier" href="autocmd.html#BufFilePre">BufFilePre</a> and <br>
<a class="Identifier" href="autocmd.html#BufFilePost">BufFilePost</a> autocommands are launched.<br>
b.number Buffer number. Can be used as <a class="Identifier" href="if_pyth.html#python-buffers">python-buffers</a> key.<br>
Read-only.<br>
b.valid True or False. Buffer object becomes invalid when <br>
corresponding buffer is wiped out.<br>
<br>
The buffer object methods are:<br>
b.append(str) Append a line to the buffer<br>
b.append(str, nr) Idem, below line "nr"<br>
b.append(list) Append a list of lines to the buffer<br>
<span class="Todo">Note</span> that the option of supplying a list of strings to<br>
the append method differs from the equivalent method<br>
for Python's built-in list objects.<br>
b.append(list, nr) Idem, below line "nr"<br>
b.mark(name) Return a tuple (row,col) representing the position<br>
of the named mark (can also get the []"<> marks)<br>
b.range(s,e) Return a range object (see <a class="Identifier" href="if_pyth.html#python-range">python-range</a>) which<br>
represents the part of the given buffer between line<br>
numbers s and e <a class="Identifier" href="motion.html#inclusive">inclusive</a>.<br>
<br>
<span class="Todo">Note</span> that when adding a line it must not contain a line break character '\n'.<br>
A trailing '\n' is allowed and ignored, so that you can do:<br>
<div class="helpExample"> :py b.append(f.readlines())</div>
<br>
Buffer object type is available using "Buffer" attribute of vim module.<br>
<br>
Examples (assume b is the current buffer)<br>
<div class="helpExample"> :py print b.name # write the buffer file name<br>
:py b[0] = "hello!!!" # replace the top line<br>
:py b[:] = None # delete the whole buffer<br>
:py del b[:] # delete the whole buffer<br>
:py b[0:0] = [ "a line" ] # add a line at the top<br>
:py del b[2] # delete a line (the third)<br>
:py b.append("bottom") # add a line at the bottom<br>
:py n = len(b) # number of lines<br>
:py (row,col) = b.mark('a') # named mark<br>
:py r = b.range(1,5) # a sub-range of the buffer<br>
:py b.vars["foo"] = "bar" # assign b:foo variable<br>
:py b.options["ff"] = "dos" # set fileformat<br>
:py del b.options["ar"] # same as :set autoread<</div>
<br>
<span class="PreProc">==============================================================================</span><br>
4. Range objects <a class="Constant" href="if_pyth.html#python-range" name="python-range">python-range</a><br>
<br>
Range objects represent a part of a vim buffer. You can obtain them in a<br>
number of ways:<br>
- via vim.current.range (<a class="Identifier" href="if_pyth.html#python-current">python-current</a>)<br>
- from a buffer's range() method (<a class="Identifier" href="if_pyth.html#python-buffer">python-buffer</a>)<br>
<br>
A range object is almost identical in operation to a buffer object. However,<br>
all operations are restricted to the lines within the range (this line range<br>
can, of course, change as a result of slice assignments, line deletions, or<br>
the range.append() method).<br>
<br>
The range object attributes are:<br>
r.start Index of first line into the buffer<br>
r.end Index of last line into the buffer<br>
<br>
The range object methods are:<br>
r.append(str) Append a line to the range<br>
r.append(str, nr) Idem, after line "nr"<br>
r.append(list) Append a list of lines to the range<br>
<span class="Todo">Note</span> that the option of supplying a list of strings to<br>
the append method differs from the equivalent method<br>
for Python's built-in list objects.<br>
r.append(list, nr) Idem, after line "nr"<br>
<br>
Range object type is available using "Range" attribute of vim module.<br>
<br>
Example (assume r is the current range):<br>
# Send all lines in a range to the default printer<br>
vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1))<br>
<br>
<span class="PreProc">==============================================================================</span><br>
5. Window objects <a class="Constant" href="if_pyth.html#python-window" name="python-window">python-window</a><br>
<br>
Window objects represent vim windows. You can obtain them in a number of ways:<br>
- via vim.current.window (<a class="Identifier" href="if_pyth.html#python-current">python-current</a>)<br>
- from indexing vim.windows (<a class="Identifier" href="if_pyth.html#python-windows">python-windows</a>)<br>
- from indexing "windows" attribute of a tab page (<a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a>)<br>
- from the "window" attribute of a tab page (<a class="Identifier" href="if_pyth.html#python-tabpage">python-tabpage</a>)<br>
<br>
You can manipulate window objects only through their attributes. They have no<br>
methods, and no sequence or other interface.<br>
<br>
Window attributes are:<br>
buffer (read-only) The buffer displayed in this window<br>
cursor (read-write) The current cursor position in the window<br>
This is a tuple, (row,col).<br>
height (read-write) The window height, in rows<br>
width (read-write) The window width, in columns<br>
vars (read-only) The window <a class="Identifier" href="eval.html#w:">w:</a> variables. Attribute is <br>
unassignable, but you can change window <br>
variables this way<br>
options (read-only) The window-local options. Attribute is <br>
unassignable, but you can change window <br>
options this way. Provides access only to <br>
window-local options, for buffer-local use <br>
<a class="Identifier" href="if_pyth.html#python-buffer">python-buffer</a> and for global ones use <br>
<a class="Identifier" href="if_pyth.html#python-options">python-options</a>. If option is <a class="Identifier" href="options.html#global-local">global-local</a> <br>
and local value is missing getting it will <br>
return None.<br>
number (read-only) Window number. The first window has number 1.<br>
This is zero in case it cannot be determined<br>
(e.g. when the window object belongs to other<br>
tab page).<br>
row, col (read-only) On-screen window position in display cells.<br>
First position is zero.<br>
tabpage (read-only) Window tab page.<br>
valid (read-write) True or False. Window object becomes invalid <br>
when corresponding window is closed.<br>
<br>
The height attribute is writable only if the screen is split horizontally.<br>
The width attribute is writable only if the screen is split vertically.<br>
<br>
Window object type is available using "Window" attribute of vim module.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
6. Tab page objects <a class="Constant" href="if_pyth.html#python-tabpage" name="python-tabpage">python-tabpage</a><br>
<br>
Tab page objects represent vim tab pages. You can obtain them in a number of <br>
ways:<br>
- via vim.current.tabpage (<a class="Identifier" href="if_pyth.html#python-current">python-current</a>)<br>
- from indexing vim.tabpages (<a class="Identifier" href="if_pyth.html#python-tabpages">python-tabpages</a>)<br>
<br>
You can use this object to access tab page windows. They have no methods and <br>
no sequence or other interfaces.<br>
<br>
Tab page attributes are:<br>
number The tab page number like the one returned by <br>
<a class="Identifier" href="eval.html#tabpagenr()">tabpagenr()</a>.<br>
windows Like <a class="Identifier" href="if_pyth.html#python-windows">python-windows</a>, but for current tab page.<br>
vars The tab page <a class="Identifier" href="eval.html#t:">t:</a> variables.<br>
window Current tabpage window.<br>
valid True or False. Tab page object becomes invalid when <br>
corresponding tab page is closed.<br>
<br>
TabPage object type is available using "TabPage" attribute of vim module.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
7. vim.bindeval objects <a class="Constant" href="if_pyth.html#python-bindeval-objects" name="python-bindeval-objects">python-bindeval-objects</a><br>
<br>
vim.Dictionary object <a class="Constant" href="if_pyth.html#python-Dictionary" name="python-Dictionary">python-Dictionary</a><br>
Dictionary-like object providing access to vim <a class="Identifier" href="eval.html#Dictionary">Dictionary</a> type.<br>
Attributes:<br>
<span class="PreProc">Attribute Description</span><br>
locked One of <a class="Constant" href="if_pyth.html#python-.locked" name="python-.locked">python-.locked</a><br>
<span class="PreProc">Value Description</span><br>
zero Variable is not locked<br>
vim.VAR_LOCKED Variable is locked, but can be unlocked<br>
vim.VAR_FIXED Variable is locked and can't be unlocked<br>
Read-write. You can unlock locked variable by assigning <br>
<span class="Comment">True</span> or <span class="Comment">False</span> to this attribute. No recursive locking <br>
is supported.<br>
scope One of<br>
<span class="PreProc">Value Description</span><br>
zero Dictionary is not a scope one<br>
vim.VAR_DEF_SCOPE <a class="Identifier" href="eval.html#g:">g:</a> or <a class="Identifier" href="eval.html#l:">l:</a> dictionary<br>
vim.VAR_SCOPE Other scope dictionary,<br>
see <a class="Identifier" href="eval.html#internal-variables">internal-variables</a><br>
Methods (<span class="Todo">note</span>: methods do not support keyword arguments):<br>
<span class="PreProc">Method Description</span><br>
keys() Returns a list with dictionary keys.<br>
values() Returns a list with dictionary values.<br>
items() Returns a list of 2-tuples with dictionary contents.<br>
update(iterable), update(dictionary), update(**kwargs)<br>
Adds keys to dictionary.<br>
get(key[, default=None])<br>
Obtain key from dictionary, returning the default if it is <br>
not present.<br>
pop(key[, default])<br>
Remove specified key from dictionary and return <br>
corresponding value. If key is not found and default is <br>
given returns the default, otherwise raises KeyError.<br>
popitem()<br>
Remove random key from dictionary and return (key, value) <br>
pair.<br>
has_key(key)<br>
Check whether dictionary contains specified key, similar <br>
to `key in dict`.<br>
<br>
__new__(), __new__(iterable), __new__(dictionary), __new__(update)<br>
You can use <span class="Comment">vim.Dictionary()</span> to create new vim <br>
dictionaries. <span class="Comment">d=vim.Dictionary(arg)</span> is the same as <br>
<span class="Comment">d=vim.bindeval('{}');d.update(arg)</span>. Without arguments <br>
constructs empty dictionary.<br>
<br>
Examples:<br>
<div class="helpExample"> d = vim.Dictionary(food="bar") # Constructor<br>
d['a'] = 'b' # Item assignment<br>
print d['a'] # getting item<br>
d.update({'c': 'd'}) # .update(dictionary)<br>
d.update(e='f') # .update(**kwargs)<br>
d.update((('g', 'h'), ('i', 'j'))) # .update(iterable)<br>
for key in d.keys(): # .keys()<br>
for val in d.values(): # .values()<br>
for key, val in d.items(): # .items()<br>
print isinstance(d, vim.Dictionary) # True<br>
for key in d: # Iteration over keys<br>
class Dict(vim.Dictionary): # Subclassing</div>
<br>
<span class="Todo">Note</span>: when iterating over keys you should not modify dictionary.<br>
<br>
vim.List object <a class="Constant" href="if_pyth.html#python-List" name="python-List">python-List</a><br>
Sequence-like object providing access to vim <a class="Identifier" href="eval.html#List">List</a> type.<br>
Supports <span class="Comment">.locked</span> attribute, see <a class="Identifier" href="if_pyth.html#python-.locked">python-.locked</a>. Also supports the <br>
following methods:<br>
<span class="PreProc">Method Description</span><br>
extend(item) Add items to the list.<br>
<br>
__new__(), __new__(iterable)<br>
You can use <span class="Comment">vim.List()</span> to create new vim lists. <br>
<span class="Comment">l=vim.List(iterable)</span> is the same as <br>
<span class="Comment">l=vim.bindeval('[]');l.extend(iterable)</span>. Without <br>
arguments constructs empty list.<br>
Examples:<br>
<div class="helpExample"> l = vim.List("abc") # Constructor, result: ['a', 'b', 'c']<br>
l.extend(['abc', 'def']) # .extend() method<br>
print l[1:] # slicing<br>
l[:0] = ['ghi', 'jkl'] # slice assignment<br>
print l[0] # getting item<br>
l[0] = 'mno' # assignment<br>
for i in l: # iteration<br>
print isinstance(l, vim.List) # True<br>
class List(vim.List): # Subclassing</div>
<br>
vim.Function object <a class="Constant" href="if_pyth.html#python-Function" name="python-Function">python-Function</a><br>
Function-like object, acting like vim <a class="Identifier" href="eval.html#Funcref">Funcref</a> object. Accepts special <br>
keyword argument <a class="Comment" href="eval.html#self">self</a>, see <a class="Identifier" href="eval.html#Dictionary-function">Dictionary-function</a>. You can also use <br>
<span class="Comment">vim.Function(name)</span> constructor, it is the same as <br>
<span class="Comment">vim.bindeval('function(%s)'%json.dumps(name))</span>.<br>
<br>
Attributes (read-only):<br>
<span class="PreProc">Attribute Description</span><br>
name Function name.<br>
args <span class="Comment">None</span> or a <a class="Identifier" href="if_pyth.html#python-List">python-List</a> object with arguments. <span class="Todo">Note</span> <br>
that this is a copy of the arguments list, constructed <br>
each time you request this attribute. Modifications made <br>
to the list will be ignored (but not to the containers <br>
inside argument list: this is like <a class="Identifier" href="eval.html#copy()">copy()</a> and not <br>
<a class="Identifier" href="eval.html#deepcopy()">deepcopy()</a>).<br>
self <span class="Comment">None</span> or a <a class="Identifier" href="if_pyth.html#python-Dictionary">python-Dictionary</a> object with self <br>
dictionary. <span class="Todo">Note</span> that explicit <a class="Comment" href="eval.html#self">self</a> keyword used when <br>
calling resulting object overrides this attribute.<br>
auto_rebind Boolean. True if partial created from this Python object <br>
and stored in the Vim script dictionary should be<br>
automatically rebound to the dictionary it is stored in<br>
when this dictionary is indexed. Exposes Vim internal<br>
difference between <span class="Comment">dict.func</span> (auto_rebind=True) and<br>
<span class="Comment">function(dict.func,dict)</span> (auto_rebind=False). This<br>
attribute makes no sense if <a class="Comment" href="eval.html#self">self</a> attribute is <span class="Comment">None</span>.<br>
<br>
Constructor additionally accepts <a class="Comment" href="editing.html#:args">args</a>, <a class="Comment" href="eval.html#self">self</a> and <span class="Comment">auto_rebind</span> <br>
keywords. If <a class="Comment" href="editing.html#:args">args</a> and/or <a class="Comment" href="eval.html#self">self</a> argument is given then it constructs <br>
a partial, see <a class="Identifier" href="eval.html#function()">function()</a>. <span class="Comment">auto_rebind</span> is only used when <a class="Comment" href="eval.html#self">self</a> <br>
argument is given, otherwise it is assumed to be <span class="Comment">True</span> regardless of <br>
whether it was given or not. If <a class="Comment" href="eval.html#self">self</a> is given then it defaults to <br>
<span class="Comment">False</span>.<br>
<br>
Examples:<br>
<div class="helpExample"> f = vim.Function('tr') # Constructor<br>
print f('abc', 'a', 'b') # Calls tr('abc', 'a', 'b')<br>
vim.command('''<br>
function DictFun() dict<br>
return self<br>
endfunction<br>
''')<br>
f = vim.bindeval('function("DictFun")')<br>
print f(self={}) # Like call('DictFun', [], {})<br>
print isinstance(f, vim.Function) # True</div>
<br>
<div class="helpExample"> p = vim.Function('DictFun', self={})<br>
print f()<br>
p = vim.Function('tr', args=['abc', 'a'])<br>
print f('b')</div>
<br>
<span class="PreProc">==============================================================================</span><br>
8. pyeval() and py3eval() Vim functions <a class="Constant" href="if_pyth.html#python-pyeval" name="python-pyeval">python-pyeval</a><br>
<br>
To facilitate bi-directional interface, you can use <a class="Identifier" href="eval.html#pyeval()">pyeval()</a> and <a class="Identifier" href="eval.html#py3eval()">py3eval()</a> <br>
functions to evaluate Python expressions and pass their values to Vim script.<br>
<a class="Identifier" href="eval.html#pyxeval()">pyxeval()</a> is also available.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
9. Dynamic loading <a class="Constant" href="if_pyth.html#python-dynamic" name="python-dynamic">python-dynamic</a><br>
<br>
On MS-Windows and Unix the Python library can be loaded dynamically. The<br>
<a class="Identifier" href="various.html#:version">:version</a> output then includes <a class="Identifier" href="various.html#+python/dyn">+python/dyn</a> or <a class="Identifier" href="various.html#+python3/dyn">+python3/dyn</a>.<br>
<br>
This means that Vim will search for the Python DLL or shared library file only<br>
when needed. When you don't use the Python interface you don't need it, thus<br>
you can use Vim without this file.<br>
<br>
<br>
<span class="PreProc">MS-Windows</span><br>
<br>
To use the Python interface the Python DLL must be in your search path. In a<br>
console window type "path" to see what directories are used. The <a class="Type" href="options.html#'pythondll'">'pythondll'</a><br>
or <a class="Type" href="options.html#'pythonthreedll'">'pythonthreedll'</a> option can be also used to specify the Python DLL.<br>
<br>
The name of the DLL should match the Python version Vim was compiled with.<br>
Currently the name for Python 2 is "python27.dll", that is for Python 2.7.<br>
That is the default value for <a class="Type" href="options.html#'pythondll'">'pythondll'</a>. For Python 3 it is python35.dll<br>
(Python 3.5). To know for sure edit "gvim.exe" and search for<br>
"python\d*.dll\c".<br>
<br>
<br>
<span class="PreProc">Unix</span><br>
<br>
The <a class="Type" href="options.html#'pythondll'">'pythondll'</a> or <a class="Type" href="options.html#'pythonthreedll'">'pythonthreedll'</a> option can be used to specify the Python<br>
shared library file instead of DYNAMIC_PYTHON_DLL or DYNAMIC_PYTHON3_DLL file<br>
what were specified at compile time. The version of the shared library must<br>
match the Python 2.x or Python 3 version Vim was compiled with.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
10. Python 3 <a class="Constant" href="if_pyth.html#python3" name="python3">python3</a><br>
<br>
<a class="Constant" href="if_pyth.html#:py3" name=":py3">:py3</a> <a class="Constant" href="if_pyth.html#:python3" name=":python3">:python3</a><br>
The <a class="Comment" href="if_pyth.html#:py3">:py3</a> and <a class="Comment" href="if_pyth.html#:python3">:python3</a> commands work similar to <a class="Comment" href="if_pyth.html#:python">:python</a>. A simple check<br>
if the <a class="Comment" href="if_pyth.html#:py3">:py3</a> command is working:<br>
<div class="helpExample"> :py3 print("Hello")</div>
<br>
To see what version of Python you have:<br>
<div class="helpExample"> :py3 import sys<br>
:py3 print(sys.version)</div>
<a class="Constant" href="if_pyth.html#:py3file" name=":py3file">:py3file</a><br>
The <a class="Comment" href="if_pyth.html#:py3file">:py3file</a> command works similar to <a class="Comment" href="if_pyth.html#:pyfile">:pyfile</a>.<br>
<a class="Constant" href="if_pyth.html#:py3do" name=":py3do">:py3do</a><br>
The <a class="Comment" href="if_pyth.html#:py3do">:py3do</a> command works similar to <a class="Comment" href="if_pyth.html#:pydo">:pydo</a>.<br>
<br>
<br>
Vim can be built in four ways (:version output):<br>
1. No Python support (-python, -python3)<br>
2. Python 2 support only (+python or +python/dyn, -python3)<br>
3. Python 3 support only (-python, +python3 or +python3/dyn)<br>
4. Python 2 and 3 support (+python/dyn, +python3/dyn)<br>
<br>
Some more details on the special case 4: <a class="Constant" href="if_pyth.html#python-2-and-3" name="python-2-and-3">python-2-and-3</a><br>
<br>
When Python 2 and Python 3 are both supported they must be loaded dynamically.<br>
<br>
When doing this on Linux/Unix systems and importing global symbols, this leads<br>
to a crash when the second Python version is used. So either global symbols<br>
are loaded but only one Python version is activated, or no global symbols are<br>
loaded. The latter makes Python's "import" fail on libraries that expect the<br>
symbols to be provided by Vim.<br>
<a class="Constant" href="if_pyth.html#E836" name="E836">E836</a> <a class="Constant" href="if_pyth.html#E837" name="E837">E837</a><br>
Vim's configuration script makes a guess for all libraries based on one<br>
standard Python library (termios). If importing this library succeeds for<br>
both Python versions, then both will be made available in Vim at the same<br>
time. If not, only the version first used in a session will be enabled.<br>
When trying to use the other one you will get the E836 or E837 error message.<br>
<br>
Here Vim's behavior depends on the system in which it was configured. In a<br>
system where both versions of Python were configured with --enable-shared,<br>
both versions of Python will be activated at the same time. There will still<br>
be problems with other third party libraries that were not linked to<br>
libPython.<br>
<br>
To work around such problems there are these options:<br>
1. The problematic library is recompiled to link to the according<br>
libpython.so.<br>
2. Vim is recompiled for only one Python version.<br>
3. You undefine PY_NO_RTLD_GLOBAL in auto/config.h after configuration. This<br>
may crash Vim though.<br>
<br>
<a class="Constant" href="if_pyth.html#E880" name="E880">E880</a><br>
Raising SystemExit exception in python isn't endorsed way to quit vim, use:<br>
<div class="helpExample"> :py vim.command("qall!")</div>
<br>
<br>
<a class="Constant" href="if_pyth.html#has-python" name="has-python">has-python</a><br>
You can test what Python version is available with:<br>
<div class="helpExample"> if has('python')<br>
echo 'there is Python 2.x'<br>
elseif has('python3')<br>
echo 'there is Python 3.x'<br>
endif</div>
<br>
<span class="Todo">Note</span> however, that when Python 2 and 3 are both available and loaded<br>
dynamically, these has() calls will try to load them. If only one can be<br>
loaded at a time, just checking if Python 2 or 3 are available will prevent<br>
the other one from being available.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
11. Python X <a class="Constant" href="if_pyth.html#python_x" name="python_x">python_x</a> <a class="Constant" href="if_pyth.html#pythonx" name="pythonx">pythonx</a><br>
<br>
Because most python code can be written so that it works with python 2.6+ and<br>
python 3 the pyx* functions and commands have been written. They work exactly<br>
the same as the Python 2 and 3 variants, but select the Python version using<br>
the <a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> setting.<br>
<br>
You should set <a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> in your <a class="Identifier" href="starting.html#.vimrc">.vimrc</a> to prefer Python 2 or Python 3<br>
for Python commands. If you change this setting at runtime you may risk that<br>
state of plugins (such as initialization) may be lost.<br>
<br>
If you want to use a module, you can put it in the <span class="Special">{rtp}</span>/pythonx directory.<br>
See <a class="Identifier" href="if_pyth.html#pythonx-directory">pythonx-directory</a>.<br>
<br>
<a class="Constant" href="if_pyth.html#:pyx" name=":pyx">:pyx</a> <a class="Constant" href="if_pyth.html#:pythonx" name=":pythonx">:pythonx</a><br>
The <a class="Comment" href="if_pyth.html#:pyx">:pyx</a> and <a class="Comment" href="if_pyth.html#:pythonx">:pythonx</a> commands work similar to <a class="Comment" href="if_pyth.html#:python">:python</a>. A simple check<br>
if the <a class="Comment" href="if_pyth.html#:pyx">:pyx</a> command is working:<br>
<div class="helpExample"> :pyx print("Hello")</div>
<br>
To see what version of Python is being used:<br>
<div class="helpExample"> :pyx import sys<br>
:pyx print(sys.version)</div>
<br>
<a class="Constant" href="if_pyth.html#:pyxfile" name=":pyxfile">:pyxfile</a> <a class="Constant" href="if_pyth.html#python_x-special-comments" name="python_x-special-comments">python_x-special-comments</a><br>
The <a class="Comment" href="if_pyth.html#:pyxfile">:pyxfile</a> command works similar to <a class="Comment" href="if_pyth.html#:pyfile">:pyfile</a>. However you can add one of<br>
these comments to force Vim using <a class="Comment" href="if_pyth.html#:pyfile">:pyfile</a> or <a class="Comment" href="if_pyth.html#:py3file">:py3file</a>:<br>
<div class="helpExample"> #!/any string/python2 " Shebang. Must be the first line of the file.<br>
#!/any string/python3 " Shebang. Must be the first line of the file.<br>
# requires python 2.x " Maximum lines depend on 'modelines'.<br>
# requires python 3.x " Maximum lines depend on 'modelines'.</div>
Unlike normal modelines, the bottom of the file is not checked.<br>
If none of them are found, the <a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> setting is used.<br>
<a class="Constant" href="if_pyth.html#W20" name="W20">W20</a> <a class="Constant" href="if_pyth.html#W21" name="W21">W21</a><br>
If Vim does not support the selected Python version a silent message will be<br>
printed. Use <a class="Comment" href="message.html#:messages">:messages</a> to read them.<br>
<br>
<a class="Constant" href="if_pyth.html#:pyxdo" name=":pyxdo">:pyxdo</a><br>
The <a class="Comment" href="if_pyth.html#:pyxdo">:pyxdo</a> command works similar to <a class="Comment" href="if_pyth.html#:pydo">:pydo</a>.<br>
<br>
<a class="Constant" href="if_pyth.html#has-pythonx" name="has-pythonx">has-pythonx</a><br>
You can test if pyx* commands are available with:<br>
<div class="helpExample"> if has('pythonx')<br>
echo 'pyx* commands are available. (Python ' . &pyx . ')'<br>
endif</div>
<br>
When compiled with only one of <a class="Identifier" href="various.html#+python">+python</a> or <a class="Identifier" href="various.html#+python3">+python3</a>, the has() returns 1.<br>
When compiled with both <a class="Identifier" href="various.html#+python">+python</a> and <a class="Identifier" href="various.html#+python3">+python3</a>, the test depends on the<br>
<a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> setting. If <a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> is 0, it tests Python 3 first, and if<br>
it is not available then Python 2. If <a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> is 2 or 3, it tests only<br>
Python 2 or 3 respectively.<br>
<br>
<span class="Todo">Note</span> that for <span class="Comment">has('pythonx')</span> to work it may try to dynamically load Python 3<br>
or 2. This may have side effects, especially when Vim can only load one of<br>
the two.<br>
<br>
If a user prefers Python 2 and want to fallback to Python 3, he needs to set<br>
<a class="Type" href="options.html#'pyxversion'">'pyxversion'</a> explicitly in his <a class="Identifier" href="starting.html#.vimrc">.vimrc</a>. E.g.:<br>
<div class="helpExample"> if has('python')<br>
set pyx=2<br>
elseif has('python3')<br>
set pyx=3<br>
endif</div>
<br>
<span class="PreProc">==============================================================================</span><br>
12. Building with Python support <a class="Constant" href="if_pyth.html#python-building" name="python-building">python-building</a><br>
<br>
A few hints for building with Python 2 or 3 support.<br>
<br>
UNIX<br>
<br>
See src/Makefile for how to enable including the Python interface.<br>
<br>
On Ubuntu you will want to install these packages for Python 2:<br>
python<br>
python-dev<br>
For Python 3:<br>
python3<br>
pytyon3-dev<br>
For Python 3.6:<br>
python3.6<br>
pytyon3.6-dev<br>
<br>
If you have more than one version of Python 3, you need to link python3 to the<br>
one you prefer, before running configure.<br>
<br>
<span class="PreProc">==============================================================================</span><br>
vim:tw=78:ts=8:ft=help:norl:<br>
</div>
</article>
<footer>
<a href="#top">Return to the top</a> - <a href="index.html">Return to main</a>
<span class="EnglishJapaneseLink">
<span class="CurrentLanguage">English</span>
</span>
<br />
<div style="text-align:right;">
Hosted by <a href="https://github.com/vim-jp/vimdoc-en">vimdoc-en project</a><br />
If you met any problem, please report it to <a href="https://github.com/vim-jp/vimdoc-en/issues">issue</a>.<br />
</div>
</footer>
<!--<script src="js/check-referrer.js" type="text/javascript"></script>-->
</body>
</html>
<!-- vim:set ts=8 sts=2 sw=2 tw=0 et: -->
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。