7
|
1 *if_pyth.txt* For Vim version 7.0aa. Last change: 2004 Feb 28
|
|
2
|
|
3
|
|
4 VIM REFERENCE MANUAL by Paul Moore
|
|
5
|
|
6
|
|
7 The Python Interface to Vim *python* *Python*
|
|
8
|
|
9 1. Commands |python-commands|
|
|
10 2. The vim module |python-vim|
|
|
11 3. Buffer objects |python-buffer|
|
|
12 4. Range objects |python-range|
|
|
13 5. Window objects |python-window|
|
|
14
|
|
15 {Vi does not have any of these commands}
|
|
16
|
|
17 The Python interface is available only when Vim was compiled with the
|
|
18 |+python| feature.
|
|
19
|
|
20 ==============================================================================
|
|
21 1. Commands *python-commands*
|
|
22
|
|
23 *:python* *:py* *E205* *E263* *E264*
|
|
24 :[range]py[thon] {stmt}
|
|
25 Execute Python statement {stmt}.
|
|
26
|
|
27 :[range]py[thon] << {endmarker}
|
|
28 {script}
|
|
29 {endmarker}
|
|
30 Execute Python script {script}.
|
|
31 Note: This command doesn't work when the Python
|
|
32 feature wasn't compiled in. To avoid errors, see
|
|
33 |script-here|.
|
|
34
|
|
35 {endmarker} must NOT be preceded by any white space. If {endmarker} is
|
|
36 omitted from after the "<<", a dot '.' must be used after {script}, like
|
|
37 for the |:append| and |:insert| commands.
|
|
38 This form of the |:python| command is mainly useful for including python code
|
|
39 in Vim scripts.
|
|
40
|
|
41 Example: >
|
|
42 function! IcecreamInitialize()
|
|
43 python << EOF
|
|
44 class StrawberryIcecream:
|
|
45 def __call__(self):
|
|
46 print 'EAT ME'
|
|
47 EOF
|
|
48 endfunction
|
|
49 <
|
|
50 Note: Python is very sensitive to the indenting. Also make sure the "class"
|
|
51 line and "EOF" do not have any indent.
|
|
52
|
|
53 *:pyfile* *:pyf*
|
|
54 :[range]pyf[ile] {file}
|
|
55 Execute the Python script in {file}. The whole
|
|
56 argument is used as a single file name. {not in Vi}
|
|
57
|
|
58 Both of these commands do essentially the same thing - they execute a piece of
|
|
59 Python code, with the "current range" |python-range| set to the given line
|
|
60 range.
|
|
61
|
|
62 In the case of :python, the code to execute is in the command-line.
|
|
63 In the case of :pyfile, the code to execute is the contents of the given file.
|
|
64
|
|
65 Python commands cannot be used in the |sandbox|.
|
|
66
|
|
67 To pass arguments you need to set sys.argv[] explicitly. Example: >
|
|
68
|
|
69 :python import sys
|
|
70 :python sys.argv = ["foo", "bar"]
|
|
71 :pyfile myscript.py
|
|
72
|
|
73 Here are some examples *python-examples* >
|
|
74
|
|
75 :python from vim import *
|
|
76 :python from string import upper
|
|
77 :python current.line = upper(current.line)
|
|
78 :python print "Hello"
|
|
79 :python str = current.buffer[42]
|
|
80
|
|
81 (Note that changes - like the imports - persist from one command to the next,
|
|
82 just like in the Python interpreter.)
|
|
83
|
|
84 ==============================================================================
|
|
85 2. The vim module *python-vim*
|
|
86
|
|
87 Python code gets all of its access to vim (with one exception - see
|
|
88 |python-output| below) via the "vim" module. The vim module implements two
|
|
89 methods, three constants, and one error object. You need to import the vim
|
|
90 module before using it: >
|
|
91 :python import vim
|
|
92
|
|
93 Overview >
|
|
94 print "Hello" # displays a message
|
|
95 vim.command(cmd) # execute an ex command
|
|
96 w = vim.windows[n] # gets window "n"
|
|
97 cw = vim.current.window # gets the current window
|
|
98 b = vim.buffers[n] # gets buffer "n"
|
|
99 cb = vim.current.buffer # gets the current buffer
|
|
100 w.height = lines # sets the window height
|
|
101 w.cursor = (row, col) # sets the window cursor position
|
|
102 pos = w.cursor # gets a tuple (row, col)
|
|
103 name = b.name # gets the buffer file name
|
|
104 line = b[n] # gets a line from the buffer
|
|
105 lines = b[n:m] # gets a list of lines
|
|
106 num = len(b) # gets the number of lines
|
|
107 b[n] = str # sets a line in the buffer
|
|
108 b[n:m] = [str1, str2, str3] # sets a number of lines at once
|
|
109 del b[n] # deletes a line
|
|
110 del b[n:m] # deletes a number of lines
|
|
111
|
|
112
|
|
113 Methods of the "vim" module
|
|
114
|
|
115 vim.command(str) *python-command*
|
|
116 Executes the vim (ex-mode) command str. Returns None.
|
|
117 Examples: >
|
|
118 vim.command("set tw=72")
|
|
119 vim.command("%s/aaa/bbb/g")
|
|
120 < The following definition executes Normal mode commands: >
|
|
121 def normal(str):
|
|
122 vim.command("normal "+str)
|
|
123 # Note the use of single quotes to delimit a string containing
|
|
124 # double quotes
|
|
125 normal('"a2dd"aP')
|
|
126 < *E659*
|
|
127 The ":python" command cannot be used recursively with Python 2.2 and
|
|
128 older. This only works with Python 2.3 and later: >
|
|
129 :python vim.command("python print 'Hello again Python'")
|
|
130
|
|
131 vim.eval(str) *python-eval*
|
|
132 Evaluates the expression str using the vim internal expression
|
|
133 evaluator (see |expression|). Returns the expression result as a
|
|
134 string.
|
|
135 Examples: >
|
|
136 text_width = vim.eval("&tw")
|
|
137 str = vim.eval("12+12") # NB result is a string! Use
|
|
138 # string.atoi() to convert to
|
|
139 # a number.
|
|
140
|
|
141 Error object of the "vim" module
|
|
142
|
|
143 vim.error *python-error*
|
|
144 Upon encountering a Vim error, Python raises an exception of type
|
|
145 vim.error.
|
|
146 Example: >
|
|
147 try:
|
|
148 vim.command("put a")
|
|
149 except vim.error:
|
|
150 # nothing in register a
|
|
151
|
|
152 Constants of the "vim" module
|
|
153
|
|
154 Note that these are not actually constants - you could reassign them.
|
|
155 But this is silly, as you would then lose access to the vim objects
|
|
156 to which the variables referred.
|
|
157
|
|
158 vim.buffers *python-buffers*
|
|
159 A sequence object providing access to the list of vim buffers. The
|
|
160 object supports the following operations: >
|
|
161 b = vim.buffers[i] # Indexing (read-only)
|
|
162 b in vim.buffers # Membership test
|
|
163 n = len(vim.buffers) # Number of elements
|
|
164 for b in vim.buffers: # Sequential access
|
|
165 <
|
|
166 vim.windows *python-windows*
|
|
167 A sequence object providing access to the list of vim windows. The
|
|
168 object supports the following operations: >
|
|
169 w = vim.windows[i] # Indexing (read-only)
|
|
170 w in vim.windows # Membership test
|
|
171 n = len(vim.windows) # Number of elements
|
|
172 for w in vim.windows: # Sequential access
|
|
173 <
|
|
174 vim.current *python-current*
|
|
175 An object providing access (via specific attributes) to various
|
|
176 "current" objects available in vim:
|
|
177 vim.current.line The current line (RW) String
|
|
178 vim.current.buffer The current buffer (RO) Buffer
|
|
179 vim.current.window The current window (RO) Window
|
|
180 vim.current.range The current line range (RO) Range
|
|
181
|
|
182 The last case deserves a little explanation. When the :python or
|
|
183 :pyfile command specifies a range, this range of lines becomes the
|
|
184 "current range". A range is a bit like a buffer, but with all access
|
|
185 restricted to a subset of lines. See |python-range| for more details.
|
|
186
|
|
187
|
|
188 Output from Python *python-output*
|
|
189 Vim displays all Python code output in the Vim message area. Normal
|
|
190 output appears as information messages, and error output appears as
|
|
191 error messages.
|
|
192
|
|
193 In implementation terms, this means that all output to sys.stdout
|
|
194 (including the output from print statements) appears as information
|
|
195 messages, and all output to sys.stderr (including error tracebacks)
|
|
196 appears as error messages.
|
|
197
|
|
198 *python-input*
|
|
199 Input (via sys.stdin, including input() and raw_input()) is not
|
|
200 supported, and may cause the program to crash. This should probably be
|
|
201 fixed.
|
|
202
|
|
203 ==============================================================================
|
|
204 3. Buffer objects *python-buffer*
|
|
205
|
|
206 Buffer objects represent vim buffers. You can obtain them in a number of ways:
|
|
207 - via vim.current.buffer (|python-current|)
|
|
208 - from indexing vim.buffers (|python-buffers|)
|
|
209 - from the "buffer" attribute of a window (|python-window|)
|
|
210
|
|
211 Buffer objects have one read-only attribute - name - the full file name for
|
|
212 the buffer. They also have three methods (append, mark, and range; see below).
|
|
213
|
|
214 You can also treat buffer objects as sequence objects. In this context, they
|
|
215 act as if they were lists (yes, they are mutable) of strings, with each
|
|
216 element being a line of the buffer. All of the usual sequence operations,
|
|
217 including indexing, index assignment, slicing and slice assignment, work as
|
|
218 you would expect. Note that the result of indexing (slicing) a buffer is a
|
|
219 string (list of strings). This has one unusual consequence - b[:] is different
|
|
220 from b. In particular, "b[:] = None" deletes the whole of the buffer, whereas
|
|
221 "b = None" merely updates the variable b, with no effect on the buffer.
|
|
222
|
|
223 Buffer indexes start at zero, as is normal in Python. This differs from vim
|
|
224 line numbers, which start from 1. This is particularly relevant when dealing
|
|
225 with marks (see below) which use vim line numbers.
|
|
226
|
|
227 The buffer object methods are:
|
|
228 b.append(str) Append a line to the buffer
|
|
229 b.append(list) Append a list of lines to the buffer
|
|
230 Note that the option of supplying a list of strings to
|
|
231 the append method differs from the equivalent method
|
|
232 for Python's built-in list objects.
|
|
233 b.mark(name) Return a tuple (row,col) representing the position
|
|
234 of the named mark (can also get the []"<> marks)
|
|
235 b.range(s,e) Return a range object (see |python-range|) which
|
|
236 represents the part of the given buffer between line
|
|
237 numbers s and e |inclusive|.
|
|
238
|
|
239 Examples (assume b is the current buffer) >
|
|
240 print b.name # write the buffer file name
|
|
241 b[0] = "hello!!!" # replace the top line
|
|
242 b[:] = None # delete the whole buffer
|
|
243 del b[:] # delete the whole buffer (same as above)
|
|
244 b[0:0] = [ "a line" ] # add a line at the top
|
|
245 del b[2] # delete a line (the third)
|
|
246 b.append("bottom") # add a line at the bottom
|
|
247 n = len(b) # number of lines
|
|
248 (row,col) = b.mark('a') # named mark
|
|
249 r = b.range(1,5) # a sub-range of the buffer
|
|
250
|
|
251 ==============================================================================
|
|
252 4. Range objects *python-range*
|
|
253
|
|
254 Range objects represent a part of a vim buffer. You can obtain them in a
|
|
255 number of ways:
|
|
256 - via vim.current.range (|python-current|)
|
|
257 - from a buffer's range() method (|python-buffer|)
|
|
258
|
|
259 A range object is almost identical in operation to a buffer object. However,
|
|
260 all operations are restricted to the lines within the range (this line range
|
|
261 can, of course, change as a result of slice assignments, line deletions, or
|
|
262 the range.append() method).
|
|
263
|
|
264 The range object attributes are:
|
|
265 r.start Index of first line into the buffer
|
|
266 r.end Index of last line into the buffer
|
|
267
|
|
268 The range object methods are:
|
|
269 r.append(str) Append a line to the range
|
|
270 r.append(list) Append a list of lines to the range
|
|
271 Note that the option of supplying a list of strings to
|
|
272 the append method differs from the equivalent method
|
|
273 for Python's built-in list objects.
|
|
274
|
|
275 Example (assume r is the current range):
|
|
276 # Send all lines in a range to the default printer
|
|
277 vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1))
|
|
278
|
|
279 ==============================================================================
|
|
280 5. Window objects *python-window*
|
|
281
|
|
282 Window objects represent vim windows. You can obtain them in a number of ways:
|
|
283 - via vim.current.window (|python-current|)
|
|
284 - from indexing vim.windows (|python-windows|)
|
|
285
|
|
286 You can manipulate window objects only through their attributes. They have no
|
|
287 methods, and no sequence or other interface.
|
|
288
|
|
289 Window attributes are:
|
|
290 buffer (read-only) The buffer displayed in this window
|
|
291 cursor (read-write) The current cursor position in the window
|
|
292 This is a tuple, (row,col).
|
|
293 height (read-write) The window height, in rows
|
|
294 width (read-write) The window width, in columns
|
|
295 The height attribute is writable only if the screen is split horizontally.
|
|
296 The width attribute is writable only if the screen is split vertically.
|
|
297
|
|
298 ==============================================================================
|
|
299 vim:tw=78:ts=8:ft=help:norl:
|