Skip to main content
added 2301 characters in body
Source Link
Christian Brabandt
  • 26.5k
  • 1
  • 54
  • 80

The problemWhat you are seeing here, is essentially the same problem, when entering thisas why many people struggle with using variables on the command line with the ex commands. See e.g. this question, there are a lot similar ones.

Basically, Vims ex commandline commands expect their arguments to be literal, they cannot contain expressions (like variables or strings). Those commands do handle % and # (and a few others that I do not remember currently) special by replacing the former with the current filename and the latter by the alternate file name. At the same time, " is used to donate the start of a comment, everything after it will be ignored (not even seen by the command). The usage of " as quotea comment character goes back all the way to the original vi and therefore cannot be changed because this would introduce an incompatibility to the classic vi. So

A few commands, that were not available in the restoriginal vi implementation, however are able to handle expressions. The help usually mentions whenever a command expects an expression instead of a (literal) argument. The most notable ex commands that do that are :exe, :echo, :echomsg and similar ex commands. That's why one has to use :exe to dynamically build ex commands (see e.g. this answer for a great explanation).

Those expressions can contain variables and strings, either single quoted or double quoted and therefore the command" is not detected as the start of a comment there. However, :put is one of the old ex commands that was available already in old original vi. However adding the possibility to put an arbitrary expression using the expression register = has been added for Vim only. So while you can use any expression when using :put=, Vim will still parse the arguments for the :put comments and see " as a start of a comment and not as start of a string.

That is an implementation detail, that can still be seen by Vimtoday. Try escaping

Note, the error message would have given you a hint:

error message after executing above command

That was me executing your 3rd example:

pu=map(getcompletion('*','event'), {i,v -> "h" . v})

See how the echoed command stops after the -> and does not contain anything after the comment?

That's why you have to escape the " using e.g. \\"\", on the command line for the :put command. So try to use this does workinstead:

:pu =map(getcompletion('*','event'), {i,v -> \"h\" . v})

The problem is, when entering this on the command line, the " is seen as quote character. So the rest of the command will not be seen by Vim. Try escaping the " using \\", So this does work:

:pu =map(getcompletion('*','event'), {i,v -> \"h\" . v})

What you are seeing here, is essentially the same problem, as why many people struggle with using variables on the command line with the ex commands. See e.g. this question, there are a lot similar ones.

Basically, Vims ex commandline commands expect their arguments to be literal, they cannot contain expressions (like variables or strings). Those commands do handle % and # (and a few others that I do not remember currently) special by replacing the former with the current filename and the latter by the alternate file name. At the same time, " is used to donate the start of a comment, everything after it will be ignored (not even seen by the command). The usage of " as a comment character goes back all the way to the original vi and therefore cannot be changed because this would introduce an incompatibility to the classic vi.

A few commands, that were not available in the original vi implementation, however are able to handle expressions. The help usually mentions whenever a command expects an expression instead of a (literal) argument. The most notable ex commands that do that are :exe, :echo, :echomsg and similar ex commands. That's why one has to use :exe to dynamically build ex commands (see e.g. this answer for a great explanation).

Those expressions can contain variables and strings, either single quoted or double quoted and therefore the " is not detected as the start of a comment there. However, :put is one of the old ex commands that was available already in old original vi. However adding the possibility to put an arbitrary expression using the expression register = has been added for Vim only. So while you can use any expression when using :put=, Vim will still parse the arguments for the :put comments and see " as a start of a comment and not as start of a string.

That is an implementation detail, that can still be seen today.

Note, the error message would have given you a hint:

error message after executing above command

That was me executing your 3rd example:

pu=map(getcompletion('*','event'), {i,v -> "h" . v})

See how the echoed command stops after the -> and does not contain anything after the comment?

That's why you have to escape the " using e.g. \" on the command line for the :put command. So try to use this instead:

:pu =map(getcompletion('*','event'), {i,v -> \"h\" . v})
Source Link
Christian Brabandt
  • 26.5k
  • 1
  • 54
  • 80

The problem is, when entering this on the command line, the " is seen as quote character. So the rest of the command will not be seen by Vim. Try escaping the " using \\", So this does work:

:pu =map(getcompletion('*','event'), {i,v -> \"h\" . v})