Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have this piece of code in my .vimrc in order to make files of different types be opened in different gVim instances.

function GetSvrName(ext)
    if !exists('s:ext2srv')
        let s:ext2srv = [
                    \['PHP', ['php', 'phtml']],
                    \['JS', ['js']],
                    \['CSS', ['css']],
                    \['HTML', ['htm', 'html', 'dwt', 'lbi']],
                    \['PY', ['py']],
                    \['TXT', ['txt', 'text', 'md', 'mkd']],
                    \['C', ['c', 'cpp', 'h', 'hpp']],
                    \['LOG', ['log']],
                    \['INI', ['ini', 'conf']],
                    \['BAT', ['bat', 'sh']],
                    \]
    endif
    for srv in s:ext2srv 
        for extname in srv[1]
            if a:ext == extname
                return srv[0]
                " break
            endif
        endfor
    endfor
    return 'GVIM'
endfunction

function VimDispatch()
    if &diff || !argc()
        return 0
    endif
    let l:argv = argv()
    for a in l:argv
        let l:sp = expand('/')
        let l:lastsp = strridx(a, l:sp)
        let l:file = strpart(a, l:lastsp + 1)
        let l:lastdot = strridx(l:file, '.')
        let l:ext = strpart(l:file, l:lastdot + 1)
        let l:srvname = GetSvrName(l:ext)
        exe 'silent !start gvim --servername ' . l:srvname . ' --remote-tab-silent "' . a . '"'
        call remote_foreground(l:srvname)
    endfor
    exit
endfunction

call VimDispatch()

But it has some issues. One is that some files can't be opened anymore. For example, when I open a file whose name contains the string ~!, a message box appears:

Error detected while processing function VimDispatch:
line   12:
E34: No previous command

Or, if the filename contains @#:

Error detected while processing function VimDispatch:
line   12:
E194: No alternate file name to substitute for '#': silent !start gvim --servername TXT --remote-tab-silent 
"D:\@#.txt"

And the file is not opened.

So how do I resolve this issue so that my code will work with these filenames?

share|improve this question
    
Interesting approach, to segregate to different Vim instances by filetype. Haven't seen that before! – Ingo Karkat Oct 9 '13 at 9:43
up vote 3 down vote accepted

That's caused by your simplistic roll-your-own quoting. You're looking for the shellescape() command; for :!, you need to pass 1 for the additional {special} argument, like this:

exe 'silent !start gvim --servername ' . l:srvname . ' --remote-tab-silent ' . shellescape(a, 1)
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .