Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 1 | *if_pyth.txt* For Vim version 7.0aa. Last change: 2004 Jul 25 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 94 | :py print "Hello" # displays a message |
| 95 | :py vim.command(cmd) # execute an ex command |
| 96 | :py w = vim.windows[n] # gets window "n" |
| 97 | :py cw = vim.current.window # gets the current window |
| 98 | :py b = vim.buffers[n] # gets buffer "n" |
| 99 | :py cb = vim.current.buffer # gets the current buffer |
| 100 | :py w.height = lines # sets the window height |
| 101 | :py w.cursor = (row, col) # sets the window cursor position |
| 102 | :py pos = w.cursor # gets a tuple (row, col) |
| 103 | :py name = b.name # gets the buffer file name |
| 104 | :py line = b[n] # gets a line from the buffer |
| 105 | :py lines = b[n:m] # gets a list of lines |
| 106 | :py num = len(b) # gets the number of lines |
| 107 | :py b[n] = str # sets a line in the buffer |
| 108 | :py b[n:m] = [str1, str2, str3] # sets a number of lines at once |
| 109 | :py del b[n] # deletes a line |
| 110 | :py del b[n:m] # deletes a number of lines |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 118 | :py vim.command("set tw=72") |
| 119 | :py vim.command("%s/aaa/bbb/g") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 129 | :py vim.command("python print 'Hello again Python'") |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 136 | :py text_width = vim.eval("&tw") |
| 137 | :py str = vim.eval("12+12") # NB result is a string! Use |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 161 | :py b = vim.buffers[i] # Indexing (read-only) |
| 162 | :py b in vim.buffers # Membership test |
| 163 | :py n = len(vim.buffers) # Number of elements |
| 164 | :py for b in vim.buffers: # Sequential access |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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: > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 169 | :py w = vim.windows[i] # Indexing (read-only) |
| 170 | :py w in vim.windows # Membership test |
| 171 | :py n = len(vim.windows) # Number of elements |
| 172 | :py for w in vim.windows: # Sequential access |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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 | |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 239 | Note that when adding a line it must not contain a line break character '\n'. |
| 240 | A trailing '\n' is allowed and ignored, so that you can do: > |
| 241 | :py b.append(f.readlines()) |
| 242 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 243 | Examples (assume b is the current buffer) > |
Bram Moolenaar | 5eb86f9 | 2004-07-26 12:53:41 +0000 | [diff] [blame] | 244 | :py print b.name # write the buffer file name |
| 245 | :py b[0] = "hello!!!" # replace the top line |
| 246 | :py b[:] = None # delete the whole buffer |
| 247 | :py del b[:] # delete the whole buffer |
| 248 | :py b[0:0] = [ "a line" ] # add a line at the top |
| 249 | :py del b[2] # delete a line (the third) |
| 250 | :py b.append("bottom") # add a line at the bottom |
| 251 | :py n = len(b) # number of lines |
| 252 | :py (row,col) = b.mark('a') # named mark |
| 253 | :py r = b.range(1,5) # a sub-range of the buffer |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 254 | |
| 255 | ============================================================================== |
| 256 | 4. Range objects *python-range* |
| 257 | |
| 258 | Range objects represent a part of a vim buffer. You can obtain them in a |
| 259 | number of ways: |
| 260 | - via vim.current.range (|python-current|) |
| 261 | - from a buffer's range() method (|python-buffer|) |
| 262 | |
| 263 | A range object is almost identical in operation to a buffer object. However, |
| 264 | all operations are restricted to the lines within the range (this line range |
| 265 | can, of course, change as a result of slice assignments, line deletions, or |
| 266 | the range.append() method). |
| 267 | |
| 268 | The range object attributes are: |
| 269 | r.start Index of first line into the buffer |
| 270 | r.end Index of last line into the buffer |
| 271 | |
| 272 | The range object methods are: |
| 273 | r.append(str) Append a line to the range |
| 274 | r.append(list) Append a list of lines to the range |
| 275 | Note that the option of supplying a list of strings to |
| 276 | the append method differs from the equivalent method |
| 277 | for Python's built-in list objects. |
| 278 | |
| 279 | Example (assume r is the current range): |
| 280 | # Send all lines in a range to the default printer |
| 281 | vim.command("%d,%dhardcopy!" % (r.start+1,r.end+1)) |
| 282 | |
| 283 | ============================================================================== |
| 284 | 5. Window objects *python-window* |
| 285 | |
| 286 | Window objects represent vim windows. You can obtain them in a number of ways: |
| 287 | - via vim.current.window (|python-current|) |
| 288 | - from indexing vim.windows (|python-windows|) |
| 289 | |
| 290 | You can manipulate window objects only through their attributes. They have no |
| 291 | methods, and no sequence or other interface. |
| 292 | |
| 293 | Window attributes are: |
| 294 | buffer (read-only) The buffer displayed in this window |
| 295 | cursor (read-write) The current cursor position in the window |
| 296 | This is a tuple, (row,col). |
| 297 | height (read-write) The window height, in rows |
| 298 | width (read-write) The window width, in columns |
| 299 | The height attribute is writable only if the screen is split horizontally. |
| 300 | The width attribute is writable only if the screen is split vertically. |
| 301 | |
| 302 | ============================================================================== |
| 303 | vim:tw=78:ts=8:ft=help:norl: |