Skip to content

Instantly share code, notes, and snippets.

@borissov
Last active October 21, 2020 14:29
Show Gist options
  • Save borissov/6563d37dd639464aecfaab3c0cb599bb to your computer and use it in GitHub Desktop.
Save borissov/6563d37dd639464aecfaab3c0cb599bb to your computer and use it in GitHub Desktop.
URL protocol handler for gVim in Ubuntu 18.04 LTS (Bionic Beaver)
[Desktop Entry]
Name=rVim
GenericName=Text Editor
TryExec=gvim
Exec=gvim --remote-tab-silent %U
Terminal=false
Type=Application
Keywords=Text;editor;
Icon=gvim
Categories=Utility;TextEditor;
StartupNotify=false
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;

There was a way to register URL protocol handler for "vim:" in Ubuntu 18.04 LTS to call gVim.

You can create urls from type: "vim:local_file?line_number" for exapmple: "vim:~/.vimrc?5" will open .vimrc and focus on line 5.

First create file "/usr/share/applications/rvim.desktop".

Append in "/usr/share/applications/defaults.list":

x-scheme-handler/vim=rvim.desktop

Append in your "~/.vimrc":

function! s:gotoline()
    let file = bufname("%")
    
    if file =~ "vim:"
        let bufnumber = bufnr("%")
        let file = substitute(file, "vim:", "", "")
        let line = 0
        if file =~ "?"
            let matches = split(file, '?')
            let file = matches[0]
            let line = matches[1]
        endif
        exec "keepalt edit +" . line . " " . fnameescape(file)
        exec "bwipeout " . bufnumber
    endif

	return file
endfunction

autocmd BufNewFile * nested call s:gotoline()
autocmd BufRead * nested call s:gotoline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment