< zurück

weiter >

Vim and Win

von Manuel Strehl

Abgelegt unter

Schlüsselwörter: , ,

When I switched from Notepad++ to Vim and MinTTY on my Windows XP machine at work, one thing I missed dearly was the context menu entry, that allows fast opening of files. Reading files from Thunderbird attachments or quickly peeking at a file’s content became rather complicated.

That started to bother me enough, that I wanted a solution for this itch, before I ventured on deeper into UNIX tools. Rummaging out my (never really brilliant) Windows batch scripting knowledge and a bit Bash scripting brought me to a quite easy solution.

We need two scripts for this task. The first is a batch script send_to_vim.bat, that must live in Windows’ %PATH%.

@echo off

chdir \cygwin\bin

set FILE=%1
bash --login /home/Manuel/bin/send_to_vim.sh "%FILE:\=\\%"

Cygwin is assumed to be installed in the standard place C:\cygwin. Then the script executes bash, which in turn calls a script send_to_vim.sh in my Cygwin home directory, passing it the name of the file to open.



# convert the file to an absolute UNIX path:
if type cygpath >/dev/null 2>/dev/null; then
  FILE=$(cygpath -u -a "${FILE/\\/\\\\}")
elif [[ ${FILE:0:1} != / ]]; then

# start mintty/xterm and screen, if necessary
if [[ ! $(ps -A | grep "screen") ]]; then
  if type mintty >/dev/null 2>&1; then
    mintty -w max -t Terminal /bin/bash --login -c screen &
    xterm $HOME/bin/bash_screen &

# Let screen stuff the command into Vim
ESC=$(echo -n -e '\e')
CR=$(echo -n -e '\r')
screen -p vim -X stuff "$ESC:tabnew ${FILE/ /\\ }$CR"
screen -X select vim

The Bash script checks first for the existance of cygpath, a small Cygwin utility that converts between Windows and Cygwin path names.

It then checks, if screen is running. I always run Vim in one screen window, that is named vim.

This allows in the third part to uniquely identify the Vim instance by selecting the appropriate screen window with -p vim. Then, screen stuffs the ex command :tabnew into Vim and lets it open the file in question (with a leading ESC, if I forgot to leave insert mode).

Finally the screen window is selected, so that switching to MinTTY/Xterm directly displays the file.

The nice thing is, that I use the Bash script without modification under Ubuntu, and it works perfectly there, too.


The most obvious alternative is Gvim, the graphical Vim interface. However, the natively compiled Gvim doesn’t know about Cygwin, and for the Cygwin Gvim I’d need a running X session, which I usually avoid. Apart from that I like the way Vim integrates in my terminal tools and wouldn’t like to change it.

The script could be changed to more reliably address Vim, if Vim would run with +clientserver compiled. Then it could just utter the editor command with an additional --remote, and Vim would try to sort out, if an existing intance of it is responsible for taking over the editing. But this feature is

  1. not compiled into Cygwin’s default Vim, and
  2. you would still need an X server running.

After all it seems to me, that my solution is both simple and meets exactly the way I work with Vim.