blob: 3ceeff88e29389b98494066f16d7936d344b2404 [file] [log] [blame]
Bram Moolenaara5792f52005-11-23 21:25:05 +00001*if_pyth.txt* For Vim version 7.0aa. Last change: 2005 Oct 14
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Paul Moore
5
6
7The Python Interface to Vim *python* *Python*
8
91. Commands |python-commands|
102. The vim module |python-vim|
113. Buffer objects |python-buffer|
124. Range objects |python-range|
135. Window objects |python-window|
Bram Moolenaara5792f52005-11-23 21:25:05 +0000146. Dynamic loading |python-dynamic|
Bram Moolenaar071d4272004-06-13 20:20:40 +000015
16{Vi does not have any of these commands}
17
18The Python interface is available only when Vim was compiled with the
19|+python| feature.
20
21==============================================================================
221. Commands *python-commands*
23
24 *:python* *:py* *E205* *E263* *E264*
25:[range]py[thon] {stmt}
26 Execute Python statement {stmt}.
27
28:[range]py[thon] << {endmarker}
29{script}
30{endmarker}
31 Execute Python script {script}.
32 Note: This command doesn't work when the Python
33 feature wasn't compiled in. To avoid errors, see
34 |script-here|.
35
36{endmarker} must NOT be preceded by any white space. If {endmarker} is
37omitted from after the "<<", a dot '.' must be used after {script}, like
38for the |:append| and |:insert| commands.
39This form of the |:python| command is mainly useful for including python code
40in Vim scripts.
41
42Example: >
43 function! IcecreamInitialize()
44 python << EOF
45 class StrawberryIcecream:
46 def __call__(self):
47 print 'EAT ME'
48 EOF
49 endfunction
50<
51Note: Python is very sensitive to the indenting. Also make sure the "class"
52line and "EOF" do not have any indent.
53
54 *:pyfile* *:pyf*
55:[range]pyf[ile] {file}
56 Execute the Python script in {file}. The whole
57 argument is used as a single file name. {not in Vi}
58
59Both of these commands do essentially the same thing - they execute a piece of
60Python code, with the "current range" |python-range| set to the given line
61range.
62
63In the case of :python, the code to execute is in the command-line.
64In the case of :pyfile, the code to execute is the contents of the given file.
65
66Python commands cannot be used in the |sandbox|.
67
68To pass arguments you need to set sys.argv[] explicitly. Example: >
69
70 :python import sys
71 :python sys.argv = ["foo", "bar"]
72 :pyfile myscript.py
73
74Here are some examples *python-examples* >
75
76 :python from vim import *
77 :python from string import upper
78 :python current.line = upper(current.line)
79 :python print "Hello"
80 :python str = current.buffer[42]
81
82(Note that changes - like the imports - persist from one command to the next,
83just like in the Python interpreter.)
84
85==============================================================================
862. The vim module *python-vim*
87
88Python code gets all of its access to vim (with one exception - see
Bram Moolenaar402d2fe2005-04-15 21:00:38 +000089|python-output| below) via the "vim" module. The vim module implements two
Bram Moolenaar071d4272004-06-13 20:20:40 +000090methods, three constants, and one error object. You need to import the vim
91module before using it: >
92 :python import vim
93
94Overview >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000095 :py print "Hello" # displays a message
96 :py vim.command(cmd) # execute an ex command
97 :py w = vim.windows[n] # gets window "n"
98 :py cw = vim.current.window # gets the current window
99 :py b = vim.buffers[n] # gets buffer "n"
100 :py cb = vim.current.buffer # gets the current buffer
101 :py w.height = lines # sets the window height
102 :py w.cursor = (row, col) # sets the window cursor position
103 :py pos = w.cursor # gets a tuple (row, col)
104 :py name = b.name # gets the buffer file name
105 :py line = b[n] # gets a line from the buffer
106 :py lines = b[n:m] # gets a list of lines
107 :py num = len(b) # gets the number of lines
108 :py b[n] = str # sets a line in the buffer
109 :py b[n:m] = [str1, str2, str3] # sets a number of lines at once
110 :py del b[n] # deletes a line
111 :py del b[n:m] # deletes a number of lines
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112
113
114Methods of the "vim" module
115
116vim.command(str) *python-command*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000117 Executes the vim (ex-mode) command str. Returns None.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118 Examples: >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000119 :py vim.command("set tw=72")
120 :py vim.command("%s/aaa/bbb/g")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121< The following definition executes Normal mode commands: >
122 def normal(str):
123 vim.command("normal "+str)
124 # Note the use of single quotes to delimit a string containing
125 # double quotes
126 normal('"a2dd"aP')
127< *E659*
128 The ":python" command cannot be used recursively with Python 2.2 and
129 older. This only works with Python 2.3 and later: >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000130 :py vim.command("python print 'Hello again Python'")
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131
132vim.eval(str) *python-eval*
133 Evaluates the expression str using the vim internal expression
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000134 evaluator (see |expression|). Returns the expression result as a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 string.
136 Examples: >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000137 :py text_width = vim.eval("&tw")
138 :py str = vim.eval("12+12") # NB result is a string! Use
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139 # string.atoi() to convert to
140 # a number.
141
142Error object of the "vim" module
143
144vim.error *python-error*
145 Upon encountering a Vim error, Python raises an exception of type
146 vim.error.
147 Example: >
148 try:
149 vim.command("put a")
150 except vim.error:
151 # nothing in register a
152
153Constants of the "vim" module
154
155 Note that these are not actually constants - you could reassign them.
156 But this is silly, as you would then lose access to the vim objects
157 to which the variables referred.
158
159vim.buffers *python-buffers*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000160 A sequence object providing access to the list of vim buffers. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 object supports the following operations: >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000162 :py b = vim.buffers[i] # Indexing (read-only)
163 :py b in vim.buffers # Membership test
164 :py n = len(vim.buffers) # Number of elements
165 :py for b in vim.buffers: # Sequential access
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166<
167vim.windows *python-windows*
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000168 A sequence object providing access to the list of vim windows. The
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 object supports the following operations: >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000170 :py w = vim.windows[i] # Indexing (read-only)
171 :py w in vim.windows # Membership test
172 :py n = len(vim.windows) # Number of elements
173 :py for w in vim.windows: # Sequential access
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174<
175vim.current *python-current*
176 An object providing access (via specific attributes) to various
177 "current" objects available in vim:
178 vim.current.line The current line (RW) String
179 vim.current.buffer The current buffer (RO) Buffer
180 vim.current.window The current window (RO) Window
181 vim.current.range The current line range (RO) Range
182
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000183 The last case deserves a little explanation. When the :python or
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 :pyfile command specifies a range, this range of lines becomes the
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000185 "current range". A range is a bit like a buffer, but with all access
186 restricted to a subset of lines. See |python-range| for more details.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188
189Output from Python *python-output*
190 Vim displays all Python code output in the Vim message area. Normal
191 output appears as information messages, and error output appears as
192 error messages.
193
194 In implementation terms, this means that all output to sys.stdout
195 (including the output from print statements) appears as information
196 messages, and all output to sys.stderr (including error tracebacks)
197 appears as error messages.
198
199 *python-input*
200 Input (via sys.stdin, including input() and raw_input()) is not
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000201 supported, and may cause the program to crash. This should probably be
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 fixed.
203
204==============================================================================
2053. Buffer objects *python-buffer*
206
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000207Buffer objects represent vim buffers. You can obtain them in a number of ways:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 - via vim.current.buffer (|python-current|)
209 - from indexing vim.buffers (|python-buffers|)
210 - from the "buffer" attribute of a window (|python-window|)
211
212Buffer objects have one read-only attribute - name - the full file name for
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000213the buffer. They also have three methods (append, mark, and range; see below).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000215You can also treat buffer objects as sequence objects. In this context, they
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216act as if they were lists (yes, they are mutable) of strings, with each
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000217element being a line of the buffer. All of the usual sequence operations,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218including indexing, index assignment, slicing and slice assignment, work as
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000219you would expect. Note that the result of indexing (slicing) a buffer is a
220string (list of strings). This has one unusual consequence - b[:] is different
221from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222"b = None" merely updates the variable b, with no effect on the buffer.
223
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000224Buffer indexes start at zero, as is normal in Python. This differs from vim
225line numbers, which start from 1. This is particularly relevant when dealing
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226with marks (see below) which use vim line numbers.
227
228The buffer object methods are:
229 b.append(str) Append a line to the buffer
230 b.append(list) Append a list of lines to the buffer
231 Note that the option of supplying a list of strings to
232 the append method differs from the equivalent method
233 for Python's built-in list objects.
234 b.mark(name) Return a tuple (row,col) representing the position
235 of the named mark (can also get the []"<> marks)
236 b.range(s,e) Return a range object (see |python-range|) which
237 represents the part of the given buffer between line
238 numbers s and e |inclusive|.
239
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000240Note that when adding a line it must not contain a line break character '\n'.
241A trailing '\n' is allowed and ignored, so that you can do: >
242 :py b.append(f.readlines())
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244Examples (assume b is the current buffer) >
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000245 :py print b.name # write the buffer file name
246 :py b[0] = "hello!!!" # replace the top line
247 :py b[:] = None # delete the whole buffer
248 :py del b[:] # delete the whole buffer
249 :py b[0:0] = [ "a line" ] # add a line at the top
250 :py del b[2] # delete a line (the third)
251 :py b.append("bottom") # add a line at the bottom
252 :py n = len(b) # number of lines
253 :py (row,col) = b.mark('a') # named mark
254 :py r = b.range(1,5) # a sub-range of the buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256==============================================================================
2574. Range objects *python-range*
258
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000259Range objects represent a part of a vim buffer. You can obtain them in a
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260number of ways:
261 - via vim.current.range (|python-current|)
262 - from a buffer's range() method (|python-buffer|)
263
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000264A range object is almost identical in operation to a buffer object. However,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265all operations are restricted to the lines within the range (this line range
266can, of course, change as a result of slice assignments, line deletions, or
267the range.append() method).
268
269The range object attributes are:
270 r.start Index of first line into the buffer
271 r.end Index of last line into the buffer
272
273The range object methods are:
274 r.append(str) Append a line to the range
275 r.append(list) Append a list of lines to the range
276 Note that the option of supplying a list of strings to
277 the append method differs from the equivalent method
278 for Python's built-in list objects.
279
280Example (assume r is the current range):
281 # Send all lines in a range to the default printer
282 vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1))
283
284==============================================================================
2855. Window objects *python-window*
286
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000287Window objects represent vim windows. You can obtain them in a number of ways:
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 - via vim.current.window (|python-current|)
289 - from indexing vim.windows (|python-windows|)
290
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000291You can manipulate window objects only through their attributes. They have no
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292methods, and no sequence or other interface.
293
294Window attributes are:
295 buffer (read-only) The buffer displayed in this window
296 cursor (read-write) The current cursor position in the window
297 This is a tuple, (row,col).
298 height (read-write) The window height, in rows
299 width (read-write) The window width, in columns
300The height attribute is writable only if the screen is split horizontally.
301The width attribute is writable only if the screen is split vertically.
302
303==============================================================================
Bram Moolenaara5792f52005-11-23 21:25:05 +00003046. Dynamic loading *python-dynamic*
305
306On MS-Windows the Python library can be loaded dynamically. The |:version|
307output then includes |+python/dyn|.
308
309This means that Vim will search for the Python DLL file only when needed.
310When you don't use the Python interface you don't need it, thus you can use
311Vim without this DLL file.
312
313To use the Python interface the Python DLL must be in your search path. In a
314console window type "path" to see what directories are used.
315
316The name of the DLL must match the Python version Vim was compiled with.
317Currently the name is "python24.dll". That is for Python 2.4. To know for
318sure edit "gvim.exe" and search for "python\d*.dll\c".
319
320==============================================================================
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 vim:tw=78:ts=8:ft=help:norl: