Open a file from Cygwin - shell

Is there an equivalent to OSX open command in cygwin. open opens a file with the default application for that type.
I want to do something like
$ magic-command file.xls
#excel opens as if file.xls would have been double-clicked
$ magic-command file.txt
#notepad opens as if file.txt would have been double-clicked
You get the idea?
Basically something like a "cygwin-double-click" command.

You can also use the cygwin utility:
cygstart <your file>
To make things OSX-like add the following to your bashrc
alias open='cygstart'
Don't forget to check out the man page for cygstart.

You can use the start command from the CMD like this:
cmd /c start <your file>

explorer <your file>
works too. What is nice is
explorer .
opens a windows explorer window in the current directory. But then
cygstart .
does the same thing and does more, but I find 'explorer' slightly easier to remember.

I am using Cygwin in Win7. I can run file on windows through ccygwin command line.
cygstart <your file>
when you run this command your file will open in windows.

Under the Windows command-line interpreter (cmd.exe) there is support for the start command. I know of somebody who implemented start in cygwin. You can find the page about it here.
You could also simply call cmd.exe (usually located in /cygdrive/c/windows/system32/cmd.exe) with the following arguments cmd /c "start yourfile.file"

If, like me, you are using putty to ssh locally on your windows machine to Cygwin as cmd.exe is a terrible console, you may want to change your sshd service to allow it to access the local desktop (this will only work on certain windows flavors) under the sshd windows service Logon properties.

Yes, there is an equivalent to Windows, try with xdg-open <your file>

Related

How to run VSCode from the command prompt

I for security reasons cannot run VSCode plainly. I have opened it in the past, but now due to specific reasons, I may only run VSCode from the command prompt. I've tried
start "path/to/file" code and start code "path/to/file"
but none work I'm on Microsoft Windows [Version 10.0.17134.407]
how may I run this by going to Windows+R then 'cmd' then start/ run?
Also it would be great if I could use this for a separate user.
I'm looking for something like:
Runas /user:user\admin /savecred "C:\Program Files (x86)\vs-code.exe"
The use of start is useless if VSCode is in the environment variables.
You can use code C:\Users\%username%\Desktop\File.c for exemple.If it doesn't work, I advise you to use a vbs script instead
You also don't need to run VSCode as an administrator unless you need to edit a file in a protected folder.
Maybe not the exact answer to the question, but...
To start Visual-Studio-Code from CMD into the current folder write:
code %cd%
The environment variable cd tell VS-Code to open it with the current folder
just open a cmd terminal and type code followed by
just open a cmd terminal and type code followed by return keyborad key.
Well shoot, as it turns out that after doing some experimentation I found out that there's a way. Do this:
Simply stick this:
runas /user:Techtiger255\admin /savecred "C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code\Code.exe"
inside of a shortcut (.lnk file)
Open your command line of choice (Powershell or Cmd) and enter the exact file path of your shortcut ex:
"C:\Users\Standard\Desktop/VSCODE.lnk" and hit go, stupidly simple really, just had to find the code.exe file path.

WSL (Ubuntu): how to open localhost in browser from bash terminal

I am trying to open http://localhost in (any) browser from WSL bash terminal.
So far I have tried:
How can I open Google Chrome from the terminal with the URL "localhost:3000"?
"Couldn't find a file descriptor referring to the console" on Ubuntu bash on Windows
How to mention C:\Program Files in batchfile
No luck in setting up BROWSER variable for xdg-open, it responds to xdg-open http://localhost with /usr/bin/xdg-open: 851: /usr/bin/xdg-open: /c/"Program: not found.
I have tried escaping with \ and ^. Using %ProgramFiles(x86)% and ofcorse "Program Files (x86)". More or less it is the same answer every time... Any ideas how to set a work flow for opening browser in WSL?
So far I've ended up with:
/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe localhost
But I am looking for more elegant solution.
You can invoke the Windows command line from Bash and use Windows file association to open URL with the default Windows browser.
To do so just type in Bash:
cmd.exe /C start http://localhost
In my case this loads localhost in Chrome, note that the full URL is necessary for Windows to decide what to do.
This is similar to what open does in MacOS, hence you may find useful to directly alias the command and use it also for other type of files:
# add this to .bash_aliases
open='cmd.exe /C start'
Now you can open URL or open file.pdf directly from WSL.
Note: since you are simply redirecting commands to cmd.exe, it needs to have access to the file your working with. As a consequence the above solution will work when you find yourself in the Windows file system, but probably will fail when you are working with files in Linux partition (i.e. in the tmp or in the bin folder). This probably has been fixed in the new version of the WSL but I have not tested it.
You are almost there. Just add an alias for the windows chrome executable
http://www.linfo.org/alias.html
alias chrome="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"
Now you can simply run chrome localhost and open chrome in any web location you desire.
To open localhost in browser from bash terminal, you need to configure wsl so that it defaults to whatever browser has been set as default in your windows 10 system.
You can do this by using some tools from wslu ("A collection of utilities for WSL").
For this purpose you need.
wslview (-u, --unregister "remove wslview as the default WSL web browser.
-r, --register "register wslview as the default WSL web browser.)
wslpath (-a "force result to absolute path format",
-u "translate from a Windows path to a WSL path (default)")
You need to register your preferred browsers like this...
For Google Chrome:
wslview -r $(wslpath -au 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')
For Microsoft Edge:
wslview -r $(wslpath -au 'C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe')
Now you can open localhost as x-www-browser localhost:8080 or www-browser localhost:8080 and x-www-browser or www-browser will default to whatever is your current windows 10 default browser provided it has been registered as described above.
Do not forget to indicate the port; localhost alone did not work for me.
To unregister any browser just change the -r flag to -u.
Have a look at wslview help: info wslview <enter> in the wsl terminal
and wslpath <enter> for help with wslpath.
Install wslu (A collection of utilities for WSL) https://github.com/wslutilities/wslu#feature and then add these two lines to your shell's RC file, e.g. .bashrc or .zshrc.
export DISPLAY=:0
export BROWSER=/usr/bin/wslview
You can set the BROWSER variable as you have done . But xdg-open won't work in WSL as the
xdg-openscripts are setup to work with unquoted environment variables ( in which case ,
the path breaks due to spaces in the pathname ).
You can use the wsl-opennpm utility to do the same for WSL .
Once you have npm installed , install wsl-open utility :
sudo npm install -g wsl-open
To open any URL with default Windows Browser :
wsl-open http://google.com
You can also set wsl-open as default program for a file type in WSL :
wsl-open -w // sets wsl-open as the Shell Browser
Then you can use the standard xdg-open for URLs as well with default windows browser :
xdg-open http://google.com
I created a script that basically forwards xdg-open to powershell -c start
Not tested much though.
sudo tee /usr/local/bin/xdg-open <<EOF
#!/bin/sh
powershell.exe -c start "'\$#'"
EOF
sudo chmod +x /usr/local/bin/xdg-open
Cheers
Oliver
Came across this article that worked for me:
https://towardsdatascience.com/running-jupyter-notebook-on-wsl-while-using-firefox-on-windows-5a47ebfae4c1
In short:
Step 1 - Generate config for Jupyter Notebook:
jupyter notebook --generate-config
Step 2 - Edit the config file using "nano" or other editor
The config fileshould be under your home directory under ".jupyter" folder:
~/.jupyter/jupyter_notebook_config.py
Step 3 - Disable launching browser by redirecting file
First comment out the line, then change True to False:
c.NotebookApp.use_redirect_file = False
Step 4 - add a line to your .bashrc file to set the BROWSER path
export BROWSER='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'
For me it was Chrome under my Windows Program File. Otherwise any linux installation under WSL doesn't have a native browser to launch, so need to set it to the Windows executable.
Step 5 - restart .bashrc
source .bashrc
That should work!
https://github.com/microsoft/WSL/issues/3632#issuecomment-690061348
export BROWSER='eval "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"'
xdg-open https://google.com # nice work
solved the spaced path problem.
it worked for me.
Now you can simple use :
sensible-browser http://www.google.com
it already comes with wsl and it opens the default browser in windows
ps: you can also use wslview . to open the file explorer from the bash terminal
i'll give you a suggestion, it could be opened via visual studio code in wsl. And install the live server plugin.
Ok so first of all, I don't use windows anymore so I can't post a full solution that I've personally tested, but back when I did use windows, I use to do this and it worked. (This should probably be a comment, but a while back I deleted some unaccepted answers and lot the associated reputation :/)
Solution:
Don't try to launch your windows programs from inside WSL, instead install the linux version of the program and an X server, such as Xming. Here is an example tutorial for forwarding X apps back to Xming on windows.
Summarized, install Xming (on Windows). Then export the DISPLAY variable:
export DISPLAY=:0
Install google-chrome inside WSL and launch it via the CLI. It should show up on your desktop.
Note: There's also a way to use PuTTY alongside XMing for remote viewing, but you'll need to disable Windows firewalls and install the full openssh-server inside WSL first.

Can I add my own executables or batch scripts to the boot recovery command prompt in windows?

when you boot into windows and screw around with the F-Keys until you get to the repair menu, there is an option to run a command prompt. However, I cannot access any of the files on my PC from that command prompt. Is there any way for me to access them or add my own? Thank you a ton.
The cmd prompt
You could access other files, just cd yourself to other drives(you're
defaulted to driveX) – SteveFest
Perfect!

(ConEmu + Cygwin) How to change tab name of ConEmu from within a cygwin bash script

i've configuring my ConEmu + Cygwin enviroment. I've created a task, when i start it in a tab, the task will run a batch file, which in turn will change dir into cygwin and run
bash --login -i my_ssh_entry_script.sh
Inside my_ssh_entry_script.sh, it will read a config file in my home directory then print a menu for me to select which host to connect. And finally
...
exec ssh -p$port $userhost
Now i can work on the selected machine in the same conemu tab. This works fine. And the script also works on linux machine too.
But there is a little flaw. I can't change the tab's title. I've tried to change it to:
....
exec $(cygpath ${ConEmuDir})/ConEmu.exe /cmd ssh -p$port $userhost -cur_console:t:$title
But this will always create a new tab. Any suggestion to slove it? Thanks~
1) You need GuiMacro, thoroughly described in the project wiki:
http://conemu.github.io/en/GuiMacro.html
ConEmuC -GuiMacro Rename 0 "Title"
2) You do not need to run batch. It causes extra and useless cmd.exe in yours process tree. You can do all required "CD" and "SET" from the task content directly.
At least for cmd shell:
"-new_console:d:C:\Users\dir_name" cmd /V /K -new_console:t:Tab_renamed

What file explorer can I use in Cygwin with UNIX like paths?

I'm fed up with using Windows Explorer as my file browser as I use
Cygwin bash as my primary command line environment.
Windows Explorer shows the full path in the address bar.
Usually, I copy that, pasting it to the cygwin command prompt as in
cygpath ""
I then copy paste the output of cygpath so I can
cd to it. I've also set up cpath "Bash Shell Here"
but this only works on right clicking directories.
I've used WinSCP via SSH.
ie. Start an SSH daemon in cygwin and connect to it via WinSCP.
What file explorer can I use in Cygwin with UNIX like paths?
Is there a cygwin X Windows file explorer?
Have you tried Midnight Commander (mc)? It's in the Cygwin package repository.
If you surround the path from Windows Explorer with single or double quotes, there is no need to use the cygpath command.
In regard to your quest for a compatible file manager, I've heard that KDE's Konqueror runs on Windows.

Resources