My question might be silly since I'm a new vimscript user.

I need to define a variable as follows:

let foo = 1
"Implementing foo
:echo foo

Without success, I tried to increment my variable with the following syntaxes:

foo = foo + 1

I might miss something quite obvious, but I can't figure what.

I know about the <C-a> shortcut but I'm searching for a syntactical way of doing things.

  • 3
    You still need to use the :let command, so either :let foo = foo + 1 or :let foo += 1 will work. You don't need the : if you're in a vimscript, only when typing commands from inside Vim itself.
    – filbranden
    May 16, 2020 at 20:28
  • 1
    The <C-a> key binding is a Normal mode command, so it's not really related to Vim variables. It acts on the buffer contents instead.
    – filbranden
    May 16, 2020 at 20:31
  • 1
    If you want to learn Vimscript, my recommendation is the excellent "Learn Vimscript the Hard Way" by Steve Losh.
    – filbranden
    May 16, 2020 at 20:32

1 Answer 1


In vim script, everything has to be an ex (:) command. Thus, the way vim distinguishes a command named foo and changes to the variable foo is the let command:

let foo += 1

This is not so in vim9script, where

foo += 1

works fine, and so does ++foo (but not (yet) in an expression).

Your Answer

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

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