1

From vim doc:

vim.command(str)                    *python-command*
    Executes the vim (ex-mode) command str.  Returns None.



vim.eval(str)                       *python-eval*
    Evaluates the expression str using the vim internal expression
    evaluator (see |expression|).  Returns the expression result as:
    - a string if the Vim expression evaluates to a string or number
    - a list if the Vim expression evaluates to a Vim list
    - a dictionary if the Vim expression evaluates to a Vim dictionary
    Dictionaries and lists are recursively expanded.

I am having trouble to distinguish these two, perhaps it's because I don't understand the difference between expression and command in the first place. An explanation with some examples is very very welcome.

1
  • expression in substitute commands, expression-commands, vim.command, yeah i also stuck at that, when i started with vim... If you really plan to work with vim, you should read the whole help document, which can be found here vimdoc.sourceforge.net/htmldoc/help.html Commented Feb 28, 2014 at 12:39

2 Answers 2

7

TL;DR: Use command() to execute a Vim command for its side effects, and eval() to get back a value computed by a Vimscript function.

Like other programming languages, Vim(script) distinguishes between a procedure (which you just invoke, but get nothing back; the interesting thing is the actions it performs), and a function (that returns a value, and can optionally also perform actions like a procedure).

Example

With :split foo.txt, you invoke this to open a file in a window split. The command returns nothing, but its effects can be easily observed (another window opens, a file being edited in there). You'd use vim.command() for that.

With :echo winnr('$'), you query the number of open windows. The :echo prints that value, but if you wanted that in Python, you'd use vim.eval("winnr('$')"). (But note that certain Vim properties are already exposed by the Python integration in Vim; you'd only use this for stuff that's not available in Python yet.)

Sign up to request clarification or add additional context in comments.

Comments

1

assume you have two strings:

"5d"

and

"5+5"

if you call command() on those, it is same as in vim press :, then input command.

"5d" -> remove the 5th line
"5+5" -> move cursor to 10th line

if you call eval(), vim evaluates the string as vim expression

"5d" -> error
"5+5" -> 10

1 Comment

I actually had a issue.. where vim.eval works generally find for most strings such as vim.eval('a:items'), but if a:items happens to contain BOM headers, vim.eval crashes.. Other people such as @ludovicchabant has also found it here:ludovic.chabant.com/devblog/2017/02/25/aaa-gamedev-with-vim. I wonder if you know of alternate way of evaluating/transfering a variable from the vimscript namespace to the python namespace? vi.stackexchange.com/questions/16756/…

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.