cd into folders breaks/hangs zsh for minutes using Windows cmd, Powershell, Hyper, and Bash (Ubuntu) - bash

To clarify which I've tried:
Ubuntu bash
Hyper
Powershell
Git bash
cmd
I have installed Ubuntu bash on my Windows 10 PC. When I open ubuntu.exe up and I type zsh after the shell has started, my zsh shell starts up instead, as expected. This is perfectly fine and it does exactly what I want.
The problem: When I do the following sequences:
cd
[TAB]
[ARROW KEYS] (select an item)
ENTER
It should cd into that folder. And it does that, but it literally takes over a minute to do so. CTRL+C to cancel does not work.
Now that I'm in that folder, I can clear the console by typing clear, so that's what I do:
user#user-pc > /mnt/c/folder/otherfolder > GIT-REPO > clear
When I type clear and hit enter, it takes probably 30+ seconds before it clears the window.
I have tried a bunch of themes and a bunch of terminals/command prompts, but none of them work, not even the default one.
I've installed zsh and oh-my-zsh by doing these commands inside the Ubuntu bash shell:
sudo apt-get install zsh
curl -L https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh | bash
Any idea why this odd behavior happens? It looks like it's zsh, because I can easily cd into other folders using all of the above terminals. The problem also happens if I simply cd /mnt/c/folder/otherfolder/ with and without the trailing slash.

Does it only happen in that particular folder? IME, large git repositories can be really slow on zsh+WSL even with the default prompt.

Related

basic shell commands extremely slow on Git-Bash, sh.exe but fine on Cygwin

When I run basic commands like pwd and cd the command itself executes fast but the console hangs for 1 second before allowing me to execute another command.
I got the latest Git Bash portable and tried
32- and 64-bit
Run as admin
sh.exe instead of git-bash.exe (and Run as admin)
But Cygwin does not have this problem.
In Cygwin, running pwd from the same directory as any Git Bash variant results in equally fast command completion but also there is no console hanging.
My Windows is: Version 10.0.19044 Build 19044
I have nVidia Quadro P3000
UPDATE from comments below:
It appears to be an issue with my Git installation but I chose the defaults so I don't know what it could be.
When I execute PS1='$ ' in Git-Bash, I do not have the the 1-second pause after each command is executed.
UPDATE from comments below
$ echo ${PS1#A}
declare -x PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u#\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$ '
I did not change anything.
I download a version of portable Git For Windows, launch as admin and type ls, pwd, etc.
I went back to 2.24 but same behavior.
I'm also going to try it on my personal PC since it could be my corporate antivirus that's causing this.
UPDATE
The issue is __git_ps1 and there's an open issue
I think that __git_ps1 is the culprit.
As a test, put following code in /tmp/experiment.sh
if [[ "$(type -t __git_ps1)" == 'function' ]]; then
cd(){
builtin cd "$#"
__GIT_PS1=$('__git_ps1') # Calling original __git_ps1, only when changing directory.
}
__git_ps1_stub(){
echo "$__GIT_PS1" # Now $PS1 will do this echo, instead of calling __git_ps1
}
alias __git_ps1=__git_ps1_stub # __git_ps1_stub will be called in $PS1
cd . # Initialize $__GIT_PS1 for the first time.
fi
Then start a git-bash terminal, and run source /tmp/experiment.sh
If situation improves, you can put the code in ~/.bashrc
You'll need to change other commands (like pushd, popd, etc) if you use them to change directory.
Try first to simplify your PATH when testing your Git bash.
In a CMD, type
set "GH=C:\Program Files\Git"
set "PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0"
set "PATH=%GH%\bin;%GH%\cmd;%GH%\usr\bin;%GH%\mingw64\bin;%GH%\mingw64\libexec\git-core;%PATH%"
Then try again and type bash to enter a shell session.
By default, I get:
$ echo $PS1
\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u#\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$
And it is quite fast.
(Microsoft Windows 10.0.19044.1586, git version 2.35.1.windows.2)
The problem may be related to slow resolution of computer network names. Since the computer name is involved in the command line. I advise you to add yourhostname and localhost to etc/hostname.
For me it was corrupt page file. Try clearing Windows page file and rebooting. Page file was constantly causing my git bash to hang for 20 seconds with just carriage returns. I've re-enabled page file several times and eventually it happens again. Clearing page file fixed it every time.
I turn page file completely off now and git bash is screaming fast.

Preserving PS1 while in SSH with GitBash?

I have a complicated prompt. Very. I use git-bash for windows.
SSH-ing in on gitbash sends me to the CMD prompt. I know to type ssh -t user#host "bash -l" to get a bash prompt. It works, however, I use a repository called gitstatus to speed up the parsing of the git commands, and sshing in using bash -l calls the PS1 set in /c/cygwin/etc/bash.bashrc (for Mintty), NOT ~/bash.bashrc (for GitBash). This initially seems fine, as I can just copy paste the code from the GitBash *.bashrc to cygwin's. However, the gitstatus repository only works on bash terminals, aka not Cygwin/Mintty, so the prompt when I ssh in appears quite slower in git repos (Mintty doesn't allow gitstatus to be sourced and speed up parsing, once again) than if I was not SSH-ed in and using the GitBash-sourced prompt.
Are there any work arounds for this? I have seen many simnilar questions here but none have provided me a solution.
If you're in my situation, just source the scripts (duh). If you can't execute foo in file x when it should do the same thing as bar in file y, just source bar from file x. It seems obvious but took me a moment.
ALSO: Make sure to run dos2unix.exe on admin command prompt to remove carriage returns every time you make a change.

alias in .bashrc using Mac OSX: command works on the command line but in an alias it doesn't work

I realise there are a few questions similar to this but nothing that fully addresses the issue I'm getting.
I'm writing a few alias' for terminal commands I often use.
$ cd ~/Documents/blah/blah/blah
This works as a terminal command, but when I try to turn this into an alias:
alias pie='cd ~/Documents/blah/blah/blah'
This doesn't work, -bash: cd: Documents/blah/blah/blah: No such file or directory
I'm storing the alias in .bashrc in my home directory. I'm using Mac OSX.
Anyone know why it would work as a straight command but not an alias?
Similarly, I use a vagrant machine, and I'd like the chain together two commands: one to ssh to vagrant and the other to open the shell:
alias vshell='vagrant ssh && python manage.py shell
But this doesn't seem possible, it only ever executes the first command, maybe because this takes a little longer to complete?
Thanks

Can't Get Shell Scripts to Run

I recently reinstalled Mojave on my Macbook Pro. Before the install, I had several bash scripts written, all of which were executable by double-clicking on them. They are all .command extensions. For each file, I ran chmod +x to make it executable. The shebang in each of the scripts is: #! /usr/bin/env bash
After the install, I cannot get the scripts to run by double-clicking. If I access the scripts through terminal, they run fine. But if I double-click, it opens a terminal window with the title of the script, but nothing happens, the script never executes.
I installed bash through homebrew, then entered /usr/local/bin/bash into /etc/shells. In terminal, if I enter which bash it returns /usr/local/bin/bash. If I enter echo $SHELL it returns /bin/bash.
I want my system to be running the bash 5 that homebrew installed. I thought I had my terminal configured correctly, but I can't figure this out. Any help would be extremely appreciated.
I attached a screenshot of the terminal output too in case it would be helpful. I wrote a script for an example titled "hello.command":
#! /usr/bin/env bash
echo 'Hello'
This is what shows up if I double click it:
In /etc/shells, add an entry on a newline:
/usr/local/bin/bash
Note that to add this entry to /etc/shells, you need root privileges.

WSL hide /mnt/c/Users/

It is possible to view shorter path in my terminal (VS Code & Hyper) with WSL (Ubuntu). On top of the Ubuntu, I have installed zsh. Currently, I am using a git bash and path looks Lukas#Y50-70 ~/Coding but with the Ubuntu, I have something like this lukas#Y50-70 /mnt/c/Users/Lukas/Coding. When I have a project in another 2 folders or so and I have a long branch name it is annoying to have a full row unnecessary info (for me).
Here is a comparison of Ubuntu and git bash:
Thanks
I was able to solve this using Named Directories - by adding this line to your ~/.zshrc file
hash -d c=/mnt/c
you will see '~c/' in your prompt rather than '/mnt/c/' which I think is a lot nicer.
This has a similar effect to setting an alias for the directory but the name is reflected in how your path is displayed.
As an added bonus you can then switch to that directory at any time by typing ~c
Check if the zsh installation guide under WSL can help (from neurogenesis):
Install zsh with sudo apt-get install zsh
bash.exe is the entrypoint to the WSL / linux subsystem. You'll have to modify the windows shortcut to specify bash -c --login or modify ~/.bashrc with exec /bin/zsh to properly load a different shell.
/etc/passwd isn't consulted because it's not a full login process. Be sure to set your SHELL env var as well. See #846 for details.
Fix your umask before you start installing things, otherwise tools like zsh will complain.
Specifically, "group" and "other" permissions will have the same privileges that owner do. This causes zsh's compaudit and compinit to fail (both are related to command completion).
See #352 for details. umask 022 can be added to your ~/.bashrc.
NOTE: This should be done before trying to install zsh plugin managers like antigen (otherwise the directory/file permissions issues from git clones).
You should also do this before installing RVM or rbenv.
I ended up inserting a few lines to the top of my ~/.bashrc, something like the following:
# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples
## Fix missing umask value
umask 022
## Launch Zsh
if [ -t 1 ]; then
cd $HOME
export SHELL=/bin/zsh
exec -cl $SHELL
fi
Issue 846 (mentioned in point 2) includes the comment:
A normal -c zsh symlink opened up in the wrong directory to me, but I managed using this (note the tilde):
C:\Windows\System32\bash.exe ~ -c /bin/zsh
See also "How to Use Zsh (or Another Shell) in Windows 10".
I know this isn't exactly the fix you were hoping for. I was looking to solve the same issue. The prompt was just too long and it was causing some of my commands to wrap to the next line. After seeing the comments on VonC's answer, I'm deciding to keep my next-best solution.
What I did in my ~/.bashrc file is this:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u#\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\n\$ '
else
PS1='${debian_chroot:+($debian_chroot)}\u#\h:\w\n\$ '
fi
I added a \n right before the \$
So when I'm at my Windows home folder, it looks like this
ryan#DESKTOP-RSKAA4F:/mnt/c/Users/ryank
$
And I start typing my commands after the $. It takes up more vertical space, but at least I don't have to maximize my terminal window just to avoid text wrap.
It appears to me that just running 'cd' after starting the terminal session, re-bases the prompt to the normal '/home/(user)'
there should be no need for installing zsh or anything else. It works for me anyway.
also when starting the session at the root folder from windows, seems to do the trick.
I keep my sessions under
C:\vms
sample:
cd -d C:\vms\minikube\ubu_jenkinsX\rootfs
C:\vms\minikube\ubu_jenkinsX\rootfs>wsl -d ubu_jenkinsX
Yours may be under your userprofile in local data. Search for the rootfs folder
The reason your WSL prompt shows such a long path is because you're not actually in your home directory. You see, WSL has its own virtual filesystem separate from Windows, and Windows paths (like your C:\Users\Lukas\...) are stored under /mnt/c/Users/Lukas/.... Your WSL home directory would be /home/Lukas (since your WSL username is capitalized), but of course, that's not where your project is.
The fish shell has a prompt_pwd function that shortens a path to something like this:
0 ---- /m/c/U/L/Documents cd Something
0 ---- /m/c/U/L/D/Something prompt_pwd
/m/c/U/L/D/Something
Is that something you're interested in? You could port the function to Bash, or just switch to Fish, or just display the current directory name instead of path.

Resources