Custom Launcher to Fire Vim

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/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:9
fullpath=$1

# split filepath to filename and line number
arr=(`echo $fullpath | sed -e 's/^vim:\/\/\(.*\):\(.*\)$/\1 \2/'`)

# determine stage from filename
stage=$( 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env bash

set -e

echo "Get vimp executable path"

# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
base=$(dirname "$SCRIPT")
vimp_path="$base/vimp"

echo "Generate vimp.desktop and write to /usr/share/applications"

echo "
[Desktop Entry]
Encoding=UTF-8
Name=GVIM
Comment=Edit text files in GVIM session
Exec=$vimp_path %U
Terminal=false
Type=Application
Icon=/usr/src/vim/runtime/vim48x48.xpm
Categories=Application;Utility;TextEditor;
MimeType=text/plain;x-scheme-handler/vim;
StartupNotify=true
StartupWMClass=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

1
2
3
#...skipped from initializers/rails_footnotes.rb
f.prefix = 'vim://%s:%d'
#...skipped

Comments