How to open a tab/pane in the same directory in Windows Terminal with Fish shell - fish

Microsoft has a guide how to do that for other shells such as bash and zsh but not for fish shell.
https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory

open your fish config file located at ~/.config/fish/config.fish and add to it the following function:
function storePathForWindowsTerminal --on-variable PWD
if test -n "$WT_SESSION"
printf "\e]9;9;%s\e\\" (wslpath -w "$PWD")
end
end
Explanation:
This function is a hook that is called whenever current path is changed.
It will confirm current session is opened by Windows Terminal (By verifying environment variable $WT_SESSION exists), and will send Operating System Command (OSC 9;9;), with the Windows equivalent path (wslpath -w) of current path.
This sequence will let Windows Terminal know that the path was changed and when a new tab / pane will be created, Windows Terminal will use the changed path as current directory.

Related

How to start WSL terminal in a current powershell directory?

I can open a powershell and type
> Ubuntu
to open a WSL ubuntu shell on windows 10. This will always take me to the WSL home directory. How would I instead open the terminal in the same location that the powershell is currently in?
FYI. I need this for creating a right click "open terminal here" type registery key for windows explorer.
If we look at what running ubuntu.exe actually does:
PS C:\> ubuntu.exe /?
Launches or configures a Linux distribution.
Usage:
<no args>
Launches the user's default shell in the user's home directory.
install [--root]
Install the distribution and do not launch the shell when complete.
--root
Do not create a user account and leave the default user set to root.
run <command line>
Run the provided command line in the current working directory. If no
command line is provided, the default shell is launched.
config [setting [value]]
Configure settings for this distribution.
Settings:
--default-user <username>
Sets the default user to <username>. This must be an existing user.
help
Print usage information.
We can see that it by default launches the WSL shell inside the home directory. If we want run inside it inside the current directory open in PowerShell, we need to specify the run option. So the full command will be ubuntu.exe run.
Another option is to just run wsl.exe or bash.exe. These commands will by default open WSL in the current working directory.
Note: We don't need to specify the .exe after the commands. Running ubuntu, wsl and bash all work as well. PowerShell knows how to run executables without specifying the extension.
method 1:
set Ubuntu to wsl default distribution by wsl --set-default Ubuntu
just type wsl and you are in the current Powershell directory
method 2:
open other folder
wsl.exe --cd $pwd or wsl.exe --cd "path"

VSCode terminal windows - git-bash aliases getting ignored

I've created aliases in c:\Users\user\.bash_profile and in C:\Program Files\Git\etc\profile.d\aliases.sh but both configs getting ignored by VSCode integrated terminal, which is configured to use git bash:
"terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe",
if I open GitBash itself - aliases works fine
how do I force integrated terminal to respect the configs?
You can try adding to the settings:
// The command line arguments to use when on the Windows terminal.
"terminal.integrated.shellArgs.windows": [
"--login", "-i"
],
-i - force the shell to run interactively.
--login - make this shell act as if it had been directly invoked by login. When Bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
When invoked as an interactive shell with the name sh, Bash looks for the variable ENV, expands its value if it is defined, and uses the expanded value as the name of a file to read and execute. Since a shell invoked as sh does not attempt to read and execute commands from any other startup files, the --rcfile option has no effect. A non-interactive shell invoked with the name sh does not attempt to read any other startup files.
Read more.
As an alternative you can use the .bashrc file instead of .bash_profile.
The solution using the "--login" and "-i" shell arguments did not work for me. What did work was using the bash "-rcfile" shell argument, like this, in my settings file:
"terminal.integrated.shellArgs.windows": [
"-rcfile",
"c:\Users\\.bash_profile", ],
... where <userid> is my Windows userid and the alias commands are in a file called ".bash_profile" that is located in c:\Users\<userid>
I simply switched to my root user directory c:\Users\user then ran source .bashrc. This did the trick on my machine, hope it helps.
Create .bashrc in c:\Users\username
Add an alias, e.g alias gotossh="cd /c/users/username/.ssh"
In your terminal, run source .bashrc while in c:\Users\username
Confirm that the alias works by running alias
You might not have a .bashrc file in the users folder which is important to note.
Also remember to write the paths correct and don't leave any space between the equals in e.g alias="somecommand"
As of 2020 there is no need to add the shellArgs.

executable file must be placed in either the current working directory or somewhere in the command path

I'm installing a command line program called plink that recommends this:
The PLINK executable file should be placed in either the current
working directory or somewhere in the command path. This means that
typing "plink" or "./plink" at the command line prompt will run PLINK,
no matter which current directory you happen to be in. PLINK is a
command line program -- clicking on an icon with the mouse will get
you nowhere.
I have the PLINK executable file. What should I do next?
Run 'chmod +x plink' to grant the file execution rights.

How to run perl script from any where (any directory)

I have a perl script exist in the follwoing path (/home/Leen/Desktop/Tools/bin/tool.pl)
Every time I want to run this tool I go to the terminal
>
and then change the directory to
..../bin>
Then I run the perl by writing
..../bin> perl tool.pl file= whatever config= whatever
The problem is that I want to run this perl script without the need to go to the bin folder where it exist . so I can run perl script from any directory and as soon as I enter shell
I went to the etc/environment and I wrote the follwoing
export PERL5LIB=$PERL5LIB:/home/Leen/Desktop/Tools/bin
But when I go to terminal and write the follwoing straight ahead without going to bin folder where tool.pl exist
>perl tool.pl file=... config=...
it says the file "tool.pl" does not exist???
The first argument to the perl program is the path to an executable file. These calls are equivalent:
:~$ perl /home/Leen/Desktop/Tools/bin/tool.pl
:~$ perl ~/Desktop/Tools/bin/tool.pl
:~$ perl ./Desktop/Tools/bin/tool.pl
:~/Desktop/Tools/bin$ perl tool.pl
:~/Desktop/Tools/bin$ perl ./tool.pl
etc.
In the shell the tilde ~ expands to your home directory, and ./ symbolizes the current directory. On *nix shells (including the various terminal emulators on ubuntu), the command prompt ususally is $ in nomal mode, # as root user and seldom %. > Is a secondary command prompt, e.g. when continuing a multiline argument, unlike cmd.exe on Windows.
The PERL5LIB variable determines where Perl looks for modules, not for executable files.
You can set a script as executable via chmod +x FILENAME. You can then call the script without specifying the perl program:
:~/Desktop/Tools/bin$ ./tool.pl
You can modify the PATH variable to change where the shell looks for executables. The PATH usually contains /usr/bin/ and other directories. You can add a directory of your own via
PATH=$PATH:/home/Leen/Desktop/Tools/bin
Add your directory at the end of the PATHes, so you don't overrule other programs.
If you want to set this permanently, you can add this line to the file ~/.bashrc (only for your user and only for the bash shell).
Then you can call your script from anywhere, without a full path name:
:~/foo/bar$ tool.pl
You should consider using a more specific command name in this case, to prevent name clashes.

Cygwin - run script silenty from "run command"

I have script lets say:
C:\foo.bsh
I want to be able to run this command via the windows run command:
Start -> Run
Windows Key + R
and type something small like 'foo' and hitting return.
However, I do not want a cmd prompt to be visible. This script does some preprocessing for an IDE. I do not want the cmd prompt to be open for the lifetime of the IDE process.
I have tried:
1) Creating a bat file with the following contents:
c:\cygwin\bin\bash --login "C:\foo.bsh" (this fails because it keeps a cmd open)
2) Converting the above bat file to an exe using bat_2_exe_converter (does not make the cmd silent)
thoughts?
EDIT: The solution so far suggests something to type from an actual cygwin shell. I am trying to get a faster solution by having something short I can type in the Windows run command. Also, the nohup command; exit doesn't automatically kill the box - however I can manually kill it without killing the IDE process. The run command accepts shortcuts (.lnk's), bat's, exe's.
Try the run.exe command of cygwin. It is a big install, a complete unix environment for your Windows machine. Suppose you installed it at c:\cygwin\.
No mystery, just run c:\cygwin\bin\run.exe <your command here> and you will have your no dos window execution.
You can run it from any DOS window (run cmd.exe from the start menu). You don't need to run it from cygwin.
To make it easier, append C:\cygwin\bin to your %PATH% env var (My Computer → Properties → Advanced → Environment Variables) (Kudos to Felipe Alvarez comment).
Now you can just type
c:\cygwin\bin\run.exe "C:\foo.bsh"
You must create a link in your Start Menu with this command so will be able to run it with Win-R.
Here is the man page of the runcommand:
$ man run
RUN(1) run 1.3.0 RUN(1)
NAME
run - start programs with hidden console window
SYNOPSIS
run [ -p path ] command [ -wait ] arguments
runcommand [ -p path ] [ -wait ] arguments
DESCRIPTION
Windows programs are either GUI programs or console programs. When
started console programs will either attach to an existing console
or create a new one. GUI programs can never attach to an exiting con‐
sole. There is no way to attach to an existing console but hide it if
started as GUI program.
run will do this for you. It works as intermediate and starts a pro‐
gram but makes the console window hidden.
With -p path you can add path to the PATH environment variable.
Issuing -wait as first program argument will make run wait for program
completition, otherwise it returns immediately.
The second variant is for creating wrappers. If the executable is
named runcommand (eg runemacs), run will try to start the program (eg
emacs).
EXAMPLES
run -p /usr/X11R6/bin xterm
run emacs -wait
runemacs -wait
run make -wait
AUTHORS
Charles S. Wilson
Harold L Hunt II
Jehan Bing
Alexander Gottwald
Version 1.3.0 November 2005 RUN(1)
You can use either...
c:\cygwin\bin\bash -l /path/to/script_to_interpret.sh
...or...
c:\cygwin\bin\bash -l -c /path/to/executable_script.sh
Note: the -l flag tell bash to "act as if it had been directly invoked by login" and use Bash Startup Files. This is important in that it sets your $PATH and other things you rely on when you launch a cygwin terminal. If you don't include -l or --login you will get "command not found" when you try to call anything except of a bash builtin.
The difference between the 2 is like the difference between doing...
bash script_to_interpret.sh
...and...
./executable_script.sh
...in *nix. The former interprets the script using bash. The latter executes the script (only if it has chmod +x executable_script.sh) and interprets it according to its "shebang" line. The latter method is also what you want to do if your executable is not a script at all, like a *nix binary compiled from source.)
It has been bugging me for a while I couldn't find the solution for this, but I finally got the right mix together.
You can simply do the following if you have cygwin on your PATH:
run bash test.js
If cygwin is not on your path, you can do this:
c:\cygwin\bin\run.exe -p /bin bash test.js
If you are looking for more control over the created window (maximize, etc) it looks like you can use cygstart also.
Sources:
- neves answer above (though that wasn't enough by itself for me personally to figure it out)
- http://cygwin.com/ml/cygwin/2008-09/msg00156.html
As the terminal can't close while your script is still running, try the command:
"nohup C:\foo.bsh; exit"
This way your script will be backgrounded and detached from the terminal, and it should exit quickly so the terminal goes away. I think that the window may still 'flash' with this approach, but the results should be better than what you're getting.
I'm running Cygwin64 and the xwin server link points to:
C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
This creates an icon AND a notification on the taskbar. I don't like that. The icon is rather useless, the notification has all your menu options from .XWinrc.
So... I wrote a .vbs script to silently run this command and make the icon go away:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe"), 0
Another imperfect possibility is to run the script via a shortcut and set the shortcut's Run option to "minimized".
Go to the directory where you have installed cygwin(on my machine it is c:/cygwin64/bin)
Once there simply type "bash.exe"