I’m trying to do something which sound super easy, but for some reason it’s not working. The command:
:m 10
moves the current line to right below line 10, and
:echo line(".") - 2
prints out the line number of the line two lines up from the cursor. After reading the documentation, I wrote this command:
:m line(".") - 2
It resulted in this error:
M14: Invalid address
So I figured that functions aren’t evaluated unless I use the = symbol, so I tried:
:m =line(".") - 2
Which gave me the same error. Just to be sure the spaces wasn’t the cause, I tried:
:m =line(".")
Which still gives me the same error! What am I doing wrong here?
I have made sure that :m accepts integers and that line() returns integers.
:echo type(5)
0
:echo type(line("."))
0
In order to evaluate an expression and pass it to a ex-mode command, you need to use the
executecommand. In your case, this works:You can think of
executeas a function taking a single variable"m" line(".") - 2. This variable is evaluated and then executed as a string in ex-mode.For more help, see
:help execute.