edge ˆ¾Æ 0Äà À 0¿Äà ˆ¾Æ 0ÄÃ,ˆl–ßi~”Šj"Tâ�Àà€¦-ÿÇ „eæ™o_‡I|¥ŠèªÆÅ?÷*if_pyth.txt* For Vim version 8.2. Last change: 2019 Dec 07 VIM REFERENCE MANUAL by Paul Moore The Python Interface to Vim *python* *Python* 1. Commands |python-commands| 2. The vim module |python-vim| 3. Buffer objects |python-buffer| 4. Range objects |python-range| 5. Window objects |python-window| 6. Tab page objects |python-tabpage| 7. vim.bindeval objects |python-bindeval-objects| 8. pyeval(), py3eval() Vim functions |python-pyeval| 9. Dynamic loading |python-dynamic| 10. Python 3 |python3| 11. Python X |python_x| 12. Building with Python support |python-building| The Python 2.x interface is available only when Vim was compiled with the |+python| feature. The Python 3 interface is available only when Vim was compiled with the |+python3| feature. Both can be available at the same time, but read |python-2-and-3|. ============================================================================== 1. Commands *python-commands* *:python* *:py* *E263* *E264* *E887* :[range]py[thon] {stmt} Execute Python statement {stmt}. A simple check if the `:python` command is working: > :python print "Hello" :[range]py[thon] << [trim] [{endmarker}] {script} {endmarker} Execute Python script {script}. Note: This command doesn't work when the Python feature wasn't compiled in. To avoid errors, see |script-here|. If [endmarker] is omitted from after the "<<", a dot '.' must be used after {script}, like for the |:append| and |:insert| commands. Refer to |:let-heredoc| for more information. This form of the |:python| command is mainly useful for including python code in Vim scripts. Example: > function! IcecreamInitialize() python << EOF class StrawberryIcecream: def __call__(self): print 'EAT ME' EOF endfunction To see what version of Python you have: > :python print(sys.version) There is no need to import sys, it's done by default. Note: Python is very sensitive to the indenting. Make sure the "class" line and "EOF" do not have any indent. *:pydo* :[range]pydo {body} Execute Python function "def _vim_pydo(line, linenr): {body}" for each line in the [range], with the function arguments being set to the text of each line in turn, without a trailing , and the current line number. The function should return a string or None. If a string is returned, it becomes the text of the line in the current turn. The default for [range] is the whole file: "1,$". Examples: > :pydo return "%s\t%d" % (line[::-1], len(line)) :pydo if line: return "%4d: %s" % (linenr, line) < One can use `:pydo` in possible conjunction with `:py` to filter a range using python. For example: > :py3 << EOF needle = vim.eval('@a') replacement = vim.eval('@b') def py_vim_string_replace(str): return str.replace(needle, replacement) EOF :'<,'>py3do return py_vim_string_replace(line) < *:pyfile* *:pyf* :[range]pyf[ile] {file} Execute the Python script in {file}. The whole argument is used as a single file name. Both of these commands do essentially the same thing - they execute a piece of Python code, with the "current range" |python-range| set to the given line range. In the case of :python, the code to execute is in the command-line. In the case of :pyfile, the code to execute is the contents of the given file. Python commands cannot be used in the |sandbox|. To pass arguments you need to set sys.argv[] explicitly. Example: > :python sys.argv = ["foo", "bar"] :pyfile myscript.py Here are some examples *python-examples* > :python from vim import * :python from string import upper :python current.line = upper(current.line) :python print "Hello" :python str = current.buffer[42] (Note that changes - like the imports - persist from one command to the next, just like in the Python interpreter.) ============================================================================== 2. The vim