Terminal doesn't auto-cd when using zsh [CLOSED]

Questions, glitches, bugs and crashes
Post Reply
xxyxxyxyx1
Posts: 9
Joined: Sun Feb 24, 2019 5:28 pm

Terminal doesn't auto-cd when using zsh [CLOSED]

Post by xxyxxyxyx1 » Fri Nov 01, 2019 2:05 pm

When I have my login shell set to `/bin/bash`, the terminal will auto-cd to match the directory I have open in the file browser panel.

When I change it to `/bin/zsh`, there is no synchronization between the terminal and the file panels. The terminal and the file panels each seem to work normally otherwise.

I have tried disabling all my ohmyzsh plugins by putting the following in my .zshrc, but it does not change things:

```
elif [[ $TERM_PROGRAM = "Nimble_Commander" ]] ; then
plugins=()
```

User avatar
mike
Posts: 945
Joined: Thu Jul 16, 2015 5:35 am
Location: Exeter, UK

Re: Terminal doesn't auto-cd when using zsh

Post by mike » Sun Nov 10, 2019 10:40 am

Cannot reproduce the issue.
I've checked it on macOS 10.15.1 + NC 1.2.6 and it does synchronize a zsh shell with a NC's file panel.

yansha
Posts: 5
Joined: Mon Dec 13, 2021 4:31 pm

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by yansha » Mon Dec 13, 2021 4:35 pm

Same thing happens to me, I'm using oh-my-zsh and folder doesn't synchronize between file panel and terminal.

Is there a way to resolve this?

User avatar
mike
Posts: 945
Joined: Thu Jul 16, 2015 5:35 am
Location: Exeter, UK

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by mike » Mon Dec 13, 2021 9:35 pm

Running with logging enabled might be of use, e.g.:

Code: Select all

/Applications/Nimble\ Commander.app/Contents/MacOS/Nimble\ Commander -NCLogLevel trace
Entries marked with "[term]" are related to the terminal emulator.

yansha
Posts: 5
Joined: Mon Dec 13, 2021 4:31 pm

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by yansha » Wed Jan 05, 2022 12:12 am

Sorry for the long time to respond

When NC starts there's a set of [term] logs.
In the case of bash:

Code: Select all

[2022-01-05 02:04:03.448] [term] [info] [ShellTask.cpp:258] Starting a new shell: "/bin/bash"
[2022-01-05 02:04:03.448] [term] [info] [ShellTask.cpp:261] Initial work directory: /Users/myuser/
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:265] Environment:
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:267] 	LC_CTYPE = UTF-8
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:267] 	TERM = xterm-16color
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:267] 	TERM_PROGRAM = Nimble_Commander
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:275] posix_openpt(O_RDWR) returned 26 (master_fd)
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:298] ptsname: /dev/ttys003
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:306] slave_fd: 27
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:365] cwd_pipe: 28, 29
[2022-01-05 02:04:03.448] [term] [debug] [ShellTask.cpp:366] semaphore_pipe: 30, 31
[2022-01-05 02:04:03.449] [term] [debug] [ShellTask.cpp:383] fork() returned 49623
[2022-01-05 02:04:03.452] [term] [debug] [ShellTask.cpp:410] started a background thread 0x70000489c000
[2022-01-05 02:04:03.452] [term] [debug] [ShellTask.cpp:434] prompt_setup:  PROMPT_COMMAND='if [ $$ -eq 49623 ]; then pwd>&20; read sema <&21; fi'
In the case of zsh:

Code: Select all

[2022-01-05 02:05:45.239] [term] [info] [ShellTask.cpp:258] Starting a new shell: "/bin/zsh"
[2022-01-05 02:05:45.239] [term] [info] [ShellTask.cpp:261] Initial work directory: /Users/myuser/
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:265] Environment:
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:267] 	LC_CTYPE = UTF-8
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:267] 	TERM = xterm-16color
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:267] 	TERM_PROGRAM = Nimble_Commander
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:275] posix_openpt(O_RDWR) returned 26 (master_fd)
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:298] ptsname: /dev/ttys003
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:306] slave_fd: 27
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:365] cwd_pipe: 28, 29
[2022-01-05 02:05:45.239] [term] [debug] [ShellTask.cpp:366] semaphore_pipe: 30, 31
[2022-01-05 02:05:45.240] [term] [debug] [ShellTask.cpp:383] fork() returned 49763
[2022-01-05 02:05:45.243] [term] [debug] [ShellTask.cpp:410] started a background thread 0x70000e066000
[2022-01-05 02:05:45.243] [term] [debug] [ShellTask.cpp:434] prompt_setup:  precmd(){ if [ $$ -eq 49763 ]; then pwd>&20; read sema <&21; fi; }
I see only a single difference in the last log:
bash:

Code: Select all

prompt_setup:  PROMPT_COMMAND='if [ $$ -eq 49623 ]; then pwd>&20; read sema <&21; fi'
zsh:

Code: Select all

prompt_setup:  precmd(){ if [ $$ -eq 49763 ]; then pwd>&20; read sema <&21; fi;
Then later on:

With bash, when switching between panes this is what I get in the logs:

Code: Select all

[2022-01-05 01:57:50.324] [panel] [trace] [CursorBackup.mm:11] Saving cursor position: 0
[2022-01-05 01:57:50.324] [panel] [trace] [CursorBackup.mm:11] Saving cursor position: -1
[2022-01-05 01:57:50.324] [panel] [trace] [CursorBackup.mm:24] Restored cursor position: -1
[2022-01-05 01:57:50.329] [panel] [info] [PanelData.mm:111] Loading Directory listing, 11 entries, /Users/Guest/
[2022-01-05 01:57:50.329] [panel] [trace] [CursorBackup.mm:11] Saving cursor position: 0
[2022-01-05 01:57:50.793] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Users/myuser/

... this is the point where startup logs when running NC stop, next logs when I actively switch between panes

[2022-01-05 01:58:02.004] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Users/myuser/
[2022-01-05 01:58:02.009] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Library/Fonts/
[2022-01-05 01:58:11.025] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Library/Fonts/
[2022-01-05 01:58:11.032] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Users/Guest/
[2022-01-05 01:58:13.116] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Users/Guest/
[2022-01-05 01:58:13.121] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Library/Fonts/
[2022-01-05 01:58:14.947] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Library/Fonts/
[2022-01-05 01:58:14.951] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49408: /Users/Guest/
With zsh nothing show in the logs after the startup logs when switching panes.

Code: Select all

[2022-01-05 01:59:49.708] [panel] [trace] [CursorBackup.mm:11] Saving cursor position: 0
[2022-01-05 01:59:49.708] [panel] [trace] [CursorBackup.mm:11] Saving cursor position: -1
[2022-01-05 01:59:49.708] [panel] [trace] [CursorBackup.mm:24] Restored cursor position: -1
[2022-01-05 01:59:49.715] [panel] [info] [PanelData.mm:111] Loading Directory listing, 11 entries, /Users/Guest/
[2022-01-05 01:59:49.716] [panel] [trace] [CursorBackup.mm:11] Saving cursor position: 0
[2022-01-05 01:59:50.085] [term] [info] [ShellTask.cpp:568] pwd prompt from shell_pid=49512: /Users/myuser/

yansha
Posts: 5
Joined: Mon Dec 13, 2021 4:31 pm

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by yansha » Fri Jan 07, 2022 12:07 am

Made some progress ...

Once I understood this had to do with the precmd() I started eliminating parts of my oh-my-zsh prompt.
What breaks nimble commander is the 'prompt_git', and specifically running git to build the prompt.

I couldn't figure out how to avoid git execution in prompt breaking nimble commander auto-cd, so the only workaround for now is to disable git prompt.

Is there a way to tell from .zshrc whether its running under nimble commander?
If that's possible it would be possible to remove git only in zsh under nimble commander.

But best solution would be to avoid the conflict with git execution as part of the prompt.
Any ideas?

yansha
Posts: 5
Joined: Mon Dec 13, 2021 4:31 pm

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by yansha » Fri Jan 07, 2022 7:02 pm

Last update

to identify zsh is running inside nimble commander I added to .zshrc:

Code: Select all

if [ "$__CFBundleIdentifier" = 'info.filesmanager.Files' ]; then
  export nimble_commander=1
fi
And then in zsh theme (I'm using angoster.zsh-theme) I modified the prompt_git() as follows:

Code: Select all

# Git: branch/detached head, dirty status
prompt_git() {
  if [[ -v nimble_commander ]]; then
    return
  fi
Still the best would be to get git in prompt supported somehow.

User avatar
mike
Posts: 945
Joined: Thu Jul 16, 2015 5:35 am
Location: Exeter, UK

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by mike » Sat Jan 08, 2022 7:27 pm

Hi yansha,

Your observations are correct.
Nimble Commander uses PROMPT_COMMAND/precmd() to get a notification about a new command prompt and from there it deduces if CWD was in fact changed.

It might be more straightforward to use $TERM_PROGRAM to find if a shell runs under NC:

Code: Select all

migun@Michaels-MBP-13-2020 Temp % env | grep TERM_PROGRAM
TERM_PROGRAM=Nimble_Commander
Frankly I don't understand what prompt_git does that breaks the NC integration.
This line is essentially 3 statements:

Code: Select all

precmd(){ if [ $$ -eq 49763 ]; then pwd>&20; read sema <&21; fi;
1) check that current PID is a pid of the shell itself, to exclude e.g. children/forks.
2) write the current PWD to FD 20 which is a write end of a pipe connected to NC.
3) wait for an acknowledgment from NC by reading from FD 21 which is a read end of a pipe connected to NC.

Is it even executed or prompt_git overwrites this function?

This code can be composed and injected into ZSH's environment manually, it should work equally.

yansha
Posts: 5
Joined: Mon Dec 13, 2021 4:31 pm

Re: Terminal doesn't auto-cd when using zsh [CLOSED]

Post by yansha » Sat Jan 08, 2022 10:49 pm

This is the full theme script from oh-my-zsh:

https://github.com/ohmyzsh/ohmyzsh/blob ... .zsh-theme

It's enough to use prompt_git and have it only run git status and return (as below) to stop nimble commander auto-cd from working.

Code: Select all

prompt_git() {
  git status
  return

...
I found an easier way to reproduce this:

Code: Select all

f1() {
    pwd
    echo \>\>
}

f2() {
    git status
    pwd
    echo \>\>
}

# auto-cd continues to work
PROMPT='$(f1)'

# auto-cd stops working
# PROMPT='$(f2)'
But there's a new twist now - after playing now some more with this auto-cd functionality, I see that it's not reliable.
Even w/o the git thing, sometimes it stops working, and then starts working again, etc.
From what it seems to me, after I issue some command in the terminal, then I switch back and forth between the file panels (4-6 switches) it stops working, then sometimes if I issue yet another command in the terminal it works again for another 4-6 switches.

So could be there is some other bug with zsh/oh-my-zsh that makes it more sensitive and it fails to work and git execution just happens to trigger this in some way.

Post Reply