1

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
++foo
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.

  • 1
    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 at 20:28
  • 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 at 20:31
  • If you want to learn Vimscript, my recommendation is the excellent "Learn Vimscript the Hard Way" by Steve Losh. – filbranden May 16 at 20:32
3

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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