1

I would like to open a file from Cygwin into Windows' configured text viewer, even if that file type is not associated with the text viewer already (i.e. Windows doesn't consider it a text file).

For my specific case, I use Notepad++ for general text file viewing and code editing, and I have it associated with most non-executable plain text file extensions (.txt, .log, etc) but not with executable ones (.pl, .py, etc). I now want to be able to open a Perl file into it from a Cygwin bash prompt.

I know I can use cygstart to open a file with the Windows application associated with that file type. So e.g. cygstart Readme.txt will open in the Notepad++ for me based on the .txt extension. However, if I cygstart prog.pl, it tries to run it with Windows' default Perl interpreter because of the .pl extension.

I also tried cygstart -e prog.pl, but that opens in Window's default text Editor, Notepad.

So, is there a way to have Cygwin open a plain text file in Windows' configured text viewer even if the extension is not already associated with the said viewer?

Note: I'm testing this out manually in a Bash prompt, but I want to roll this into a shared script that can then open Perl files for the user to edit. Hence my concerns below around making it portable to other users. For the same reason, I'm not interested in "just use Vim!" solutions.

Some thoughts I had so far:

  1. Associate all the file types I want to be able to open like this with the same viewer, i.e. Notepad++ in my case. The downside is it requires an update to the Windows file associations every time you want to open a new file type, and requires any other users to do the same thing.
  2. Change the Windows default Text editor, i.e. the app it uses when you right-click ->Edit in an explorer window. I don't know if this can be done and it's not a terrible option if it can be done. I.e. I can communicate this to another user as a one-time config step.
  3. Hard code the desired viewer path as an alias an use that instead. E.g. right now I have in my aliases alias npp='"C:/Program Files (x86)/Notepad++/notepad++.exe"' and I can do what I want with npp prog.pl. This presupposes that everyone who is going to use this method has the same editor installed or knows how to set up aliases.
  4. Get the path to the configured Windows text file viewer (maybe from cygstart?) and then open another non-associated file type using that path.
SSilk
  • 153
  • Another option: do not use Cygwin for opening files. Text editors by themself in Windows, cygwin to execute scripts/binaries only. Yet another option: forget cygwin, use WSL. – White Owl Mar 06 '23 at 11:48
  • @WhiteOwl Thanks for the suggestions. The script I'm working on is used by a team of engineers who also use Cygwin for lots of other stuff, and this particular script is not mission critical, so I don't think I'll get much traction trying to switch everyone to WSL. – SSilk Mar 06 '23 at 12:55
  • @Peregrino69 I think you're describing option 3 from my post. It works fine for me on my machine where I know the preferred text viewer/ editor, but it's a bit clunky to port to other users. Which is why I was hoping to basically infer others team members' preferred viewers from Windows associations that cygstart uses to open text files for viewing (option 4). – SSilk Mar 06 '23 at 12:57
  • LOL yes I am :-D Sorry, blood level in my caffeine too high again... I'll remove the comment :-) You can do the same to leave comment space for Those In The Know. If you want the script to work as drop-in for anyone you probably need to make it query the registry to identify the viewer the user has associated with .txt. Practicals are unfortunately beyond me as I have currently no Win installation. And thanks for reminding me of the exact command :-D – Peregrino69 Mar 06 '23 at 13:08
  • On my Windows Explorer there is a "Edit with Notepad++" item on the left mouse click menu when selecting any file. I do not see why you need to ask Cygwin to run Notepad++, just ask Explorer – matzeri Mar 06 '23 at 21:40
  • @matzeri I'm doing this from a bash script, and as I mentioned in my post, Notepad++ was an example of a non-default (i.e. not regular Notepad.exe) Windows text editor that a user might have configured. Other users may have other editors configured instead and I'd like to just automatically launch the viewer/editor that the user has associated with plaint text files. – SSilk Mar 07 '23 at 15:17

1 Answers1

0

Assuming I understood your need, the following bash script will launch the defined EDITOR (as windows PATH) on the first argument

#!/bin/bash
if [ $# -ne 1 ]
then
    echo "launch your editor defined in this script on any file "
    echo "Usage : " $0 "filename"
    exit 1
fi

if [ -z "${EDITOR}" ] then EDITOR="C:\Program Files\Notepad++\notepad++.exe" fi echo "using EDITOR as " ${EDITOR}

CYG_EDITOR=$(echo -n $(cygpath -u $EDITOR)) echo "using CYG_EDITOR as " ${CYG_EDITOR}

"${CYG_EDITOR}" $1 &

so for example called in this way

$ ./edit-text.sh edit-text.sh
using EDITOR as  C:\Program Files\Notepad++\notepad++.exe
using CYG_EDITOR as  /cygdrive/c/Program Files/Notepad++/notepad++.exe

is calling Notepad++ on itself

You can use '-wa' if you want to handle more complex file name

$ cygpath -wa ../tmp/edit-text.sh
D:\cyg_pub\tmp\edit-text.sh
matzeri
  • 961