How to spawn a new tab in same directory as previous directory? [closed] - windows-terminal

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
This post was edited and submitted for review 11 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I want to simply start a new tab in the same folder as my previous tab that I was in. Is this possible, and if so how? This is with the Windows Terminal by the way.
Now if I open a new tab, it will open in home directory (which is configured in the Windows Terminal's settings page).

Open the windows terminal
Click the dropdown button
Click the settings option
set the startingDirectory value to "./"
Example
"profiles":
{
"defaults":
{
// Put settings here that you want to apply to all profiles.
"startingDirectory": "./" <---- set this value
}

The Windows Terminal command line interface (wt.exe) can be used to achieve this, but with a command instead of a keyboard shortcut.
wt -w 0 nt -d .
-w 0: Use the current terminal window.
nt or new-tab: Create a new tab.
-d .: Start the process in the current directory.
Doc: https://learn.microsoft.com/windows/terminal/command-line-arguments

Zsh
If you are using Zsh, you can add this to ~/.zshrc
[[ -n "$WT_SESSION" ]] && {
chpwd() {
echo -en '\e]9;9;"'
wslpath -w "$PWD" | tr -d '\n'
echo -en '"\x07'
}
}
After that, you can use duplicate tab in Windows Terminal to open the same directory in new tab
Bash
You can also do similar thing in Bash
my_prompt() {
echo -en '\e]9;9;"'
wslpath -w "$PWD" | tr -d '\n'
echo -en '"\x07'
}
export PROMPT_COMMAND=my_prompt
Explanation
$WT_SESSION is an environment set by Windows Terminal. This will ensure we define this escape sequence only in WT.
chpwd() is a callback function that Zsh will invoke whenever it changes the working directory.
Inside the chpwd() it will send a OSC 9;9 escape sequence. This will tell the terminal (many terminals including Windows Terminal) that the CWD of the shell has been changed.
The syntax of OSC 9;9 escape sequence is \e]9;9;"C:\Temp\"\x07
The C:\Temp\ in the sequence has to be Windows-style path. It can point to some path in WSL (e.g. \\wsl.localhost\Ubuntu-20.04\home\superman\bin). When opening new tab, the shells will open corresponding UNIX path (e.g. /home/superman/bin).

This is not currently possible, as explained here https://github.com/microsoft/terminal/issues/1437 the issue is closed at the moment but could perhaps be re-opened in future.

I've found something interesting.... This worked for me... You can put:
"startingDirectory": "./"
In the terminal configuration....
Example
{
// Make changes here to the powershell.exe profile.
"guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
"name": "Windows PowerShell",
"commandline": "powershell.exe",
"startingDirectory": "./",
"hidden": false
},

You can always rewrite setting json evertime you open a tab but its a shell specific hack.
Put this function into $PROFILE (make sure to adjust $path)
function sd {
$path = 'C:\Users\Admin\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json'
((Get-Content -path $path) -replace '"startingDirectory":.*', ("`"startingDirectory`": `"$pwd`"") -replace "\\", "\\") | Set-Content -Path $path
}
Solution taken from here.

dotnet tool install -g p2u
p2u dir ${PWD}
Then CTRL+SHIFT+D
Hopefully it will work with split pane as well
Same idea of #apena but now with a dotnet tool. For further details: https://github.com/celsojr/p2u

Also a similar vein as #apena's post as it is specific to bash.
In my case I wanted Debian to open on my current working directory.
Although I couldn't exactly get this behavior I was able to get it to open the last directory visited (effectively) using a few hacks:
In my linux distro, I added this to my .bashrc to intercept all calls to cd and store the last call to cd into a destination of my choice.
cd() {
builtin cd "$#" ; echo $PWD > /opt/chdir/lastestdir
}
Next, since Windows Terminal didn't let me inline the command, I created this batch script to in a custom location:
#echo off
for /F "tokens=*" %%n IN (\\wsl$\Debian\opt\chdir\lastestdir) DO #(wsl.exe -d Debian --cd \\wsl$\Debian%%n)
NOTE: \\wsl$\Debian is the path to my distro within Powershell, and the \opt\chdir\lastestdir is based on the path I used in the previous step.
Next, I edited the profile for Debian in Windows terminal to point at my script:
I'm open for suggestions, but this let me happily duplicate my current directory to the next tab as long as it was the last terminal I had changed directory in.

Related

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

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

How to start Atom or VSCode natively installed on Windows from within WSL (Ubuntu)?

I have installed Atom editor natively on Windows 10 by downloading an running the installer. Now I start WSL Ubuntu distro and want to start Atom (atom-editor) from there with the command atom . or VSCode (visual-studio-code) with the command code .
Atom starts, but not in the directory where the command was executed, instead it shows files from C:\\Windows. Moreover Ubuntu WSL terminal shows following error message:
atom .
grep: /etc/wsl.conf: No such file or directory
"\\wsl$\Ubuntu-18.04\home\wlad\projects\udemy\flask-bootcamp\Flask-Bootcamp-master"
CMD.EXE wurde mit dem oben angegebenen Pfad als aktuellem Verzeichnis gestartet.
UNC-Pfade werden nicht unterstützt.
Stattdessen wird das Windows-Verzeichnis als aktuelles Verzeichnis gesetzt.
Sorry it's German localized but it says something like UNC-paths are not supported
(haven't tested VSCode yet)
So how can I use Atom or VSCode editor installed on Windows 10 from within WSL?
** UPDATE **
As of today (April 2020) there is a much better way to use VSCode on Windows w/ WSL, VirtualMachines (VM) and even Containers. Check out remote-development plugin for VSCode.
I created a short script to handle the three atom commands I use most (I use Ubuntu with WSL):
atom
atom .
atom RELATIVE_PATH_FILE
This script is not optimized, and I'm sure many people will find edge cases, but it gets the job done for me. To use it, simply placed it in ~/.local/bin/atom and make it executable by running chmod +x ~/.local/bin/atom. You may need to restart your shell for ~/.local/bin to be added to your path. In order to simplify things a bit, I mapped the WSL network drive for my ubuntu installation to U:, so you'll either want to do the same or modify the script accordingly on line 9.
#!/bin/bash
if [ -z $1 ]; then
pushd /mnt/c > /dev/null
/mnt/c/Windows/System32/cmd.exe /c "atom"
else
[[ $1 = "." ]] && path=$(pwd) || path=$(realpath $1)
winPath=$(echo "U:$path" | sed -e 's/\//\\/g')
pushd /mnt/c > /dev/null
/mnt/c/Windows/System32/cmd.exe /c "atom $winPath"
fi
popd > /dev/null
The script performs a few simple steps. First, if there is no command line argument, it simply calls atom using cmd.exe without arguments. If the command line argument is ., it gets the path to the current directory, otherwise, it gets the absolute path to the given file using realpath. The path is converted from POSIX to Windows format using sed before calling atom using cmd.exe as before, but with the path argument.
The pushd and popd commands are just there to get rid of the annoying warning message about UNC paths not being supported:
...
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory
In the "Known issues" section of the blog post #Wlad mentioned, there states
Accessing Linux files is treated the same as accessing a network resource, and any rules for accessing network resources will still apply e.g: When using CMD, cd \\wsl$\Ubuntu\home will not work (as CMD does not support UNC paths as current directories), however copy \\wsl$\Ubuntu\home\somefile.txt C:\dev\ will work
So as Atom may use cmd.exe to launch itself from the command line (maybe some batch file), and given the fact that cmd.exe cannot open network resources as current directory (which WSL directory is treated as), there came the failure as you attempted to launch Atom from WSL shell.
Actually, in VS Code there is a better solution to launch VS Code directly from the WSL shell: VS Code Remote.
You can take the following steps to enable VS Code to be directly launched from WSL shell:
Install the extension Remote - WSL to VS Code on the Windows side;
Then when you type code . in your WSL shell, VS Code Remote Server will be automatically installed, and VS Code will soon launch.
By using VS Code Remote, you can not only open the directory in VS Code, but can also be benefited in many other aspects: for example, you can use the WSL shell as the integrated shell in VS Code and run programs in WSL directly from VS Code.
Here is the official doc for VS Code Remote - WSL.
The script in Eduardo's answer is a great approach, but didn't allow to open multiple directories/repos at once (e.g. atom terraform-modules terraform-repo), which I do often.
The following is my twist on it:
#!/bin/bash
winPathPrefix="U:"
function convertToWinPath() {
echo "${winPathPrefix}$(realpath ${1})" | sed -e 's/\//\\/g'
}
declare -a atomCmd=(/mnt/c/Windows/System32/cmd.exe /c "atom")
for path in "$#"; do
atomCmd+=($(convertToWinPath ${path}))
done
${atomCmd[#]} 2>/dev/null
That is entirely based on Eduardo's script and should allow a more general use case
Wanting to run Atom from WSL got me here but unfortunately the accepted answer does not mention atom and the other atom related workarounds don't work anymore.
In case someone googles the question and ends up here. Here's an actual workaround (It will break in new Atom updates).
Add the following path path in the windows enviroments: C:\Users\{username}\AppData\Local\atom\app-{version}\ (at the time of this post the version is 1.60.0 so app-1.60.0.
Use the path mentioned above as the default path contains a bash executable that will be run fail to run in wsl.
Here's where it will break in future updates. The fix is to update the env in windows to the new path since the folder where the exe is located will change to match atom's version.
Add the following in your .bashrc or .zshrc:
Important to be added in .bashrc or .zshrc as making a separate script in /usr/bin will always make atom to open in the C:/Windows folder.
function _atom () { exec nohup atom.exe "$#" &> /dev/null & } # Do not output in terminal and do not block the terminal. Also send the command arguments to atom.
alias atom="_atom"
Open a new wsl terminal.
I wrote a bash script to open atom with and without files from WSL2. It can handle any number (including 0) of file arguments, on any drive. Both relative and absolute paths are supported, but it can't handle path name containing .. or ~. Pointing atom to a director also works as expected. Here's my script:
#!/bin/bash
atom_cmd="/mnt/c/Users/`whoami`/AppData/Local/atom/atom.exe"
for i in "$#"; do
if [[ $i == /mnt* ]]; then
linPath="$i" #for absolute file paths
else
linPath="`pwd`/$i" #for relative file paths
fi
if [[ $linPath == *".."* || $linPath != "/mnt"* || $i == "/home"* ]] ; then
echo "atom script is unacceptable file path $linPath"
continue 1
fi
winPath="\""`echo $linPath | sed -e 's|\/mnt\/\([a-z]\)|\u\1:|' -e 's:\/:\\\\:g'`""
atom_cmd="$atom_cmd $winPath\""
done
unset linPath
unset winPath
echo "command:" "$atom_cmd"
eval "$atom_cmd"
unset atom_cmd
(I'm sure there are things to improve about this, like edge cases and better use of language features. I'd welcome suggestions.)
this can be a little bit outdated but you can simply run a powershell and use:
wsl.exe -d Ubuntu-20.04 //In my case ubuntu
This should open a ubuntu session or whatever wsl you have set on your own.
A little bit nooby on this but trying to help. =)

How can I setup IntelliJ to remember Git Bash current working directory between sessions?

I'm running IntelliJ 2018.3 on Windows 7, as well as openSUSE Leap 15.
Under Windows 7, I've configured IntelliJ to use Git Bash, i.e., in Settings, under Tools -> Terminal, I'm setting Shell path to:
C:\Program Files (x86)\Git_2.17.1\bin\bash.exe
One of IntelliJ's new features is the ability to save and reload terminal sessions (see this link).
It works perfectly with openSUSE, however, on Windows, while the terminal tab names are correctly restored, I always end up with a new shell.
Is there a way to make IntelliJ and Git Bash play well together so that I can retain the current working directory and shell history after restarting IntelliJ?
You can try and setup your Git for Windows bash to remember the last used path for you, as seen in "How can I open a new terminal in the same directory of the last used one from a window manager keybind?"
For instance:
So instead of storing the path at every invocation of cd the last path can be saved at exit.
My ~/.bash_logout is very simple:
echo $PWD >~/.lastdir
And somewhere in my .bashrc I placed this line:
[ -r ~/.lastdir ] && cd $(<~/.lastdir)
That does not depend on Intellij IDEA directly, but on the underlying bash setup (here the Git for Windows bash referenced and used by Intellij IDEA.
Here's a possible workaround. It was heavily inspired by VonC's answer, as well as other answers to the question that he mentioned.
~/.bashrc
if [[ -v __INTELLIJ_COMMAND_HISTFILE__ ]]; then
__INTELLIJ_SESSION_LASTDIR__="$(cygpath -u "${__INTELLIJ_COMMAND_HISTFILE__%history*}lastdir${__INTELLIJ_COMMAND_HISTFILE__##*history}")"
# save path on cd
function cd {
builtin cd $#
pwd > $__INTELLIJ_SESSION_LASTDIR__
}
# restore last saved path
[ -r "$__INTELLIJ_SESSION_LASTDIR__" ] && cd $(<"$__INTELLIJ_SESSION_LASTDIR__")
fi
I don't like the fact that I had to wrap the cd command, however Git Bash does not execute ~/.bash_logout unless I explicitly call exit or logout; unfortunately due to this limitation, the .bash_logout variant is inadequate for the mentioned scenario.
The workaround above also leave small junk files inside __INTELLIJ_COMMAND_HISTFILE__ parent dir, however, I couldn't do any better.
Additionally I've opened a ticket in Jetbrain's issue tracker. There are many different shells that may benefit from official support. It would be great if JetBrains could eventually support powershell and popular terminals like windows-subsystem-for-linux, cygwin and git-bash. The only shell that currently works out of the box for me is cmd.

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

cygwin clearscreen from bash

I want to clearscreen from bash in a cygwin terminal.
I have tried
cmd /c cls
echo -e "^V^L"
echo -e "\014"
None of these work - they all give a "linefeed". Pressing CTRL+L does work - but I want to call it from a Bash script.
Any ideas?
Or you could try ctrl-L :)
That should work in most emulated terminals as well.
There is two ways:
Using shortcuts
Alt+F8 to clear screen and Ctrl-L to skip page
Using Clear alias
Go to: cygwin\home\USER\.bashrc
Open .bashrc file and put this alias alias clear='printf "\033c"'.
restart your cygwin terminal.
to clear your screen use clear command.
If it's contained in your cygwin install (run Cygwin's setup and make sure you have the package called "ncurses"), try
tput clear
I have this in my ~/.bashrc:
alias clear='printf "\033c"'
likewise, printf "\033c" clears the screen from within a bash script.
Alt+F8 to clear screen and Ctrl-L to skip page
Using shortcuts (for mitty console users)
Alt+F8 to clear screen and Ctrl-L to skip page
Using sh bash adding a command
If you use console2 or same consoles, you can add this sh code as without extension this location : ..\cygwin64\bin\
cls and /or clear (without extension)
#!/bin/sh
printf "\033c"
Then you can use cls and|or clear in cygwin command.
I was trying to do something similar and upon researching I found the above answers, I tried to follow some of the above solutions, I added the line to .bashrc, but it did not accept the function, I tweaked around and found out another solution...it basically executes the ' printf "\33c" but instead of tweaking the .bash.rc file, you simply go to the directory - /cygwin/bin ,
Write the following command :- printf "\033c"
And save it with the filename "cls" , in the bin directory, ensure that there is NO EXTENSION to the file, thus it will have the type "file" ..save it,open the shell and the command will run...
I found this method by playing around with msys and cygwin, I have both on my windows PC and turns out msys supports the "cls" command, I found a file with the same name in it's bin directory and copied it to the cygwin's bin folder and edited the file with the printf function and this worked like a charm...hope this helps.

Resources