5. The Python Interface to Vim
5
• Document: $VIMRUNTIME/doc/if_pyth.txt
• :help python
6. The Python Interface to Vim
6
• Check your Vim is compiled with +python.
• :version
7. The Python Interface to Vim
7
• :python {stmt}
• :python <<EOF
• ...multiline...
• EOF
8. The Python Interface to Vim
8
• :pydo {body}
• Execute "def _vim_pydo(line, linenr)"
• :pyfile {file}
• Find the sum of line
• :pydo return str(sum(map(int, line.split())))
• Execute the current file
• :pyfile %
9. The vim module
• 'import vim' in vim
• :python import vim
• Via vim module, Python code can access:
• vim windows
• vim buffers
• read and change vim buffers
• ......
9
10. The vim module
• vim.command(str)
• Execute the vim (ex-mode) command.
• vim.eval(str)
• Evaluates the vim expression.
• vim.chdir(*args, **kwargs)
• Change the current directory.
10
11. The vim module
• vim.tabpages
• A tabpage may contain many windows.
• vim.windows
• vim.buffers
• vim.current
• vim.current.tabpage
• vim.curent.window
• vim.current.buffer
11
12. The vim module
• Vim Tabpage Object
• t.number
• t.windows
• window list
• t.window
• current window
• t.vars
• t: variables
12
13. The vim module
• Vim Window Object
• w.number
• w.buffer
• w.cursor
• w.height, w.width
• w.vars
• w: variables
• w.tabpage
• Point to its tabpage.
13
14. The vim module
• Vim Buffer Object
• b[linenr] = line
• The content of the buffer.
• b.name
• b.vars
• b: variables
• b.options
14
15. Example - Conque
• https://code.google.com/p/conque/
• A terminal emulator which uses a Vim buffer to
display the program output.
15
16. Example - python-mode
• https://github.com/klen/python-mode
• A vim plugin that helps you to create python code
very quickly by utilizing libraries including pylint,
rope, pydoc, pyflakes, pep8, and mccabe for
features like static analysis, refactoring, folding,
completion, documentation, and more.
16