Let's see. I have a gvim
running and I want open a file, respecting the autocmd
s (which rules out --remote-tab
).
Now I know I can do (basically, with some tweak):
gvim --remote-send ":tabe my_file<CR>"
which works. But if a file has spaces or strange chars in it, I have to do the following:
gvim --remote-send ":tabe my\\ file<CR>"
(the double \\
is because one of them is eaten by the shell; this is equivalent to manually type
`:tabe my\ file`
in vim
and it works). Now, I can find a way to create that string in the shell or whatever, but I hoped I could "globally quote" the string in the ":tabe" command, like
gvim --remote-send ":tabe 'my file'<CR>"
or
gvim --remote-send ":tabe \"my file\"<CR>"
--- this is equivalent to writing directly in the vim command line :tabe "my file"
; it seems it is not working. I can explicitly quote all space in the string with the shell, doing something like
# <ESC> because the gvim instance can be in a mode different from normal
# the double CR: do not ask.
# the argument MUST be a full path
file="$(readlink -f "$@")"
fileq="$(echo "$file" | awk '{gsub(/ /,"\\\ ")}1')" # quote spaces FIXME add other chars
exec gvim 2>/dev/null --servername $desktop --remote-send "<ESC>:tabe $fileq <CR><CR>"
but it works just for spaces and not other special chars like tabs and "
(nor newlines, but if you have newlines in your file names you deserve it!).
The question:
Independently on the particular shell, with which I will deal after :-), is there a way when directly typing in the vim tabe:
line to globally quote a filename without going to quote the "strange" chars one by one?
gvim --remote-send ':tabe foo\ bar.txt<CR>'
worked for me on bash and zsh. And the quotes seem to matter too. If I use"
internally, it didn't work, but'
did:gvim --remote-send ":tabe 'foo bar.txt'<CR>"
gvim --remote-send ":tabe 'f s.txt'<CR>"
didn't work for me, nor writing:tabe 'f s.txt'
in vim, I gotE77: Too many files names
.gvim --servername $desktop --remote-send "<ESC>:tabe ${file// /\\ }<CR>"
be simpler?shellescape
function be helpful?:edit
(and its variants) doesn't accept a quoted filename. All special characters need to be individually escaped. So,:edit "foo bar.txt"
won't work; you need:edit foo\ bar.txt
. That said, something like:execute 'tabedit' escape('$file', ' ')
might be on the right track.