I am trying to use PYthon to interface with VIM in my vimrc. I am aware that this can be done in vimrc in VIML, but I don't want to learn VIML too much, and am already familiar with Python.

I looked into some of the examples here: http://vimhelp.appspot.com/if_pyth.txt.html#python-window

Its mentioned here the width is a parameter of any window object.

However, when I do py3 print(dir(vim.current.window)), it gives me the following output: ['__dir__', 'buffer', 'col', 'cursor', 'height', 'number', 'options', 'row', 'tabpage', 'v alid', 'vars'].

This doesn't contain width as one of the attributes of current.window, however, when I use the following ex command: :py3 print(vim.current.window.width)

It does give me a output of 90, so it seems width is indeed an attribute of vim.current.window.

Is this a bug or how this is supposed to happen?

1 Answer 1


I had posted a couple theories as to why width, a valid field in vim.current.window, wasn't showing up in the output of dir(). The actual reason has been astutely uncovered by @Mass...

Objects are allowed to provide hints to dir() by listing the object's fields in an array. vim.current.window does so but it's not accurate. It contains...

"buffer", "cursor", "height", "vars", "options", "number", "row", "col", "tabpage", "valid"

Yeah, those are exactly the fields dir() is returning. See https://github.com/vim/vim/blob/d79a26219d7161e9211fd144f0e874aa5f6d251e/src/if_py_both.h#L3839

Anyways, regardless of that error, width is a valid field and your best bet is to follow the Vim examples that you linked and access the fields like this:

:py cw = vim.current.window
:py width = cw.width

Or just use vim.current.window.width as you've done.

  • Ok.. I see.. The vim documentation is very space regarding the variables contained in any of the objects, so I was using the dir() command, which basically prints out the contents of that vim-object using the print statement in python .
    – alpha_989
    Jun 9, 2018 at 23:34
  • @alpha_989 It was worth a try. :) But, like I said, it's hard to say what exactly the exposed Vim/Python objects are. Meanwhile, dir() expects things to be purely/strictly Python, I'm sure.
    – B Layer
    Jun 9, 2018 at 23:36
  • Thanks no problem.. I guess, I will have to live with that..
    – alpha_989
    Jun 9, 2018 at 23:38
  • This line is the problem; it could easily be fixed: github.com/vim/vim/blob/…
    – Mass
    Jun 10, 2018 at 0:10
  • @Mass Oh, good catch. I think the main question has been answered ("Is 'width' a parameter...") but I'll mention your discovery in my answer if you don't mind.
    – B Layer
    Jun 10, 2018 at 0:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.