if_pyth.txt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. *if_pyth.txt* Nvim
  2. NVIM REFERENCE MANUAL
  3. The Python Interface to NVim *if_pyth* *python* *Python*
  4. See |provider-python| for more information.
  5. Type |gO| to see the table of contents.
  6. ==============================================================================
  7. Commands *python-commands*
  8. *:python* *:py* *E263* *E264* *E887*
  9. :[range]py[thon] {stmt}
  10. Execute Python statement {stmt}. A simple check if
  11. the `:python` command is working: >
  12. :python print "Hello"
  13. :[range]py[thon] << [endmarker]
  14. {script}
  15. {endmarker}
  16. Execute Python script {script}. Useful for including
  17. python code in Vim scripts. Requires Python, see
  18. |script-here|.
  19. The {endmarker} below the {script} must NOT be preceded by any white space.
  20. If [endmarker] is omitted from after the "<<", a dot '.' must be used after
  21. {script}, like for the |:append| and |:insert| commands.
  22. Example: >
  23. function! IcecreamInitialize()
  24. python << EOF
  25. class StrawberryIcecream:
  26. def __call__(self):
  27. print 'EAT ME'
  28. EOF
  29. endfunction
  30. To see what version of Python you have: >
  31. :python print(sys.version)
  32. There is no need to "import sys", it's done by default.
  33. *python-environment*
  34. Environment variables set in Vim are not always available in Python. This
  35. depends on how Vim and Python were build. Also see
  36. https://docs.python.org/3/library/os.html#os.environ
  37. Note: Python is very sensitive to indenting. Make sure the "class" line and
  38. "EOF" do not have any indent.
  39. *:pydo*
  40. :[range]pydo {body} Execute Python function "def _vim_pydo(line, linenr):
  41. {body}" for each line in the [range], with the
  42. function arguments being set to the text of each line
  43. in turn, without a trailing <EOL>, and the current
  44. line number. The function should return a string or
  45. None. If a string is returned, it becomes the text of
  46. the line in the current turn. The default for [range]
  47. is the whole file: "1,$".
  48. Examples:
  49. >
  50. :pydo return "%s\t%d" % (line[::-1], len(line))
  51. :pydo if line: return "%4d: %s" % (linenr, line)
  52. <
  53. One can use `:pydo` in possible conjunction with `:py` to filter a range using
  54. python. For example: >
  55. :py3 << EOF
  56. needle = vim.eval('@a')
  57. replacement = vim.eval('@b')
  58. def py_vim_string_replace(str):
  59. return str.replace(needle, replacement)
  60. EOF
  61. :'<,'>py3do return py_vim_string_replace(line)
  62. <
  63. *:pyfile* *:pyf*
  64. :[range]pyf[ile] {file}
  65. Execute the Python script in {file}. The whole
  66. argument is used as a single file name.
  67. Both of these commands do essentially the same thing - they execute a piece of
  68. Python code, with the "current range" |python-range| set to the given line
  69. range.
  70. In the case of :python, the code to execute is in the command-line.
  71. In the case of :pyfile, the code to execute is the contents of the given file.
  72. Python commands cannot be used in the |sandbox|.
  73. To pass arguments you need to set sys.argv[] explicitly. Example: >
  74. :python sys.argv = ["foo", "bar"]
  75. :pyfile myscript.py
  76. Here are some examples *python-examples* >
  77. :python from vim import *
  78. :python from string import upper
  79. :python current.line = upper(current.line)
  80. :python print "Hello"
  81. :python str = current.buffer[42]
  82. Note that changes (such as the "import" statements) persist from one command
  83. to the next, just like the Python REPL.
  84. *script-here*
  85. When using a script language in-line, you might want to skip this when the
  86. language isn't supported. Note that this mechanism doesn't work:
  87. >
  88. if has('python')
  89. python << EOF
  90. this will NOT work!
  91. EOF
  92. endif
  93. Instead, put the Python command in a function and call that function:
  94. >
  95. if has('python')
  96. function DefPython()
  97. python << EOF
  98. this works
  99. EOF
  100. endfunction
  101. call DefPython()
  102. endif
  103. Note that "EOF" must be at the start of the line.
  104. ==============================================================================
  105. The vim module *python-vim*
  106. Python code gets all of its access to vim (with one exception - see
  107. |python-output| below) via the "vim" module. The vim module implements two
  108. methods, three constants, and one error object. You need to import the vim
  109. module before using it: >
  110. :python import vim
  111. Overview >
  112. :py print "Hello" # displays a message
  113. :py vim.command(cmd) # execute an Ex command
  114. :py w = vim.windows[n] # gets window "n"
  115. :py cw = vim.current.window # gets the current window
  116. :py b = vim.buffers[n] # gets buffer "n"
  117. :py cb = vim.current.buffer # gets the current buffer
  118. :py w.height = lines # sets the window height
  119. :py w.cursor = (row, col) # sets the window cursor position
  120. :py pos = w.cursor # gets a tuple (row, col)
  121. :py name = b.name # gets the buffer file name
  122. :py line = b[n] # gets a line from the buffer
  123. :py lines = b[n:m] # gets a list of lines
  124. :py num = len(b) # gets the number of lines
  125. :py b[n] = str # sets a line in the buffer
  126. :py b[n:m] = [str1, str2, str3] # sets a number of lines at once
  127. :py del b[n] # deletes a line
  128. :py del b[n:m] # deletes a number of lines
  129. Methods of the "vim" module
  130. vim.command(str) *python-command*
  131. Executes the vim (ex-mode) command str. Returns None.
  132. Examples: >
  133. :py vim.command("set tw=72")
  134. :py vim.command("%s/aaa/bbb/g")
  135. < The following definition executes Normal mode commands: >
  136. def normal(str):
  137. vim.command("normal "+str)
  138. # Note the use of single quotes to delimit a string containing
  139. # double quotes
  140. normal('"a2dd"aP')
  141. < *E659*
  142. The ":python" command cannot be used recursively with Python 2.2 and
  143. older. This only works with Python 2.3 and later: >
  144. :py vim.command("python print 'Hello again Python'")
  145. vim.eval(str) *python-eval*
  146. Evaluates the expression str using the vim internal expression
  147. evaluator (see |expression|). Returns the expression result as:
  148. - a string if the Vim expression evaluates to a string or number
  149. - a list if the Vim expression evaluates to a Vim list
  150. - a dictionary if the Vim expression evaluates to a Vim dictionary
  151. Dictionaries and lists are recursively expanded.
  152. Examples: >
  153. :py text_width = vim.eval("&tw")
  154. :py str = vim.eval("12+12") # NB result is a string! Use
  155. # string.atoi() to convert to
  156. # a number.
  157. vim.strwidth(str) *python-strwidth*
  158. Like |strwidth()|: returns number of display cells str occupies, tab
  159. is counted as one cell.
  160. vim.foreach_rtp(callable) *python-foreach_rtp*
  161. Call the given callable for each path in 'runtimepath' until either
  162. callable returns something but None, the exception is raised or there
  163. are no longer paths. If stopped in case callable returned non-None,
  164. vim.foreach_rtp function returns the value returned by callable.
  165. vim.chdir(*args, **kwargs) *python-chdir*
  166. vim.fchdir(*args, **kwargs) *python-fchdir*
  167. Run os.chdir or os.fchdir, then all appropriate vim stuff.
  168. Note: you should not use these functions directly, use os.chdir and
  169. os.fchdir instead. Behavior of vim.fchdir is undefined in case
  170. os.fchdir does not exist.
  171. Error object of the "vim" module
  172. vim.error *python-error*
  173. Upon encountering a Vim error, Python raises an exception of type
  174. vim.error.
  175. Example: >
  176. try:
  177. vim.command("put a")
  178. except vim.error:
  179. # nothing in register a
  180. Constants of the "vim" module
  181. Note that these are not actually constants - you could reassign them.
  182. But this is silly, as you would then lose access to the vim objects
  183. to which the variables referred.
  184. vim.buffers *python-buffers*
  185. A mapping object providing access to the list of vim buffers. The
  186. object supports the following operations: >
  187. :py b = vim.buffers[i] # Indexing (read-only)
  188. :py b in vim.buffers # Membership test
  189. :py n = len(vim.buffers) # Number of elements
  190. :py for b in vim.buffers: # Iterating over buffer list
  191. <
  192. vim.windows *python-windows*
  193. A sequence object providing access to the list of vim windows. The
  194. object supports the following operations: >
  195. :py w = vim.windows[i] # Indexing (read-only)
  196. :py w in vim.windows # Membership test
  197. :py n = len(vim.windows) # Number of elements
  198. :py for w in vim.windows: # Sequential access
  199. < Note: vim.windows object always accesses current tab page.
  200. |python-tabpage|.windows objects are bound to parent |python-tabpage|
  201. object and always use windows from that tab page (or throw vim.error
  202. in case tab page was deleted). You can keep a reference to both
  203. without keeping a reference to vim module object or |python-tabpage|,
  204. they will not lose their properties in this case.
  205. vim.tabpages *python-tabpages*
  206. A sequence object providing access to the list of vim tab pages. The
  207. object supports the following operations: >
  208. :py t = vim.tabpages[i] # Indexing (read-only)
  209. :py t in vim.tabpages # Membership test
  210. :py n = len(vim.tabpages) # Number of elements
  211. :py for t in vim.tabpages: # Sequential access
  212. <
  213. vim.current *python-current*
  214. An object providing access (via specific attributes) to various
  215. "current" objects available in vim:
  216. vim.current.line The current line (RW) String
  217. vim.current.buffer The current buffer (RW) Buffer
  218. vim.current.window The current window (RW) Window
  219. vim.current.tabpage The current tab page (RW) TabPage
  220. vim.current.range The current line range (RO) Range
  221. The last case deserves a little explanation. When the :python or
  222. :pyfile command specifies a range, this range of lines becomes the
  223. "current range". A range is a bit like a buffer, but with all access
  224. restricted to a subset of lines. See |python-range| for more details.
  225. Note: When assigning to vim.current.{buffer,window,tabpage} it expects
  226. valid |python-buffer|, |python-window| or |python-tabpage| objects
  227. respectively. Assigning triggers normal (with |autocommand|s)
  228. switching to given buffer, window or tab page. It is the only way to
  229. switch UI objects in python: you can't assign to
  230. |python-tabpage|.window attribute. To switch without triggering
  231. autocommands use >
  232. py << EOF
  233. saved_eventignore = vim.options['eventignore']
  234. vim.options['eventignore'] = 'all'
  235. try:
  236. vim.current.buffer = vim.buffers[2] # Switch to buffer 2
  237. finally:
  238. vim.options['eventignore'] = saved_eventignore
  239. EOF
  240. <
  241. vim.vars *python-vars*
  242. vim.vvars *python-vvars*
  243. Dictionary-like objects holding dictionaries with global (|g:|) and
  244. vim (|v:|) variables respectively.
  245. vim.options *python-options*
  246. Object partly supporting mapping protocol (supports setting and
  247. getting items) providing a read-write access to global options.
  248. Note: unlike |:set| this provides access only to global options. You
  249. cannot use this object to obtain or set local options' values or
  250. access local-only options in any fashion. Raises KeyError if no global
  251. option with such name exists (i.e. does not raise KeyError for
  252. |global-local| options and global only options, but does for window-
  253. and buffer-local ones). Use |python-buffer| objects to access to
  254. buffer-local options and |python-window| objects to access to
  255. window-local options.
  256. Type of this object is available via "Options" attribute of vim
  257. module.
  258. Output from Python *python-output*
  259. Vim displays all Python code output in the Vim message area. Normal
  260. output appears as information messages, and error output appears as
  261. error messages.
  262. In implementation terms, this means that all output to sys.stdout
  263. (including the output from print statements) appears as information
  264. messages, and all output to sys.stderr (including error tracebacks)
  265. appears as error messages.
  266. *python-input*
  267. Input (via sys.stdin, including input() and raw_input()) is not
  268. supported, and may cause the program to crash. This should probably be
  269. fixed.
  270. *python3-directory* *pythonx-directory*
  271. Python 'runtimepath' handling *python-special-path*
  272. In python vim.VIM_SPECIAL_PATH special directory is used as a replacement for
  273. the list of paths found in 'runtimepath': with this directory in sys.path and
  274. vim.path_hooks in sys.path_hooks python will try to load module from
  275. {rtp}/python3 and {rtp}/pythonx for each {rtp} found in 'runtimepath'.
  276. Implementation is similar to the following, but written in C: >
  277. from imp import find_module, load_module
  278. import vim
  279. import sys
  280. class VimModuleLoader(object):
  281. def __init__(self, module):
  282. self.module = module
  283. def load_module(self, fullname, path=None):
  284. return self.module
  285. def _find_module(fullname, oldtail, path):
  286. idx = oldtail.find('.')
  287. if idx > 0:
  288. name = oldtail[:idx]
  289. tail = oldtail[idx+1:]
  290. fmr = find_module(name, path)
  291. module = load_module(fullname[:-len(oldtail)] + name, *fmr)
  292. return _find_module(fullname, tail, module.__path__)
  293. else:
  294. fmr = find_module(fullname, path)
  295. return load_module(fullname, *fmr)
  296. # It uses vim module itself in place of VimPathFinder class: it does not
  297. # matter for python which object has find_module function attached to as
  298. # an attribute.
  299. class VimPathFinder(object):
  300. @classmethod
  301. def find_module(cls, fullname, path=None):
  302. try:
  303. return VimModuleLoader(_find_module(fullname, fullname, path or vim._get_paths()))
  304. except ImportError:
  305. return None
  306. @classmethod
  307. def load_module(cls, fullname, path=None):
  308. return _find_module(fullname, fullname, path or vim._get_paths())
  309. def hook(path):
  310. if path == vim.VIM_SPECIAL_PATH:
  311. return VimPathFinder
  312. else:
  313. raise ImportError
  314. sys.path_hooks.append(hook)
  315. vim.VIM_SPECIAL_PATH *python-VIM_SPECIAL_PATH*
  316. String constant used in conjunction with vim path hook. If path hook
  317. installed by vim is requested to handle anything but path equal to
  318. vim.VIM_SPECIAL_PATH constant it raises ImportError. In the only other
  319. case it uses special loader.
  320. Note: you must not use value of this constant directly, always use
  321. vim.VIM_SPECIAL_PATH object.
  322. vim.find_module(...) *python-find_module*
  323. vim.path_hook(path) *python-path_hook*
  324. Methods or objects used to implement path loading as described above.
  325. You should not be using any of these directly except for vim.path_hook
  326. in case you need to do something with sys.meta_path. It is not
  327. guaranteed that any of the objects will exist in the future vim
  328. versions.
  329. vim._get_paths *python-_get_paths*
  330. Methods returning a list of paths which will be searched for by path
  331. hook. You should not rely on this method being present in future
  332. versions, but can use it for debugging.
  333. It returns a list of {rtp}/python3 and {rtp}/pythonx
  334. directories for each {rtp} in 'runtimepath'.
  335. ==============================================================================
  336. Buffer objects *python-buffer*
  337. Buffer objects represent vim buffers. You can obtain them in a number of ways:
  338. - via vim.current.buffer (|python-current|)
  339. - from indexing vim.buffers (|python-buffers|)
  340. - from the "buffer" attribute of a window (|python-window|)
  341. Buffer objects have two read-only attributes - name - the full file name for
  342. the buffer, and number - the buffer number. They also have three methods
  343. (append, mark, and range; see below).
  344. You can also treat buffer objects as sequence objects. In this context, they
  345. act as if they were lists (yes, they are mutable) of strings, with each
  346. element being a line of the buffer. All of the usual sequence operations,
  347. including indexing, index assignment, slicing and slice assignment, work as
  348. you would expect. Note that the result of indexing (slicing) a buffer is a
  349. string (list of strings). This has one unusual consequence - b[:] is different
  350. from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas
  351. "b = None" merely updates the variable b, with no effect on the buffer.
  352. Buffer indexes start at zero, as is normal in Python. This differs from vim
  353. line numbers, which start from 1. This is particularly relevant when dealing
  354. with marks (see below) which use vim line numbers.
  355. The buffer object attributes are:
  356. b.vars Dictionary-like object used to access
  357. |buffer-variable|s.
  358. b.options Mapping object (supports item getting, setting and
  359. deleting) that provides access to buffer-local options
  360. and buffer-local values of |global-local| options. Use
  361. |python-window|.options if option is window-local,
  362. this object will raise KeyError. If option is
  363. |global-local| and local value is missing getting it
  364. will return None.
  365. b.name String, RW. Contains buffer name (full path).
  366. Note: when assigning to b.name |BufFilePre| and
  367. |BufFilePost| autocommands are launched.
  368. b.number Buffer number. Can be used as |python-buffers| key.
  369. Read-only.
  370. b.valid True or False. Buffer object becomes invalid when
  371. corresponding buffer is wiped out.
  372. The buffer object methods are:
  373. b.append(str) Append a line to the buffer
  374. b.append(str, nr) Idem, below line "nr"
  375. b.append(list) Append a list of lines to the buffer
  376. Note that the option of supplying a list of strings to
  377. the append method differs from the equivalent method
  378. for Python's built-in list objects.
  379. b.append(list, nr) Idem, below line "nr"
  380. b.mark(name) Return a tuple (row,col) representing the position
  381. of the named mark (can also get the []"<> marks)
  382. b.range(s,e) Return a range object (see |python-range|) which
  383. represents the part of the given buffer between line
  384. numbers s and e |inclusive|.
  385. Note that when adding a line it must not contain a line break character '\n'.
  386. A trailing '\n' is allowed and ignored, so that you can do: >
  387. :py b.append(f.readlines())
  388. Buffer object type is available using "Buffer" attribute of vim module.
  389. Examples (assume b is the current buffer) >
  390. :py print b.name # write the buffer file name
  391. :py b[0] = "hello!!!" # replace the top line
  392. :py b[:] = None # delete the whole buffer
  393. :py del b[:] # delete the whole buffer
  394. :py b[0:0] = [ "a line" ] # add a line at the top
  395. :py del b[2] # delete a line (the third)
  396. :py b.append("bottom") # add a line at the bottom
  397. :py n = len(b) # number of lines
  398. :py (row,col) = b.mark('a') # named mark
  399. :py r = b.range(1,5) # a sub-range of the buffer
  400. :py b.vars["foo"] = "bar" # assign b:foo variable
  401. :py b.options["ff"] = "dos" # set fileformat
  402. :py del b.options["ar"] # same as :set autoread<
  403. ==============================================================================
  404. Range objects *python-range*
  405. Range objects represent a part of a vim buffer. You can obtain them in a
  406. number of ways:
  407. - via vim.current.range (|python-current|)
  408. - from a buffer's range() method (|python-buffer|)
  409. A range object is almost identical in operation to a buffer object. However,
  410. all operations are restricted to the lines within the range (this line range
  411. can, of course, change as a result of slice assignments, line deletions, or
  412. the range.append() method).
  413. The range object attributes are:
  414. r.start Index of first line into the buffer
  415. r.end Index of last line into the buffer
  416. The range object methods are:
  417. r.append(str) Append a line to the range
  418. r.append(str, nr) Idem, after line "nr"
  419. r.append(list) Append a list of lines to the range
  420. Note that the option of supplying a list of strings to
  421. the append method differs from the equivalent method
  422. for Python's built-in list objects.
  423. r.append(list, nr) Idem, after line "nr"
  424. Range object type is available using "Range" attribute of vim module.
  425. Example (assume r is the current range):
  426. # Send all lines in a range to the default printer
  427. vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1))
  428. ==============================================================================
  429. Window objects *python-window*
  430. Window objects represent vim windows. You can obtain them in a number of ways:
  431. - via vim.current.window (|python-current|)
  432. - from indexing vim.windows (|python-windows|)
  433. - from indexing "windows" attribute of a tab page (|python-tabpage|)
  434. - from the "window" attribute of a tab page (|python-tabpage|)
  435. You can manipulate window objects only through their attributes. They have no
  436. methods, and no sequence or other interface.
  437. Window attributes are:
  438. buffer (read-only) The buffer displayed in this window
  439. cursor (read-write) The current cursor position in the window
  440. This is a tuple, (row,col).
  441. height (read-write) The window height, in rows
  442. width (read-write) The window width, in columns
  443. vars (read-only) The window |w:| variables. Attribute is
  444. unassignable, but you can change window
  445. variables this way
  446. options (read-only) The window-local options. Attribute is
  447. unassignable, but you can change window
  448. options this way. Provides access only to
  449. window-local options, for buffer-local use
  450. |python-buffer| and for global ones use
  451. |python-options|. If option is |global-local|
  452. and local value is missing getting it will
  453. return None.
  454. number (read-only) Window number. The first window has number 1.
  455. This is zero in case it cannot be determined
  456. (e.g. when the window object belongs to other
  457. tab page).
  458. row, col (read-only) On-screen window position in display cells.
  459. First position is zero.
  460. tabpage (read-only) Window tab page.
  461. valid (read-write) True or False. Window object becomes invalid
  462. when corresponding window is closed.
  463. The height attribute is writable only if the screen is split horizontally.
  464. The width attribute is writable only if the screen is split vertically.
  465. Window object type is available using "Window" attribute of vim module.
  466. ==============================================================================
  467. Tab page objects *python-tabpage*
  468. Tab page objects represent vim tab pages. You can obtain them in a number of
  469. ways:
  470. - via vim.current.tabpage (|python-current|)
  471. - from indexing vim.tabpages (|python-tabpages|)
  472. You can use this object to access tab page windows. They have no methods and
  473. no sequence or other interfaces.
  474. Tab page attributes are:
  475. number The tab page number like the one returned by
  476. |tabpagenr()|.
  477. windows Like |python-windows|, but for current tab page.
  478. vars The tab page |t:| variables.
  479. window Current tabpage window.
  480. valid True or False. Tab page object becomes invalid when
  481. corresponding tab page is closed.
  482. TabPage object type is available using "TabPage" attribute of vim module.
  483. ==============================================================================
  484. pyeval() and py3eval() Vim functions *python-pyeval*
  485. To facilitate bi-directional interface, you can use |pyeval()| and |py3eval()|
  486. functions to evaluate Python expressions and pass their values to Vim script.
  487. |pyxeval()| is also available.
  488. ==============================================================================
  489. Python 3 *python3*
  490. As Python 3 is the only supported version in Nvim, "python" is synonymous
  491. with "python3" in the current version. However, code that aims to support older
  492. versions of Neovim, as well as Vim, should prefer to use "python3"
  493. variants explicitly if Python 3 is required.
  494. *:py3* *:python3*
  495. :[range]py3 {stmt}
  496. :[range]py3 << [endmarker]
  497. {script}
  498. {endmarker}
  499. :[range]python3 {stmt}
  500. :[range]python3 << [endmarker]
  501. {script}
  502. {endmarker}
  503. The `:py3` and `:python3` commands work similar to `:python`. A
  504. simple check if the `:py3` command is working: >
  505. :py3 print("Hello")
  506. <
  507. To see what version of Python you have: >
  508. :py3 import sys
  509. :py3 print(sys.version)
  510. < *:py3file*
  511. :[range]py3f[ile] {file}
  512. The `:py3file` command works similar to `:pyfile`.
  513. *:py3do*
  514. :[range]py3do {body}
  515. The `:py3do` command works similar to `:pydo`.
  516. *E880*
  517. Raising SystemExit exception in python isn't endorsed way to quit vim, use: >
  518. :py vim.command("qall!")
  519. <
  520. *has-python*
  521. You can test if Python is available with: >
  522. if has('pythonx')
  523. echo 'there is Python'
  524. endif
  525. if has('python3')
  526. echo 'there is Python 3.x'
  527. endif
  528. Python 2 is no longer supported. Thus `has('python')` always returns
  529. zero for backwards compatibility reasons.
  530. ==============================================================================
  531. Python X *python_x* *pythonx*
  532. The "pythonx" and "pyx" prefixes were introduced for python code which
  533. works with Python 2.6+ and Python 3. As Nvim only supports Python 3,
  534. all these commands are now synonymous to their "python3" equivalents.
  535. *:pyx* *:pythonx*
  536. `:pyx` and `:pythonx` work the same as `:python3`. To check if `:pyx` works: >
  537. :pyx print("Hello")
  538. To see what version of Python is being used: >
  539. :pyx import sys
  540. :pyx print(sys.version)
  541. <
  542. *:pyxfile* *python_x-special-comments*
  543. `:pyxfile` works the same as `:py3file`.
  544. *:pyxdo*
  545. `:pyxdo` works the same as `:py3do`.
  546. *has-pythonx*
  547. To check if `pyx*` functions and commands are available: >
  548. if has('pythonx')
  549. echo 'pyx* commands are available. (Python ' .. &pyx .. ')'
  550. endif
  551. ==============================================================================
  552. vim:tw=78:ts=8:noet:ft=help:norl: