Launching VIM from browser empowered us to build efficient workflow. Last time I introduced how to open a partial with Chrome Native Messaging. Another way is using launcher. rails-footnotes launching editor using launcher by default. We will take gvim as an example.
Let’s design the protocol first. In this setup, I want gvim to be launched when accessing vim:///home/loki/some/file.txt:9.
The Launcher
Setup below script and named vimp.
123456789101112131415161718192021222324
#!/usr/bin/env bash# xdg-open "vim:///home/loki/loki/loper/trunk/project.vim:9"# grab filepath from first parameter# eg. vim:///home/loki/loki/loper/trunk/project.vim:9fullpath=$1# split filepath to filename and line numberarr=(`echo$fullpath | sed -e 's/^vim:\/\/\(.*\):\(.*\)$/\1 \2/'`)# determine stage from filenamestage=$(echo${arr[0]} | sed -e "s#$HOME/##" | cut -d'/' -f1,2 )# if stage found in server list (opened with servername)if vim --serverlist | grep -qi $stage ; then# open file and that line on the remote vim launch $stage vim --servername $stage --remote-send ":tab drop ${arr[0]}<cr>${arr[1]}G"else# open new gvim session gvim ${arr[0]} +${arr[1]}fi
The Launcher Install Script
Please place file below next to the launcher script
#!/usr/bin/env bashset -e
echo"Get vimp executable path"# Absolute path to this script, e.g. /home/user/bin/foo.shSCRIPT=$(readlink -f "$0")# Absolute path this script is in, thus /home/user/binbase=$(dirname "$SCRIPT")vimp_path="$base/vimp"echo"Generate vimp.desktop and write to /usr/share/applications"echo"[Desktop Entry]Encoding=UTF-8Name=GVIMComment=Edit text files in GVIM sessionExec=$vimp_path %UTerminal=falseType=ApplicationIcon=/usr/src/vim/runtime/vim48x48.xpmCategories=Application;Utility;TextEditor;MimeType=text/plain;x-scheme-handler/vim;StartupNotify=trueStartupWMClass=VIMP" > ~/vimp.desktop
sudo mv ~/vimp.desktop /usr/share/applications/vimp.desktop
echo"Update Desktop Database"sudo update-desktop-database
echo"Done!"
After setup, you may execute ./path/to/install/script.sh to register our vim launcher.
Verify
1
xdg-open "vim:///home/loki/.vimrc:9"
Make it Work with rails-footnotes
123
#...skipped from initializers/rails_footnotes.rbf.prefix='vim://%s:%d'#...skipped