Home Explore Blog CI



neovim

2nd chunk of `runtime/doc/if_pyth.txt`
66a19e99d006e98ac427500cf17dee4b7bd0b1cd4c76636d0000000100000fa1
 myscript.py

Here are some examples					*python-examples*
>vim
	:python from vim import *
	:python current.line = str.upper(current.line)
	:python print("Hello")
	:python str = current.buffer[42]

Note that changes (such as the "import" statements) persist from one command
to the next, just like the Python REPL.

							*script-here*
When using a script language in-line, you might want to skip this when the
language isn't supported.
>vim
   if has('python')
     python << EOF
       print("python works")
   EOF
   endif
<
Note that "EOF" must be at the start of the line without preceding white
space.

==============================================================================
The vim module							*python-vim*

Python code gets all of its access to vim (with one exception - see
|python-output| below) via the "vim" module.  The vim module implements two
methods, three constants, and one error object.  You need to import the vim
module before using it: >vim
	:python import vim

Overview >vim
	:py print("Hello")		# displays a message
	:py vim.command(cmd)		# execute an Ex command
	:py w = vim.windows[n]		# gets window "n"
	:py cw = vim.current.window	# gets the current window
	:py b = vim.buffers[n]		# gets buffer "n"
	:py cb = vim.current.buffer	# gets the current buffer
	:py w.height = lines		# sets the window height
	:py w.cursor = (row, col)	# sets the window cursor position
	:py pos = w.cursor		# gets a tuple (row, col)
	:py name = b.name		# gets the buffer file name
	:py line = b[n]			# gets a line from the buffer
	:py lines = b[n:m]		# gets a list of lines
	:py num = len(b)		# gets the number of lines
	:py b[n] = str			# sets a line in the buffer
	:py b[n:m] = [str1, str2, str3]	# sets a number of lines at once
	:py del b[n]			# deletes a line
	:py del b[n:m]			# deletes a number of lines


Methods of the "vim" module

vim.command(str)					*python-command*
	Executes the vim (ex-mode) command str.  Returns None.
	Examples: >vim
	    :py vim.command("set tw=72")
	    :py vim.command("%s/aaa/bbb/g")
<	The following definition executes Normal mode commands: >python
		def normal(str):
			vim.command("normal "+str)
		# Note the use of single quotes to delimit a string containing
		# double quotes
		normal('"a2dd"aP')

vim.eval(str)						*python-eval*
	Evaluates the expression str using the vim internal expression
	evaluator (see |expression|).  Returns the expression result as:
	- a string if the Vim expression evaluates to a string or number
	- a list if the Vim expression evaluates to a Vim list
	- a dictionary if the Vim expression evaluates to a Vim dictionary
	Dictionaries and lists are recursively expanded.
	Examples: >vim
	    :py text_width = vim.eval("&tw")
	    :py str = vim.eval("12+12")		# NB result is a string! Use
						# int() to convert to a
						# number.

vim.strwidth(str)					*python-strwidth*
	Like |strwidth()|: returns number of display cells str occupies, tab
	is counted as one cell.

vim.foreach_rtp(callable)				*python-foreach_rtp*
	Call the given callable for each path in 'runtimepath' until either
	callable returns something but None, the exception is raised or there
	are no longer paths. If stopped in case callable returned non-None,
	vim.foreach_rtp function returns the value returned by callable.

`vim.chdir(*args, **kwargs)`				*python-chdir*
`vim.fchdir(*args, **kwargs)`				*python-fchdir*
	Run os.chdir or os.fchdir, then all appropriate vim stuff.
	Note: you should not use these functions directly, use os.chdir and
	      os.fchdir instead. Behavior of vim.fchdir is undefined in case
	      os.fchdir does not exist.

Error object of the "vim" module

vim.error						*python-error*
	Upon encountering a Vim error, Python raises an exception of type
	vim.error.
	Example: >python
		try:
			vim.command("put a")
		except vim.error:
			# nothing in register a

Constants of the "vim" module

	Note that these are not actually constants - you could reassign them.
	But this is silly, as you would then lose

Title: NVim Python Interface: Examples, Scripting, and the `vim` Module Details
Summary
This section provides examples of using Python within NVim, including manipulating buffer lines and executing Vim commands. It explains how to conditionally execute Python code. The main focus is detailing the `vim` module, covering its methods like `vim.command()` and `vim.eval()`, as well as the `vim.error` exception. It also covers `vim.strwidth()`, `vim.foreach_rtp()`, `vim.chdir()` and `vim.fchdir()`. The section describes how to interact with Vim's internal functions and handle errors that may occur during execution.