Discussion:
Invoking Cygwin vim from Windows Explorer
carolus
2014-02-09 04:15:45 UTC
Permalink
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
Andrey Repin
2014-02-09 05:09:43 UTC
Permalink
Greetings, carolus!
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
There's like 5 ways to do it.
What you've tried already and what your results so far?


--
WBR,
Andrey Repin (***@yandex.ru) 09.02.2014, <09:07>

Sorry for my terrible english...
carolus
2014-02-09 14:01:36 UTC
Permalink
Post by Andrey Repin
Greetings, carolus!
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
There's like 5 ways to do it.
What you've tried already and what your results so far?
I don't know how to pass the filename and directory from an Explorer
left-click to a Cygwin shell script. (I'm not a programmer, just a dumb
engineer.)
Andrey Repin
2014-02-09 14:37:02 UTC
Permalink
Greetings, carolus!
Post by carolus
Post by Andrey Repin
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
There's like 5 ways to do it.
What you've tried already and what your results so far?
I don't know how to pass the filename and directory from an Explorer
left-click to a Cygwin shell script. (I'm not a programmer, just a dumb
engineer.)
So, vim or shell script?...


--
WBR,
Andrey Repin (***@yandex.ru) 09.02.2014, <18:36>

Sorry for my terrible english...
carolus
2014-02-09 17:11:43 UTC
Permalink
Post by Andrey Repin
Greetings, carolus!
Post by carolus
Post by Andrey Repin
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
There's like 5 ways to do it.
What you've tried already and what your results so far?
I don't know how to pass the filename and directory from an Explorer
left-click to a Cygwin shell script. (I'm not a programmer, just a dumb
engineer.)
So, vim or shell script?...
If there is no simple answer, let's just drop the subject. I'll
continue to open cygwin and cd to the target directory whenever I want
to open a file in vim and happen to be in Explorer.
Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-02-09 17:35:16 UTC
Permalink
Post by carolus
If there is no simple answer,
Maybe not as simple, but...

What about adding a "Vim" destination to your "Send To..." folder
under your Windows profile?

The target command can be something like:

C:\cygwin\bin\mintty.exe -e c:\cygwin\bash.exe -c C:\cygwin\bin\vim.sh

Where vim.sh is something like this:

#! /bin/sh
vim -- "`cygpath -u $1`"

This way you can right-click on the file, select "Send To", then "Vim".

HTH,

Anton Lavrentiev
Contractor NIH/NLM/NCBI
Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-02-09 17:37:31 UTC
Permalink
Correction: c:\cygwin\bash.exe should have been written as c:\cygwin\bin\bash.exe.
Sorry about that.

Anton Lavrentiev
Contractor NIH/NLM/NCBI
carolus
2014-02-09 18:49:32 UTC
Permalink
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
Post by carolus
If there is no simple answer,
Maybe not as simple, but...
What about adding a "Vim" destination to your "Send To..." folder
under your Windows profile?
Thanks, but maybe not simple enough for me. I find a SendTo shortcut
under my username, but can't open it. Right clicking does not give me
an administrator option. As you can see, I don't know much about
Windows, which is why I like Cygwin.
carolus
2014-02-10 00:13:07 UTC
Permalink
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
Post by carolus
If there is no simple answer,
Maybe not as simple, but...
What about adding a "Vim" destination to your "Send To..." folder
under your Windows profile?
Thanks, but maybe not simple enough for me. I find a SendTo shortcut
under my username, but can't open it. Right clicking does not give me an
administrator option. As you can see, I don't know much about Windows,
which is why I like Cygwin.
I discovered that I can get into that directory from the command line
even though it is forbidden from the GUI! But there are no simple files
in it that I could use as syntax examples. So what more do I have to
add to the line

C:\cygwin\bin\mintty.exe -e c:\cygwin\bin\bash.exe -c C:\cygwin\bin\vim.sh

to make sendto work?
Andrey Repin
2014-02-09 20:07:37 UTC
Permalink
Greetings, carolus!
Post by carolus
Post by Andrey Repin
Post by carolus
Post by Andrey Repin
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
There's like 5 ways to do it.
What you've tried already and what your results so far?
I don't know how to pass the filename and directory from an Explorer
left-click to a Cygwin shell script. (I'm not a programmer, just a dumb
engineer.)
So, vim or shell script?...
If there is no simple answer, let's just drop the subject. I'll
continue to open cygwin and cd to the target directory whenever I want
to open a file in vim and happen to be in Explorer.
As I said, there's like five different answers. Depends on what you actually
want to do.
I.e. just make an association to open a file with vi(m). In a usual Windows way.

But since you're changing subject back and forth, it's hard to help you.


--
WBR,
Andrey Repin (***@yandex.ru) 10.02.2014, <00:04>

Sorry for my terrible english...
carolus
2014-02-09 21:29:49 UTC
Permalink
Post by Andrey Repin
Greetings, carolus!
Post by carolus
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
If there is no simple answer, let's just drop the subject. I'll
continue to open cygwin and cd to the target directory whenever I want
to open a file in vim and happen to be in Explorer.
As I said, there's like five different answers. Depends on what you actually
want to do.
To clarify: In Cygwin folders I routinely use vim for all text
files, and in Windows folders I routinely use Wordpad for only those
text files with extension .txt. For files in a Windows folder that have
extension .f or .sh or no extension at all, I would like to open those
with vim in a Cygwin terminal. Currently I'm doing this by going into
cygwin and manually changing directories, but there must be a better way.

The suggestion by Anton Lavrentiev appears to accomplish this, but
without enough detail for me to implement given my limited understanding
of Windows.
Post by Andrey Repin
I.e. just make an association to open a file with vi(m). In a usual Windows way.
The only "usual Windows way" I know is for windows executables. I want
to use the Cygwin console application, which I am used to, and not to
install the native Windows gvim. Googling shows lots of discussion of
how to use the windows native executable with Cygwin, but not the reverse.
Post by Andrey Repin
But since you're changing subject back and forth, it's hard to help you.
Well, I can see one change: originally I failed to state that I wanted
the selection of vim to be optional. Lavrentiev's proposal, which would
do exactly that, reminded me.
Andrey Repin
2014-02-09 22:16:57 UTC
Permalink
Greetings, carolus!
Post by carolus
Post by Andrey Repin
Post by carolus
Post by carolus
Is there some configuration that will let me open a text file in Cygwin
vim by clicking on the file in an Explorer window?
If there is no simple answer, let's just drop the subject. I'll
continue to open cygwin and cd to the target directory whenever I want
to open a file in vim and happen to be in Explorer.
As I said, there's like five different answers. Depends on what you actually
want to do.
To clarify: In Cygwin folders I routinely use vim for all text
files, and in Windows folders I routinely use Wordpad for only those
text files with extension .txt. For files in a Windows folder that have
extension .f or .sh or no extension at all, I would like to open those
with vim in a Cygwin terminal. Currently I'm doing this by going into
cygwin and manually changing directories, but there must be a better way.
The suggestion by Anton Lavrentiev appears to accomplish this, but
without enough detail for me to implement given my limited understanding
of Windows.
Just create a shell link in your personal "Send To..." folder with specified
command. On Windows XP, it is in "%USERPROFILE%/SendTo". Dunno about other,
This way, even though a bit convoluted, allow you to edit ANY file with your
chosen program. Regardless of extension, and even in absence of it, as it is
the case for many traditional shell scripts.
Post by carolus
Post by Andrey Repin
I.e. just make an association to open a file with vi(m). In a usual Windows way.
The only "usual Windows way" I know is for windows executables.
Cygwin applications are (surprize!) windows executables.
And, as I just checked, the vim transparently understands native paths.
Post by carolus
I want to use the Cygwin console application, which I am used to, and not to
install the native Windows gvim. Googling shows lots of discussion of
how to use the windows native executable with Cygwin, but not the reverse.
No comments...
Post by carolus
Post by Andrey Repin
But since you're changing subject back and forth, it's hard to help you.
Well, I can see one change: originally I failed to state that I wanted
the selection of vim to be optional. Lavrentiev's proposal, which would
do exactly that, reminded me.
Anton's... Comma reverses the order.


--
WBR,
Andrey Repin (***@yandex.ru) 10.02.2014, <02:10>

Sorry for my terrible english...
carolus
2014-02-09 23:27:36 UTC
Permalink
Post by Andrey Repin
Just create a shell link in your personal "Send To..." folder with specified
command. On Windows XP, it is in "%USERPROFILE%/SendTo". Dunno about other,
This way, even though a bit convoluted, allow you to edit ANY file with your
chosen program. Regardless of extension, and even in absence of it, as it is
the case for many traditional shell scripts.
On Windows 7, %USERPROFILE$ points to my user folder. But I get an
"access forbidden" message if I try to open the SendTo subdirectory from
Explorer. There is no option to right-click and elevate, and I get the
same "access forbidden" message even if I switch to an administrator
account. Strangely, I can open that directory from the command line, but
the only way I know how to create a Windows link is by right-click
drag&drop from the GUI.
Post by Andrey Repin
Post by carolus
Post by Andrey Repin
I.e. just make an association to open a file with vi(m). In a usual Windows way.
The only "usual Windows way" I know is for windows executables.
Cygwin applications are (surprize!) windows executables
But not "normal" executables in the sense that they run when you click
on them from Explorer. Somehow you have to get into cmd.exe first. And
then you need to get the path and filename into vim. This is getting
too complicated for a nonprogrammer like myself.

Probably the subject is best dropped. It is more a Windows problem than
a Cygwin one, and I have been getting by with the clumsy method for the
last ten years. I just thought there might be an easy fix. Thanks for
the help.
Lee
2014-02-09 23:54:57 UTC
Permalink
Post by carolus
Post by Andrey Repin
Just create a shell link in your personal "Send To..." folder with specified
command. On Windows XP, it is in "%USERPROFILE%/SendTo". Dunno about other,
This way, even though a bit convoluted, allow you to edit ANY file with your
chosen program. Regardless of extension, and even in absence of it, as it is
the case for many traditional shell scripts.
On Windows 7, %USERPROFILE$ points to my user folder. But I get an
"access forbidden" message if I try to open the SendTo subdirectory from
Explorer. There is no option to right-click and elevate, and I get the
same "access forbidden" message even if I switch to an administrator
account. Strangely, I can open that directory from the command line, but
the only way I know how to create a Windows link is by right-click
drag&drop from the GUI.
Post by Andrey Repin
Post by carolus
Post by Andrey Repin
I.e. just make an association to open a file with vi(m). In a usual
Windows way.
The only "usual Windows way" I know is for windows executables.
Cygwin applications are (surprize!) windows executables
But not "normal" executables in the sense that they run when you click
on them from Explorer. Somehow you have to get into cmd.exe first. And
then you need to get the path and filename into vim. This is getting
too complicated for a nonprogrammer like myself.
I'm surprised this worked, but
right click on a .txt file in explorer, select open with, choose program
click on "browse", navigate to c:\cygwin\bin (or wherever you
installed cygwin), double-click on vi.exe
Post by carolus
Probably the subject is best dropped. It is more a Windows problem than
a Cygwin one, and I have been getting by with the clumsy method for the
last ten years. I just thought there might be an easy fix.
have you tried http://www.vim.org/download.php
installs a windows version of vi that you can put in your path & no
nasty cygwin/windows permissions or line endings issues.

Regards,
Lee
Balaji Venkataraman
2014-02-10 01:09:45 UTC
Permalink
Post by Lee
I'm surprised this worked, but
right click on a .txt file in explorer, select open with, choose program
click on "browse", navigate to c:\cygwin\bin (or wherever you
installed cygwin), double-click on vi.exe
This solution works (I'm surprised too) - and seems to be the simplest
of all suggestions so far. Thanks.
carolus
2014-02-10 05:42:45 UTC
Permalink
Post by Lee
I'm surprised this worked, but
right click on a .txt file in explorer, select open with, choose program
click on "browse", navigate to c:\cygwin\bin (or wherever you
installed cygwin), double-click on vi.exe
Hot dog! It really works. (But on my system it is vim-nox.exe, not vi.exe)

This solves the problem, but it would still be nice to get the "SendTo"
method working to provide an option for when the default association is
not appropriate.
Post by Lee
have you tried http://www.vim.org/download.php
installs a windows version of vi that you can put in your path& no
nasty cygwin/windows permissions or line endings issues.
Some lines in my configuration file go back 20 years and I'd rather not
monkey with my habitual environment. (I still use .exrc, from old vi
days, rather than .vimrc) I also prefer to use a vim version that is
ubiquitous on linux and even present on my wife's Mac.
Andrey Repin
2014-02-10 01:49:16 UTC
Permalink
Greetings, carolus!
Post by carolus
Post by Andrey Repin
Just create a shell link in your personal "Send To..." folder with specified
command. On Windows XP, it is in "%USERPROFILE%/SendTo". Dunno about other,
This way, even though a bit convoluted, allow you to edit ANY file with your
chosen program. Regardless of extension, and even in absence of it, as it is
the case for many traditional shell scripts.
On Windows 7, %USERPROFILE$ points to my user folder. But I get an
"access forbidden" message if I try to open the SendTo subdirectory from
Explorer.
It's likely a symlink... Let me find my netbook.

It's in "%AppData%/Microsoft/Windows/SendTo"
Post by carolus
There is no option to right-click and elevate, and I get the
same "access forbidden" message even if I switch to an administrator
account. Strangely, I can open that directory from the command line, but
the only way I know how to create a Windows link is by right-click
drag&drop from the GUI.
Yeah. Just make a copy of default Cygwin shortcut in SendTo, and edit it's
command line to suit your needs.
Post by carolus
Post by Andrey Repin
Post by carolus
Post by Andrey Repin
I.e. just make an association to open a file with vi(m). In a usual Windows way.
The only "usual Windows way" I know is for windows executables.
Cygwin applications are (surprize!) windows executables
But not "normal" executables in the sense that they run when you click
on them from Explorer.
There's no more or less "normal" windows executables.
Post by carolus
Somehow you have to get into cmd.exe first.
No.
Post by carolus
And then you need to get the path and filename into vim.
As I said, you can just drop a file you want to edit over vim.exe and it'll
open it. In regular console, that's right, but it works.
If you want mintty instead of regular console, you gotta work for it a little
harder.
Post by carolus
This is getting too complicated for a nonprogrammer like myself.
Probably the subject is best dropped. It is more a Windows problem than
a Cygwin one, and I have been getting by with the clumsy method for the
last ten years. I just thought there might be an easy fix. Thanks for
the help.
One day, you ought to learn something to make your life easier. Why not start
today?


--
WBR,
Andrey Repin (***@yandex.ru) 10.02.2014, <05:38>

Sorry for my terrible english...
carolus
2014-02-10 04:44:42 UTC
Permalink
Post by Andrey Repin
Greetings, carolus!
Post by carolus
Post by Andrey Repin
Just create a shell link in your personal "Send To..." folder with specified
command. On Windows XP, it is in "%USERPROFILE%/SendTo".
On Windows 7, %USERPROFILE$ points to my user folder. But I get an
"access forbidden" message if I try to open the SendTo subdirectory from
Explorer.
It's likely a symlink... Let me find my netbook.
It's in "%AppData%/Microsoft/Windows/SendTo"
I've finally tracked it down on my Windows 7 computer to

/cygdrive/c/Users/cdr/AppData/Roaming/Microsoft/Windows/SendTo


.> Just make a copy of default Cygwin shortcut in SendTo, and edit it's
Post by Andrey Repin
command line to suit your needs.
Did that. In the shortcut properties, I followed Anton (with slight
modification, since I routinely put my own stuff in /usr/local) to enter
in the "target" field

C:\cygwin\bin\mintty.exe -e c:\cygwin\bin\bash.exe -c
C:\cygwin\usr\local\bin\vim.sh

with the vim.sh that he suggests.

Right-click and "Send To" now briefly flashes a Cygwin console but then
immediately closes it.
carolus
2014-02-10 05:06:49 UTC
Permalink
Post by carolus
Right-click and "Send To" now briefly flashes a Cygwin console but then
immediately closes it.
More exactly, it requests an administrative password to run mintty, and
only after supplying the password does the Cygwin window flash on the
screen.

I had forgotten chmod +x for vim.sh, but fixing that did not change
anything.
Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-02-10 16:14:43 UTC
Permalink
My suggestion was not an exact recipe, it was just a scheme.

Here the exact step-by-step procedure. I've checked it works on my Windows 7 box.

From the Windows "Start" menu, in the Search box at the bottom
(formerly called "Run") enter (substitute <YourUserName> with whatever
ID you are going by on that Windows system):

C:\Users\<YourUserName>\AppData\Roaming\Microsoft\Windows\SendTo

In the opened Explorer window, right click on unoccupied space on the right hand
pane, select "New"->"Shortcut".

As prompted for "Location", enter (I assume your Cygwin installation is
at C:\cygwin):

C:\cygwin\bin\mintty.exe -e C:\cygwin\bin\bash.exe -l /bin/vim.sh

click "Next", when prompted for the name, enter "Vim",
close the dialog with "Finish".
cd /bin
cat <<EOF
#! /bin/sh
exec vim -- "`cygpath -u $1`"
EOF
chmod a+x vim.sh
Now you can check whether it work by going to C:\cygwin\ in Windows
Explorer, right-clicking Cygwin.bat, and using "Send To" with "Vim".

HTH,

Anton Lavrentiev
Contractor NIH/NLM/NCBI
carolus
2014-02-10 17:34:46 UTC
Permalink
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
My suggestion was not an exact recipe, it was just a scheme.
Here the exact step-by-step procedure. I've checked it works on my Windows 7 box.
From the Windows "Start" menu, in the Search box at the bottom
(formerly called "Run") enter (substitute<YourUserName> with whatever
C:\Users\<YourUserName>\AppData\Roaming\Microsoft\Windows\SendTo
In the opened Explorer window, right click on unoccupied space on the right hand
pane, select "New"->"Shortcut".
As prompted for "Location", enter (I assume your Cygwin installation is
C:\cygwin\bin\mintty.exe -e C:\cygwin\bin\bash.exe -l /bin/vim.sh
click "Next", when prompted for the name, enter "Vim",
close the dialog with "Finish".
cd /bin
cat<<EOF
#! /bin/sh
exec vim -- "`cygpath -u $1`"
EOF
chmod a+x vim.sh
Now you can check whether it work by going to C:\cygwin\ in Windows
Explorer, right-clicking Cygwin.bat, and using "Send To" with "Vim".
HTH,
Anton Lavrentiev
Contractor NIH/NLM/NCBI
Now working correctly except that it requires an administrative password
to run mintty. The message looks like the one you get when installing
new software.

I think permissions on vim.sh are OK:
-rwxr-xr-x 1 cdr None 36 Feb 9 22:22 vim.sh*
Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-02-10 17:41:06 UTC
Permalink
an administrative password to run minty
That's new to me. My PC at work (despite being secured with tons and tons
of Gov't restrictive policies) does not require any additional permissions
to run a command incorporated in the shortcut file...

Anton Lavrentiev
Contractor NIH/NLM/NCBI
carolus
2014-02-10 18:02:54 UTC
Permalink
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
an administrative password to run minty
That's new to me. My PC at work (despite being secured with tons and tons
of Gov't restrictive policies) does not require any additional permissions
to run a command incorporated in the shortcut file...
On this computer the default is the old cygwin console, and I find now
that any attempt to run mintty requires an administrative password. So
that is a different problem. Thanks for solving the original problem.
Andrey Repin
2014-02-11 02:41:40 UTC
Permalink
Greetings, Lavrentiev, Anton (NIH/NLM/NCBI) [C]!
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
an administrative password to run minty
That's new to me. My PC at work (despite being secured with tons and tons
of Gov't restrictive policies) does not require any additional permissions
to run a command incorporated in the shortcut file...
But not new to the list.

@carolus, navigate to your Cygwin installation /bin, locate mintty.exe, bring
up it's properties and remove the "Requires admin powers" checkbox on
Compatibility tab.
Confirm your changes, and it should work fine from now on.

The confusion (and the problem) stems from the fact that the windows shortcut
have two places to raise "Needs to be admin" flag.
One in "Link - Advanced...", which sets the flag in the shortcut itself, and
the other in Compatibility, which sets the flag FOR APPLICATION, no matter
from which shortcut, or even without, you starting it.


--
WBR,
Andrey Repin (***@yandex.ru) 11.02.2014, <06:35>

Sorry for my terrible english...
carolus
2014-02-11 04:03:40 UTC
Permalink
Post by Andrey Repin
@carolus, navigate to your Cygwin installation /bin, locate mintty.exe, bring
up it's properties and remove the "Requires admin powers" checkbox on
Compatibility tab.
Confirm your changes, and it should work fine from now on.
Done, works OK now. This is not a problem on my other computer, which
has a more recent installation of Cygwin. Thanks.
Andrey Repin
2014-02-11 02:34:48 UTC
Permalink
Greetings, Lavrentiev, Anton (NIH/NLM/NCBI) [C]!
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
My suggestion was not an exact recipe, it was just a scheme.
Here the exact step-by-step procedure. I've checked it works on my Windows 7 box.
From the Windows "Start" menu, in the Search box at the bottom
(formerly called "Run") enter (substitute <YourUserName> with whatever
C:\Users\<YourUserName>\AppData\Roaming\Microsoft\Windows\SendTo
In the opened Explorer window, right click on unoccupied space on the right hand
pane, select "New"->>"Shortcut".
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
As prompted for "Location", enter (I assume your Cygwin installation is
C:\cygwin\bin\mintty.exe -e C:\cygwin\bin\bash.exe -l /bin/vim.sh
Just

C:\Programs\CygWin\bin\mintty.exe --exec /bin/vi.exe

is enough.
No need to make it more convoluted, than it really necessary.
Then everything else became unnecessary as well.
Post by Lavrentiev, Anton (NIH/NLM/NCBI) [C]
click "Next", when prompted for the name, enter "Vim",
close the dialog with "Finish".
--
WBR,
Andrey Repin (***@yandex.ru) 11.02.2014, <06:33>

Sorry for my terrible english...
carolus
2014-02-11 03:54:49 UTC
Permalink
Post by Andrey Repin
Just
C:\Programs\CygWin\bin\mintty.exe --exec /bin/vi.exe
is enough.
or in my case

C:\cygwin\bin\mintty.exe --exec /bin/vim-nox.exe

Thanks
Lavrentiev, Anton (NIH/NLM/NCBI) [C]
2014-02-10 16:18:17 UTC
Permalink
Oops, I copied it incompletely
cat <<EOF
should have been
cat >vim.sh <<EOF
Anton Lavrentiev
Contractor NIH/NLM/NCBI
Max Polk
2014-02-10 02:47:25 UTC
Permalink
Post by carolus
Is there some configuration that will let me open a text file
in Cygwin vim by clicking on the file in an Explorer window?
I wonder if anyone else needs Windows file associations for sequences of
Cygwin commands as well. If this is useful to many, maybe a nice
utility can be whipped up. I ran into this with emacs-w32.exe which is
the Cygwin emacs compiled for Win32 (not run under X).

(I require native Cygwin emacs, not the one downloaded from ftp.gnu.org,
because version control things like bzr which are Python scripts aren't
supported by Windows emacs, and because in general I want Cygwin, not
Windows filenames, and so forth.)

The first problem is that Windows file associations have to be a single
exe with no arguments. When you run that file (double click in
Explorer), Windows passes the filename to that single .exe program.

The second problem is that it might need to be started from a full login
shell (run from bash, after .bashrc and .bash_profile are loaded) to get
right env vars and paths and other things to be set. No, I don't want to
pollute Windows environment by duplicating everything I do in .bashrc
and .bash_profile. So yes, I really need to run emacs from bash run as
full login shell.

Why full login shell? When I run emacs-w32.exe, things like version
control programs need the full login shell env vars, because checking
the current directory under vc-dir doesn't run a shell, it runs "cvs" or
"svn" or "git" directly. Without the full login shell, you don't have
an env vars set. Fail.

No, I don't want to pollute my ~/.emacs with lots of (setenv "MYVAR"
"myvalue") to mirror what's in .bashrc and .bash_profile, then monkeying
with load-path and many other ugly hacks. Again, duplicating .bashrc
and .bash_profile I dislike.

So in the end, my emacs start menu link is normally this:
Target: C:\Apps\Cyg\usr\local\bin\run.exe /bin/bash --login -i -c emacs
Start in: C:\Apps\Cyg\bin

(The patch for run.exe for quoting args better is accepted but not yet
out there, so it's a locally compiled one for now. Because
/usr/local/bin/run doesn't have the cygwin1.dll it won't start when
located elsewhere. Ignore, secondary issue.)

The third problem is that the file passed to the Windows file
association program is in Windows form, not Cygwin form with mounted
filename substitutions (eg, I have /c for C:\). Many Cygwin programs if
you pass C:\X\y will see C:Xy.

So to get a .txt file association to work with emacs-w32.exe, and also
to get it to run under a full bash shell, and also to replace Windows
with Cygwin file format, we need a single .exe that does all that. I
ask, are others wanting to run random sequences of Cygwin programs as
file associations? If so, maybe a little utility is in order.

I wrote a little utility of my own, a C++ program that essentially does:
cd C:\Apps\Cyg\bin
C:\Apps\Cyg\usr\local\bin\run.exe /bin/bash --login -i -c "emacs
C:/X/My\ File.txt"

The "C:\X\My File.txt" replaced back with forward slashes, so slashed
aren't lost to the Cygwin application, and escapes space with backslash
space so it doesn't look like separate files to emacs.

After assembling where cygwin lives based on a personal env var
"CYGROOT" into cygbin ostringstream:
if (chdir (cygbin.str ().c_str ())) { ...error... }

After assembling the run argument as "runexe" ostringstream, and the
bash login emacs argument as "cmd" ostringstream, in the end it did
this, which might be useful for a little utility if someone wants to run
with this:

// Create an array with length 3 (2 plus null array terminator)
char ** newAv = (char **)malloc (sizeof (char *) * 3);

// First element of array is 'run'
newAv[0] = (char *)malloc (strlen (runexe.c_str ()) + 1);
strcpy (newAv[0], runexe.c_str ());

// Second element of array is the bash command to launch emacs with
args
newAv[1] = (char *)malloc (strlen (cmd.str ().c_str ()) + 1);
strcpy (newAv[1], cmd.str ().c_str ());

// Third element of array is null terminator
newAv[2] = 0;

// Report what we are doing in case someone is watching
printf ("%s\n", newAv[1]);

// Replace this process with 'run' process
execve (newAv[0], newAv, env);

To get emacs to parse the C:\whatever as /c/whatever (since I mounted /c
as C:\):

; When in cygwin, allow C:\whatever to turn into /c/whatever
(defun cygwin-name-hook (operation &rest args)
"Turn Windows filenames into Cygwin filenames."
;; Handle all operations the same
(let ((first (car args))
(inhibit-file-name-handlers
(cons 'cygwin-name-hook
(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(setq first (replace-regexp-in-string "^C:" "/c" first t))
(setq first (replace-regexp-in-string "\\\\" "/" first t))
(apply operation (cons first (cdr args)))))

(add-to-list 'file-name-handler-alist '("^[Cc]:" . cygwin-name-hook))

This also lets emacs be passed a Windows filename from places like
compiler output errors, and it loads the right one.

For my C++ little utility I copied the emacs.ico and created a tiny
LaunchEmacs.rc referencing it (so the "launch" file association looks
like Emacs). Finally the Makefile was:

i686-pc-mingw32-windres LaunchEmacs.rc -O coff -o LaunchEmacs.res
i686-pc-mingw32-g++ -static -mwindows -o LaunchEmacs.exe
LaunchEmacs.cc LaunchEmacs.res

As you can see, this is a lot of work, and it may be useful to have
Cygwin support file associations where some utility will go all the way
with filename conversion, launching, full login shell support, and whatnot.

Are people interested? Enough to start a little utility for this?
Jarno Malmari
2014-02-11 05:44:56 UTC
Permalink
The first problem is that Windows file associations have to be a single exe with no arguments. When you run that file (double click in Explorer), Windows passes the filename to that single .exe program.
Not exactly true. There's just no good tool for it anymore shipped
with Windows. You can do this by editing registry or use a tool
similar to what I suggested in my earlier post
<http://cygwin.com/ml/cygwin/2014-02/msg00171.html>.
For instance, you can edit the default action "Open" which opens .txt
files with cygwin nano using mintty as terminal, on double click. More
conservative approach would be to add new non-default action "Open in
nano" and do the same by picking that from the right-click menu in
explorer.

Jarno Malmari
2014-02-09 15:10:06 UTC
Permalink
Post by carolus
I don't know how to pass the filename and directory from an Explorer
left-click to a Cygwin shell script. (I'm not a programmer, just a dumb
engineer.)
I'd suggest you use http://defaultprogramseditor.com/ to either change
existing "Open" action for .txt files or to create a new one.
Start the app and go to the context menu settings for .txt files. For
the "Program path", use something similar to
C:\cygwin64\bin\mintty.exe" --exec C:\cygwin64\bin\vim "%1"

Pro tip: if you intend to use network shares by IP address, e.g.
"\\192.168.0.2\share\mytxtfile.txt", set the environment variable
CYGWIN to include "noglob". Otherwise Cygwin's file name globbing
breaks your path argument.
Loading...