How to elegantly create a Windows shortcut that starts a WSL program under X - windows

I'm struggling a little to launch xfce4-terminal from my WSL installation under VcXsrv from a button on the Windows taskbar. I don't want any DOS box/console/terminal other than the xfce4-terminal when I'm done. I have it working, but man is it ugly. Does anybody have a cleaner way of doing this?
Here is what I got working: I created a windows shortcut on the Desktop with this target (all in one line, broken with newlines for readability here):
C:\Windows\System32\wscript.exe
\\wsl$\Ubuntu-20.04\home\peter\bin\windows\startTerminal.vbs
startTerminal.vbs was inspired by from 10 Ways To Run Batch Files Silently And Hide The Console Window • Raymond.CC (one of the few solutions that didn't require installing a separate program for this!) and it contains:
CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter --exec /home/peter/bin/windows/startTerminal.sh",0,True
and startTerminal.sh contains:
export DISPLAY=localhost:0.0
xfce4-terminal --command=/bin/zsh
Setting DISPLAY is apparently required, even though I set the DISPLAY environment in ~/.zshrc. wsl.exe apparently doesn't do that unless you run the login shell.
Once all of this is working, I can drag my shortcut to the taskbar and click on it there.
I count 3, three files cooperating to achieve this simple goal? Can I limit that to one or two without installing an external program?

Yes, I think you can do what I do in one shortcut file. I'll share what works for me then you'll have to fiddle for your program.
Try putting all your commands in the Target section of the Shortcut link file.
Step 1. Right click on desktop then New then Shortcut then name it (LaunchXFCE4)
Step 2. Right click LaunchXFCE4 select Properties
Step 3. Enter all your launch sequences in the Target section
Step 4. Right click LaunchXFCE4 select Pin to Taskbar
I'm launching Emacs 27.1 on WSL 2 with Ubuntu 20.10 on Windows 10 using this code which perhaps you can modify.
C:\Windows\System32\wsl.exe --distribution Ubuntu20 bash -c "export DISPLAY=$(ip route | awk '{print $3; exit}'):0 && export LIBGL_ALWAYS_INDIRECT=1 && export XCURSOR_SIZE=16 && setsid emacs"
The first export display is similar to your localhost:0.0 so replace it or try mine. Maybe your xfce4 replaces my emacs. Read this post about double ampersands. So possibly this
C:\Windows\System32\wsl.exe --distribution Ubuntu20 bash -c "export DISPLAY=localhost:0.0 && export LIBGL_ALWAYS_INDIRECT=1 && export XCURSOR_SIZE=16 && setsid xfce4-terminal --command=/bin/zsh"
Note, my VcXsrv starts when windows starts by putting a shortcut to config.xlaunch in my C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.
By the way, Microsoft will provide a GUI for WSL in a few months, according to their blog
GUI app support in WSL is becoming a reality! We are getting closer to
an initial preview and happy to announce a preview release for Windows
Insiders within the next couple of months.

I removed the need for a startTerminal.sh file by:
Editing startTerminal.vbs to contain:
CreateObject("Wscript.Shell").Run "C:\Windows\System32\wsl.exe -u peter bash -l -c xfce4-terminal",0,True
And then I created a ~/.bash_profile to set the $DISPLAY variable, which probably should've been done anyway:
$ cat .bash_profile
if [ -z "$DISPLAY" ]; then
export DISPLAY=:0
fi
So now there is the shortcut and a startTerminal.vbs that starts it up. It still isn't very elegant...

One of the few good additions to Windows was WSL-2, it's much more capable now.
It's part of late Win 10 and Win 11.
Here is how to elegently create a terminal window, replace the terminal with any X app you want.
Linux: "apt-get install xfce4-terminal" (or equivalent)
Windows: Create a new Shortcut (right click in any windows folder)
2.1) Shortcut to -> C:\Windows\System32\wslg.exe -u root -- /usr/bin/xfce4-terminal
If you have multiple distributions use it like: "-d Debian"
That's it. You can give it a better icon and name it.
It will launch the application as if it was a windows application.

After experimenting with #Saj 's answer which did not work for me I resulted in the below:
C:\Windows\System32\wsl.exe --distribution Ubuntu-20.04 bash -c "export DISPLAY=$((route.exe print | Select-String 0.0.0.0 | select -first 1) -Split "\s+" | select -Index 4):0.0 && export LIBGL_ALWAYS_INDIRECT=1 && setsid emacs"
Note that my problem was that it could not use bash commands inside the parenthesis (grep, awk)
Environment: WSL2 on Windows 10

Related

Why does Fastscripts work with this but Platypus doesn't

Mac running Catalina. This code
#!/bin/bash
pbpaste|pbcopy
pbpaste>/tmp/tmp$$
open -W -a macvim /tmp/tmp$$
while [ `ps -A|grep MacVim|wc -l` -gt 1 ]
do sleep 1
done
cat /tmp/tmp$$|pbcopy
rm -f /tmp/tmp$$
is intended to plain-text the paste buffer then call up a terminal running macvim so I can use vi with less faff then put the result back in the clipboard. Its a way to speed-up editing when using various tools other tools and I just want to edit a section with vi.
I works well when called from Fastscripts or just plain execution but it won't work when I use Platypus to build a menubar app so its one fast click to use it - or rather, it works sometimes. Sometimes it hangs because it cannot connect input to the window in which macvim runs. I have to kill it from Activity Monitor to regain any control of input to other windows such as terminal. I've tried connecting stdin in the "open" command but still only works sometimes. And it shouldn't be standin anyway.
How is Fastscripts launching it and how can I do the same inside the script ?
I'd like very much to be able to launch it with a single click from the menubar but I don't know how to. I can build the platypus app if I know what to put in the shell script.
Thanks
andy

Opening Cygwin X11 applications in Windows 10 from desktop shortcuts

I have 64-bit Cygwin installed on Windows 10, and I would like to open Cygwin GUI applications such as atril from desktop shortcuts, without having to open a Cygwin terminal and launching them from the command line manually.
I have:
created a ~/.startxwinrc file containing the line "exec sleep infinity"
added these lines to ~/.bashrc
export DISPLAY=:0.0
which startxwin > /dev/null 2>&1
if [ $? -eq 0 ]; then
startxwin > /dev/null 2>&1 &
fi
I can now open the applications by launching a Cygwin terminal, typing the name of the application, and pressing Enter. However, the desktop shortcuts I have created silently fail. One of these was:
C:\cygwin64\bin\run.exe -p /bin xterm -display 0.0 -e /bin/bash -l /cygdrive/c/Users/MYNAME/atril-launcher.sh
(I think that one failed because xterm and startxwin are incompatible; I may be wrong.)
I have tried other command lines which include run.exe, but have been unable either to launch the application or to get a meaningful error message.
atril-launcher.sh has the correct permissions, and contains the following text after the shebang (I couldn't get the formatting right trying to post the shebang in here.). It can be successfully launched from within the Cygwin terminal.
nohup /bin/atril.exe &
sleep 3s
I have searched the Internet for information on this problem, much of which is out of date (Cygwin now using /bin instead of /usr/bin, Cygwin no longer having an X11R6 folder, DISPLAY=:0.0 instead of DISPLAY=localhost:0.0, etc...) but at this point I'm at my wits' end.
It looks as though I've found a partial answer to my own question. It's imperfect for various reasons:
It probably depends on startxwin already having been launched elsewhere as a background process.
The applications are slow to launch; there's a delay of a few seconds.
Terminal windows which the user cannot interact with (presumably the shells which act as parent process for the GUI applications) appear alongside the application windows.
But it will probably be enough to be getting on with. Assuming pluma to be our application, here is the shortcut that successfully launches it:
C:\cygwin64\bin\mintty.exe /bin/bash -l -c "export DISPLAY=:0.0; pluma"
I'd like to thank the author of answer https://stackoverflow.com/a/12215570/9223868 - which I did not find through googling, only through its appearing in the "Related" list when this was posted.

Set up path on prompt on pycharm terminal windon

I just upgrade pycharm into 2016.3.1. Before upgrade, I do not have problem to see the current directory path under the prompt on terminal window. After upgrade into pycharm 2016
3.1. All directory path on prompt on terminal window seems like messed up with 133;C;133;D;01337;RemoteHost=hawkins#pc_name.home1337;CurrentDir=/Users/hawkins/path133;MAC:path hawkins$ 133;B for some reason. Anyone have expereience on how to resolve this?
MAC:path$
133;C;133;D;01337;RemoteHost=hawkins#pc_name.home1337;CurrentDir=/Users/hawkins/path133;MAC:path hawkins$ 133;B
133;C;133;D;01337;RemoteHost=hawkins#pc_name.home1337;CurrentDir=/Users/hawkins/path133;MAC:path hawkins$ 133;B
133;C;133;D;01337;RemoteHost=hawkins#pc_name.home1337;CurrentDir=/Users/hawkins/path133;MAC:path hawkins$ 133;B
I had a similar problem. It turned out my problem was due to have iTerm shell integration installed.
As you have tagged the question [osx], I assume that you might also have done this.
There is a nice explanation to what these strange symbols mean in this answer: https://unix.stackexchange.com/a/294886/47407
I figured I could avoid having to uninstall Shell Integration by clearing my PROMT_COMMAND, and setting PS1 again. I made a shell script called pycharm_terminal.sh with the following
export PROMT_COMMAND=
export PS1="\[\e[31m\]\u\[\e[0m\] at \[\e[33m\]\h\[\e[0m\] in \[\e[32m\]\w\[\e[0m\] at [\A] \[\033[31m\]`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\[\033[35m\]\n$ \[\033[00m\]"
bash -i
The PS1 variable can be set to whatever you had before. This is just how I like mine.
Now, in PyCharm Settings: Go to Tools > Terminal and in "Shell path" set it to /bin/bash <path_to_pycharm_terminal.sh>.
Now try opening a new terminal in PyCharm. It shouldn't have those control character errors.
Solution that worked for me:
Go to Settings > Plugins
Find plugin "Python Terminal" and disable it
Restart PyCharm
The only negative effect I've noticed - auto activation of virtualenv on terminal start doesn't work.
for some reason, I figure it out by create a .pycharmrc under by user directories
and setting up /usr/local/bin/bash --rcfile ~/.pycharmrc under Tool->Terminal on shell path. It seems like it fixed but i have no idea why
MAC: path$ cat ~/.pycharmrc
exec bash
This is what worked for me:
Created new file pycharm_terminal.sh with one command bash -l
Then I set PyCharm Settings: to Tools > Terminal and in "Shell path" set it to /bin/bash/path_to_pycharm_terminal.sh.
Restart PyCharm and all work as expected
bash -l, by man, is a: -l Make bash act as if it had been invoked as a login shell (see INVOCATION below).
which is probably mean, that when PyCharm starts the Terminal and executing the pycharm_terminal.sh with bash -l, everything in .bash_profile is include by default
Found this question while trying to resolve similar issue for 'Geany' on mac osx. I had an install of iterm2 on my machine and was getting garbage in front of the terminal prompt.
similar solution as KPLauritzen offered also works for 'Geany' terminal as such.
Hope this helps someone:
Create shell script in home directory using desired PS1 & CLICOLOR settings:
~/geany_terminal.sh
export PROMT_COMMAND=
export PS1='[\e[1;32m][\u#\h \W] \D{%F %T}\n\$[\e[0m]'
export CLICOLOR=1
export LSCOLORS=gxBxhxDxfxhxhxhxhxcxcx
bash -i
Then I went to 'Edit -> Preferences --> Terminal' and set my 'Shell' to:
/bin/bash /Users/myusername/geany_terminal.sh
Upon reloading Geany, the Terminal now displays with PS1 and CLICOLOR settings exactly as I have set in my .bash_profile without garbage in front.
You can just activate your .bash_profile
/bin/bash --rcfile ~/.bash_profile

Set Default Shell in Cygwin

I've been using Cygwin for quite a while now. I now want to switch to fish from bash. Everyone is saying that you should use chsh -s /usr/bin/fish, but for some reason, chsh does not work for me. Trying to modify my Cygwin.bat file doesn't work either, and neither does changing my /etc/passwd file.
I really don't understand what's going on. Could someone please help me?
Try editing /etc/nsswitch.conf instead of /etc/passwd
Instead of creating a passwd file, which Cygwin recommends against1, you could edit /etc/nsswitch.conf. Add or edit the following line:
db_shell: /usr/bin/fish
The down/up side of this method is that, if you have multiple users, this change affects all of them. The up/up side is that it's dead simple. The only catch is that you have to restart Cygwin.
If you do use mkpasswd after this change, it will use your new default shell for all users that are allowed to log on.
References
1 The mkpasswd documentation says this:
Don't use this command to generate a local /etc/passwd file, unless you really need one. See the Cygwin User's Guide for more information.
I can't really find any solid reasoning in the user's guide, other than a mention that you'll have to regenerate the /etc/passwd and /etc/group files if your users and groups change, which I suppose is a decent enough reason. I can say that the process is somewhat error prone for newbies.
I randomly meet the same problem, and now I providing my solution for you.
0- Open cygwin by mintty.exe
1- Build passwd file manually (mkpasswd)
$mkpasswd > /etc/passwd
2- Change default shell in passwd file
$vim /etc/passwd
user:*:1976XX:1971XX:U-user\user,S-1-5-21-XXXX:/home/user:/bin/fish
3- Restart mintty.exe
There's an answer on SuperUser that is even simpler. Set the SHELL environment variable in Windows. Set it to your preferred shell (e.g. /bin/tcsh) and newly-launched shells should use it.
To set environment variables:
Go to the "System" control panel (right-click on the Start menu and select "System")
Click on "Advanced system settings" in the left-side column
From the "System Properties" window that pops up, click the "Environment Variables..." button
In either the "User variables" or "System variables" box (depending on whether you want the change to affect just yourself or all users), click the "New..." button.
Type "SHELL" in the "Variable name" box and the path (relative to the Cygwin file system) to your preferred shell in the "Variable value" box.
Click OK on the three open windows to save your changes.
I'm not sure how 'proper' of a solution this is, but I just added /usr/bin/zsh; exit to the bottom of my .bashrc file. You can substitute /usr/bin/zsh for the path to your shell of choice. It seems to work just fine so far.
If you're running Cygwin through ConEmu I found the easiest way is to replace commands line to run fish instead of default shell (Settings->Startup->Tasks):
""c:\cygwin64\bin\fish.exe" --login -i" (please change path to your cygwin folder accordingly)
I wanted to comment on the accepted answer, but not enough rep.
Regarding this method
$ mkpasswd > /etc/passwd
While this works to generate an /etc/passwd file, the odds are you're installing cygwin for yourself, and if your computer belongs to a domain, mkpasswd may run out and attempt to consume the entire domain user list. I had a 500MB+ /etc/passwd file from exactly this condition, and cygwin wouldn't even start until I deleted the file.
I strongly recommend, if using the mkpasswd route to use the -c switch, so it puts an entry in only for yourself.
$ mkpasswd -c > /etc/passwd
On a machine with a shared cygwin service (assuming you have write permissions)
$ mkpasswd -c >> /etc/passwd
I was trying to change my default shell in cygwin to zsh and I found parts of the answers
here helpful, but none of them gave a full solution. The best instructions I found were in the "installing zsh" instruction in the oh my zsh github repo by Robby Russel that you can find here
At the top of the page it has general instructions for installing zsh and then specific instructions for many different platforms. You can scroll down to the Cygwin section and follow the steps:
"The easiest way to change the default shell is to set your SHELL user environment variable. Search for "Edit Environment variables for your account" to bring up the environment variables window, create a new variable named "SHELL" and give it the value "/usr/bin/zsh/"."
(On Windows 7:
Press Windows key.
Start typing "environment".
When it appears, select "Edit environment variables for your account" and add the new variable as stated above.)
After I did this I closed and reopened my Cygwin shell window and tested that it worked with echo $SHELL. Expected result: /bin/zsh or similar.
Hope this helps!
Go to c:\cygwin or c:\cygwin64 (whatever is your cygwin home directory) and edit cygwin.bat. Instead of
bash --login -i
write, for example,
rem bash --login -i
tcsh -l
One simple way to do this is call tcsh at the end of .bashrc file
# x2=$(dirs +${cnt} 2>/dev/null)
# [[ $? -ne 0 ]] && return 0
# [[ ${x2:0:1} == '~' ]] && x2="${HOME}${x2:1}"
# if [[ "${x2}" == "${the_new_dir}" ]]; then
# popd -n +$cnt 2>/dev/null 1>/dev/null
# cnt=cnt-1
# fi
# done
#
# return 0
# }
#
# alias cd=cd_func
tcsh
alias g=gvim
source ~/.cshrc
source ~/.aliases

How do I autorun an application in a terminal in Ubuntu?

I've created a few autorun script files on various USB devices that run bash scripts when they mount. These scripts run "in the background", how do I get them to run in a terminal window? (Like the "Application in Terminal" gnome Launcher type.)
Run them as a two stage process with your "autorun" script calling the second script in a new terminal eg
gnome-terminal -e top --title Testing
Would run the program "top" in a new gnome terminal window with the title "Testing" You can add additional arguments like setting the geometry to determine the size and location of the window checkout the man page for gnome-terminal and the "X" man page for more details
xterm -e shellscript.sh
or (if xterm isn't installed)
gnome-terminal -e shellscript.sh
or (if you're using kubuntu / kde)
konsole -e shellscript.sh

Resources