3

Let's say i create a filename with this:

xb@dnxb:/tmp/test$ touch '"i'"'"'m noob.mp4"'
xb@dnxb:/tmp/test$ ls -1
"i'm noob.mp4"
xb@dnxb:/tmp/test$ 

Then vim . to go inside Netrw directory listing.

" ============================================================================
" Netrw Directory Listing                                        (netrw v156)
"   /tmp/test
"   Sorted by      name
"   Sort sequence: [\/]$,\<core\%(\.\d\+\)\=\>,\.h$,\.c$,\.cpp$,\~\=\*$,*,\.o$,\.obj$,\.info$,\.swp$,\.bak$,\~$
"   Quick Help: <F1>:help  -:go up dir  D:delete  R:rename  s:sort-by  x:special
" ==============================================================================
../
./
"i'm noob.mp4"

Then press Enter to view the file. Type:

:!ls -l %

It will shows error:

xb@dnxb:/tmp/test$ vim .

ls: cannot access '/tmp/test/i'\''m noob.mp4': No such file or directory

shell returned 2

Press ENTER or type command to continue

I also tried:

[1] :!ls -l '%':

Press ENTER or type command to continue
/bin/bash: -c: line 0: unexpected EOF while looking for matching `"'
/bin/bash: -c: line 1: syntax error: unexpected end of file

shell returned 1

Press ENTER or type command to continue

[2] :!ls -l "%":

Press ENTER or type command to continue
/bin/bash: -c: line 0: unexpected EOF while looking for matching `''
/bin/bash: -c: line 1: syntax error: unexpected end of file

shell returned 1

Press ENTER or type command to continue

[3] :!ls -l expand("%"):

/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `ls -l expand(""i'm noob.mp4"")'

shell returned 1

Press ENTER or type command to continue

[4] !ls -l shellescape("%"):

/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `ls -l shellescape("/tmp/test/"i'm noob.mp4"")'

shell returned 1

Press ENTER or type command to continue

[5] !ls -l shellescape(expand("%")):

/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `ls -l shellescape(expand("/tmp/test/"i'm noob.mp4""))'

shell returned 1

Press ENTER or type command to continue

My ultimate goal is perform rsync by Ctrl+c, e.g:

nnoremap <C-c> :!eval `ssh-agent -s`; ssh-add; rsync -azvb --no-t % [email protected]:/home/xiaobai/storage/

My platform is Kali Linux's vim.gtk3, bash. Fedora's vim and gvim also have the same problem.

What's the correct syntax to escape filename containing single and double quotes ?

2
  • 2
    Hmmm. How about exec '!ls -l' shellescape(expand('%'))?
    – muru
    Oct 28, 2016 at 0:01
  • @muru Thanks, it did work ! But still i need to figure out why rsync syntax not working. Can you provide a key mapping answer based on rsync above as answer ? I think i might need to add single quote to destination but failed. e.g. nnoremap <C-c> :exec '!eval `ssh-agent -s`; ssh-add; rsync -azvb --no-t' shellescape(expand('%')) '[email protected]:/tmp/'
    – 林果皞
    Oct 28, 2016 at 0:37

1 Answer 1

3

I got the answer from Unix & Linux Stack Exchange, backslash-escape literally every character:

nnoremap <C-c> :!eval `ssh-agent -s`; ssh-add; rsync -azvb --no-t %:gs/./\\&/:p '[email protected]:/tmp/'

Tested with chinese unicode characters and works fine, even in virtual console.

But I still welcome any alternative answers or more detailed explanation. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.