mhinz / neovim-remote

:ok_hand: Support for --remote and friends.
MIT License
1.75k stars 83 forks source link

Add -n option to prevent the output of a newline #121

Closed cjlarose closed 5 years ago

cjlarose commented 5 years ago

nvr --remote-expr "$expr" will always add a trailing newline character when printing the output it got from the nvim server. This is usually fine, but there are use cases where this behavior is really inconvenient.

For example, if I wanted a program that read the contents of my " register and print it out to stdout, it's tempting to write

#!/bin/bash
nvr --remote-expr 'getreg()'

but this doesn't quite work since if I yank a single word (yw), and try to print it with this program, the program writes to stdout a newline character at the end. If you're using bash or a another POSIX-compatible shell, you might try to work around this problem using command substitution like so:

#!/bin/bash
content=$(nvr --remote-expr 'getreg()')
echo -n "$content"

This program solves the single-word yank case, but fails in a different case. The problem here is that command substitution will always remove all trailing newline characters produced by the command. So, if I yank a whole line (yy), this new program actually removes newline characters that are significant.

It's possible to write a program that correctly gets the entire output from nvr, then removes the final (and only the final) newline character, but it is cumbersome:

#!/bin/bash
IFS= read -r -d '' content < <(nvr --remote-expr 'getreg()')
echo -n "${content%?}"

What I think would be convenient is the ability to get the unmodified output from the call to eval, without anything added. This PR adds the -n option, which tries to accomplish this. -n here mirrors the behavior of the builtin echo, since echo -n will not print a trailing newline.

With this change,

#!/bin/bash
nvr --remote-expr 'getreg()'

does exactly what it did before (print a newline at the end), but

#!/bin/bash
nvr -n --remote-expr 'getreg()'

instead reproduces the output verbatim from the call to eval.

mhinz commented 5 years ago

The biggest goal is compatibility to Vim. And the result of Vim actually never ends without a \n. So, no matter whether the register is set to either foo or foo\n, --remote-expr will print the same. There's no way to distinguish it and you wouldn't know when to use -n.

And adding options come with a cost and in this case it solves a very special use case. It seems you want to use it often anyway, so please go on using an alias or wrapper script (taking the above gotcha into account).

Thanks anyway!