Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Version 9

gw 2/12/13 Prefer to use cmd-line vim to list remote servers. If it does have to use tasklist, handle case where sessionname is not populated (by failing more nicely)
  • Loading branch information...
commit 39919266d6c8ccfe6aa27ba15be7fad3cd1433b8 1 parent 74f75a4
Geoff Wood authored committed
Showing with 34 additions and 14 deletions.
  1. +3 −1 README
  2. +7 −3 doc/gvimt_README.txt
  3. +24 −10 gvimt.bat
View
4 README
@@ -4,7 +4,9 @@ gvimt.bat is a script for Windows to open new files in gvim in new tabs, splits
Call it from SendTo menu, from Context menu entries or from the command line.
-It uses the tasklist command (which exists from Windows XP, but not in Windows 2000) when checking if gvim is already running. On Windows 2000 you'll need to have an instance of gvim running already.
+It ignores command line vim, and gvims where the --servername parameter has changed the name to not include "GVIM"
+
+It prefers to use vim.exe (command line vim, not gvim) to check if gvim is already running. If that's not available it will use the tasklist command (which exists from Windows XP, but not in Windows 2000). On Windows 2000 you'll need to have an instance of gvim running already. Tasklist is not always reliable when using several remote desktop sessions on a machine (because for some reason %SESSIONNAME% parameter is not always populated in the batch file's environment).
It creates gvimt.tmp while it is running, if it crashes or goes wrong for some reason, delete this file.
View
10 doc/gvimt_README.txt
@@ -6,12 +6,15 @@ gw 9/10/12 - Goes to normal mode first to make sure commands execute and don't j
gw 9/5/13 adds tv and ts modes to open groups on new tab
gw 10/5/13 added tabsplit_wait_s to fix problem where opening many files would end up on more than one tab
gw 17/6/13 only look at tasks for this user session when checking if vim is already running
+gw 2/12/13 Prefer to use cmd-line vim to list remote servers. If it does have to use tasklist, handle case where sessionname is not populated
gvimt.bat is a script for Windows to open new files in gvim in new tabs, splits or vertical splits. It starts gvim if it is not running already.
+It ignores command line vim, and gvims where the --servername parameter has changed the name to not include "GVIM"
+
Call it from SendTo menu, from Context menu entries or from the command line.
-It uses the tasklist command (which exists from Windows XP, but not in Windows 2000) when checking if gvim is already running. On Windows 2000 you'll need to have an instance of gvim running already.
+It prefers to use vim.exe (command line vim, not gvim) to check if gvim is already running. If that's not available it will use the tasklist command (which exists from Windows XP, but not in Windows 2000). On Windows 2000 you'll need to have an instance of gvim running already. Tasklist is not always reliable when using several remote desktop sessions on a machine (because for some reason %SESSIONNAME% parameter is not always populated in the batch file's environment).
It creates gvimt.tmp while it is running, if it crashes or goes wrong for some reason, delete this file.
@@ -19,15 +22,16 @@ Install instructions:
=====================
Create directory C:\Batch Files\ and extract the package there. (If you want another directory, change "set batch_path=" at the start of gvimt.bat, and modify gvim.reg accordingly.)
-If you do not have the tasklist command (e.g. using Windows 2000), edit the batch file and change "set already_ran" to "=true" at the start of gvimt.bat.
+If you do not have vim.exe, and do not have the tasklist command (e.g. using Windows 2000), edit the batch file and change "set already_ran" to "=true" at the start of gvimt.bat.
If your gvim typically takes longer than a second to start up, change "set vim_startup_time_ms" to be more milliseconds.
If you find tv and ts commands open files on more than one tab, change "set tabsplit_wait_s" to be more seconds.
-If gvim.exe is not in your path, edit gvimt.bat and change "set vim_path=" at the start. Paths should end with \ and be quoted, e.g.
+If gvim.exe or vim.exe are not in your path, edit gvimt.bat and change "set gvim_path=" and "set vim_path=" at the start. Paths should end with \ and be quoted, e.g.
set vim_path="c:\program files\vim\vim73\"
+ set gvim_path="c:\program files\vim\vim73\"
Optionally run the gvimt.reg file to create Context Menu entries.
View
34 gvimt.bat
@@ -15,12 +15,18 @@ REM gw 8/10/12 get into normal mode first, fixes problem where command appears i
REM gw 9/5/13 adds tv and ts modes to open groups on new tab
REM gw 10/5/13 added tabsplit_wait_s to fix problem where opening many files would end up on more than one tab
REM gw 17/6/13 only look at tasks for this user session when checking if vim is already running
+REM gw 2/12/13 Prefer to use cmd-line vim to list remote servers
+REM If it does have to use tasklist, handle case where sessionname is not populated
+REM (crudely, by quoting the parameter so findstr doesn't just bomb out. Will still fail if
+REM there are two sessions and this is the wrong one. But the error will be more noticable.)
setlocal ENABLEDELAYEDEXPANSION
REM If gvim.exe is not in your path you can add the path here,
REM make sure it ends in a \ character and is quoted if necessary
-REM e.g. set vim_path="c:\program files\vim\vim73\"
+REM Set the path to command-line vim as well
+REM e.g. set gvim_path="c:\program files\vim\vim73\"
+set gvim_path=
set vim_path=
set batch_path="c:\batch files"
@@ -71,9 +77,17 @@ REM If another instance ran, it made sure
REM gvim was running
REM Otherwise do it now
if %already_ran%==false (
- tasklist | findstr %SESSIONNAME% | findstr gvim.exe > nul
+
+ REM tasklist sometimes fails in odd circumstances, sessionname is not populated
+ REM So prefer to use vim to check instead. This only shows vims local to this session
+ %vim_path%vim --version > nul
+ if !errorlevel!==0 (
+ %vim_path%vim --serverlist | findstr GVIM
+ ) else (
+ tasklist | findstr "%SESSIONNAME%.*" | findstr gvim.exe > nul
+ )
if not !errorlevel!==0 (
- start %vim_path%gvim.exe -c "let g:gvimt_time_this=localtime()" %1
+ start %gvim_path%gvim.exe -c "let g:gvimt_time_this=localtime()" %1
set already_ran=true
REM sometimes we miss the first file out possibly if vim hasn't finished starting?
@@ -88,13 +102,13 @@ if %already_ran%==false (
REM if %1_==_ goto raise_to_foreground
if %1_==_ goto end
-if /i %task% EQU t start %vim_path%gvim.exe --remote-send "<Esc>:tablast | tabe %~f1<CR>:call foreground()<CR><CR>"
-if /i %task% EQU v start %vim_path%gvim.exe --remote-send "<Esc>:vsplit %~f1<CR>:call foreground()<CR><CR>"
-if /i %task% EQU s start %vim_path%gvim.exe --remote-send "<Esc>:split %~f1<CR>:call foreground()<CR><CR>"
+if /i %task% EQU t start %gvim_path%gvim.exe --remote-send "<Esc>:tablast | tabe %~f1<CR>:call foreground()<CR><CR>"
+if /i %task% EQU v start %gvim_path%gvim.exe --remote-send "<Esc>:vsplit %~f1<CR>:call foreground()<CR><CR>"
+if /i %task% EQU s start %gvim_path%gvim.exe --remote-send "<Esc>:split %~f1<CR>:call foreground()<CR><CR>"
-if /i %task% EQU tv start %vim_path% gvim.exe --remote-send "<Esc>:if exists("""g:gvimt_time_this""") | let g:gvimt_time_last=g:gvimt_time_this | else | let g:gvimt_time_last=0 | endif | let g:gvimt_time_this=localtime() | if g:gvimt_time_this > g:gvimt_time_last + %tabsplit_wait_s% | tablast | tabe %~f1 | else | vsplit %~f1 | endif | call foreground()<CR><CR>"
+if /i %task% EQU tv start %gvim_path% gvim.exe --remote-send "<Esc>:if exists("""g:gvimt_time_this""") | let g:gvimt_time_last=g:gvimt_time_this | else | let g:gvimt_time_last=0 | endif | let g:gvimt_time_this=localtime() | if g:gvimt_time_this > g:gvimt_time_last + %tabsplit_wait_s% | tablast | tabe %~f1 | else | vsplit %~f1 | endif | call foreground()<CR><CR>"
-if /i %task% EQU ts start %vim_path% gvim.exe --remote-send "<Esc>:if exists("""g:gvimt_time_this""") | let g:gvimt_time_last=g:gvimt_time_this | else | let g:gvimt_time_last=0 | endif | let g:gvimt_time_this=localtime() | if g:gvimt_time_this > g:gvimt_time_last + %tabsplit_wait_s% | tablast | tabe %~f1 | else | split %~f1 | endif | call foreground()<CR><CR>"
+if /i %task% EQU ts start %gvim_path% gvim.exe --remote-send "<Esc>:if exists("""g:gvimt_time_this""") | let g:gvimt_time_last=g:gvimt_time_this | else | let g:gvimt_time_last=0 | endif | let g:gvimt_time_this=localtime() | if g:gvimt_time_this > g:gvimt_time_last + %tabsplit_wait_s% | tablast | tabe %~f1 | else | split %~f1 | endif | call foreground()<CR><CR>"
shift
@@ -108,13 +122,13 @@ REM REM have to go through all this to find out what the remote server is called
REM REM call remote_foreground. It still sometimes doesn't work (if called from Windows
REM REM Explorer) but at least the taskbar flashes now.
REM if %already_ran%==false (
-REM %vim_path%gvim.exe --remote-send ":let gvimt_server = [v:servername]<CR>:call writefile(gvimt_server,'%short_batch_path%\gvimt_server.tmp')<CR><CR>"
+REM %gvim_path%gvim.exe --remote-send ":let gvimt_server = [v:servername]<CR>:call writefile(gvimt_server,'%short_batch_path%\gvimt_server.tmp')<CR><CR>"
REM set /p gvimt_server=< %short_batch_path%\gvimt_server.tmp
REM set gvimt_server='!gvimt_server!'
REM ) else (
REM set gvimt_server='GVIM'
REM )
-REM start %vim_path%gvim.exe -c "call remote_foreground(%gvimt_server%)" -c "q"
+REM start %gvim_path%gvim.exe -c "call remote_foreground(%gvimt_server%)" -c "q"
REM del %short_batch_path%\gvimt_server.tmp
:end
Please sign in to comment.
Something went wrong with that request. Please try again.