HBergeron
HBergeron

Reputation: 21

vim script: can't echo newline after input and if statement

The following function does not echo the result variable.

fu! Test()
  let input = input(">")
  let result = "error\n"
  if 1
    echo result
  endif
endf

Removing the newline from result, removing the input, or removing the if statement will fix this issue. Any ideas why this happens?

In my actual function the result variable is set from executing a system command and I would prefer not parsing/correcting the result before echoing it.

Upvotes: 2

Views: 1106

Answers (2)

Kevin
Kevin

Reputation: 186

Vimscript can be strange. When I have issues with echo not showing when it should, usually a call to 'redraw' either before or after the echo fixes it for me.

Upvotes: 2

Ben Klein
Ben Klein

Reputation: 1959

Try replacing the \n newline with \r, as mentioned at “How to replace a character for a newline in Vim?”:

fu! Test()
  let input = input(">")
  let result = "error\r"
  if 1
    echo result
  endif
endf

Note that in running the above function I do not get the input cleared before result is echoed, so that if I enter >foo for the input, result is echoed directly and I get >fooerror. Echoing a newline before result is echoed takes care of this:

fu! Test()
  let input = input(">")
  let result = "error\r"
  if 1
    echo "\r"
    echo result
  endif
endf

Upvotes: 1

Related Questions