wez / wezterm

A GPU-accelerated cross-platform terminal emulator and multiplexer written by @wez and implemented in Rust
https://wezfurlong.org/wezterm/
Other
17.26k stars 781 forks source link

New tab doesn't open in previous directory (zimrc, title formating bug) #4427

Closed jacksongoode closed 11 months ago

jacksongoode commented 1 year ago

What Operating System(s) are you seeing this problem on?

macOS

Which Wayland compositor or X11 Window manager(s) are you using?

No response

WezTerm version

wezterm 20230712-072601-f4abf8fd

Did you try the latest nightly build to see if the issue is better (or worse!) than your current version?

No, and I'll explain why below

Describe the bug

Opening a new tab/pane from a directory does not open that tab in the same working directory. However, opening a new tab from the original pane a second time does open the second tab instance in the working directory.

However, calling set-working-directory before launching a new tab does carry the directory over.

To Reproduce

Configuration

   1   │ local wezterm = require 'wezterm'
   2   │ local config = {}
   3   │
   4   │ wezterm.on('gui-startup', function(cmd)
   5   │     local mux = wezterm.mux
   6   │
   7   │     wezterm.mux.spawn_window {
   8   │         width = 106,
   9   │         height = 60,
  11   │         position = {
  12   │             x = 2400,
  13   │             y = 0
  14   │         }
  15   │     }
  16   │ end)
  17   │
  18   │ config = {
  19   │     front_end = 'WebGpu',
  20   │     font = wezterm.font 'MesloLGS Nerd Font Mono',
  21   │     window_background_opacity = 0.9,
  22   │     macos_window_background_blur = 16,
  23   │     window_decorations = 'RESIZE|MACOS_FORCE_ENABLE_SHADOW',
  24   │     enable_scroll_bar = true,
  25   │     adjust_window_size_when_changing_font_size = false,
  26   │     window_close_confirmation = 'NeverPrompt',
  27   │     default_cursor_style = 'SteadyBar',
  28   │     use_fancy_tab_bar = true,
  29   │     hide_tab_bar_if_only_one_tab = true,
  30   │     show_new_tab_button_in_tab_bar = false,
  31   │     adjust_window_size_when_changing_font_size = false,
  32   │     color_scheme = 'Monokai Pro Spectrum',
  33   │
  34   │     keys = {
  35   │         {
  36   │             key = 'd',
  37   │             mods = 'CMD',
  38   │             action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' },
  39   │         },
  40   │         {
  41   │             key = 'd',
  42   │             mods = 'CMD|SHIFT',
  43   │             action = wezterm.action.SplitHorizontal { domain = 'CurrentPaneDomain' },
  44   │         },
  45   │         {
  46   │             key = 'w',
  47   │             mods = 'CMD',
  48   │             action = wezterm.action.CloseCurrentPane { confirm = false },
  49   │         },
  50   │
  51   │     }
  52   │ }
  53   │
  54   │ return config

Here is my zshrc:

# Start configuration added by Zim install {{{
#
# User configuration sourced by interactive shells
#

# -----------------
# Zsh configuration
# -----------------

#
# History
#

# Remove older command from the history if a duplicate is to be added.
setopt HIST_IGNORE_ALL_DUPS

#
# Input/output
#

# Set editor default keymap to emacs (`-e`) or vi (`-v`)
bindkey -e

# Prompt for spelling correction of commands.
#setopt CORRECT

# Customize spelling correction prompt.
#SPROMPT='zsh: correct %F{red}%R%f to %F{green}%r%f [nyae]? '

# Remove path separator from WORDCHARS.
WORDCHARS=${WORDCHARS//[\/]}

# -----------------
# Zim configuration
# -----------------

# Use degit instead of git as the default tool to install and update modules.
#zstyle ':zim:zmodule' use 'degit'

# --------------------
# Module configuration
# --------------------

#
# git
#

# Set a custom prefix for the generated aliases. The default prefix is 'G'.
zstyle ':zim:git' aliases-prefix 'g'

#
# input
#

# Append `../` to your input for each `.` you type after an initial `..`
#zstyle ':zim:input' double-dot-expand yes

#
# termtitle
#

# Set a custom terminal title format using prompt expansion escape sequences.
# See http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Simple-Prompt-Escapes
# If none is provided, the default '%n@%m: %~' is used.
#zstyle ':zim:termtitle' format '%1~'

#
# zsh-autosuggestions
#

# Disable automatic widget re-binding on each precmd. This can be set when
# zsh-users/zsh-autosuggestions is the last module in your ~/.zimrc.
ZSH_AUTOSUGGEST_MANUAL_REBIND=1

# Customize the style that the suggestions are shown with.
# See https://github.com/zsh-users/zsh-autosuggestions/blob/master/README.md#suggestion-highlight-style
#ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=242'

#
# zsh-syntax-highlighting
#

# Set what highlighters will be used.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)

# Customize the main highlighter styles.
# See https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/main.md#how-to-tweak-it
#typeset -A ZSH_HIGHLIGHT_STYLES
#ZSH_HIGHLIGHT_STYLES[comment]='fg=242'

# ------------------
# Initialize modules
# ------------------

ZIM_HOME=${ZDOTDIR:-${HOME}}/.zim
# Download zimfw plugin manager if missing.
if [[ ! -e ${ZIM_HOME}/zimfw.zsh ]]; then
  if (( ${+commands[curl]} )); then
    curl -fsSL --create-dirs -o ${ZIM_HOME}/zimfw.zsh \
        https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
  else
    mkdir -p ${ZIM_HOME} && wget -nv -O ${ZIM_HOME}/zimfw.zsh \
        https://github.com/zimfw/zimfw/releases/latest/download/zimfw.zsh
  fi
fi
# Install missing modules, and update ${ZIM_HOME}/init.zsh if missing or outdated.
if [[ ! ${ZIM_HOME}/init.zsh -nt ${ZDOTDIR:-${HOME}}/.zimrc ]]; then
  source ${ZIM_HOME}/zimfw.zsh init -q
fi
# Initialize modules.
source ${ZIM_HOME}/init.zsh

# ------------------------------
# Post-init module configuration
# ------------------------------

#
# zsh-history-substring-search
#

zmodload -F zsh/terminfo +p:terminfo
# Bind ^[[A/^[[B manually so up/down works both before and after zle-line-init
for key ('^[[A' '^P' ${terminfo[kcuu1]}) bindkey ${key} history-substring-search-up
for key ('^[[B' '^N' ${terminfo[kcud1]}) bindkey ${key} history-substring-search-down
for key ('k') bindkey -M vicmd ${key} history-substring-search-up
for key ('j') bindkey -M vicmd ${key} history-substring-search-down
unset key
# }}} End configuration added by Zim install

# --- Exports ---

# brew
export HOMEBREW_NO_ENV_HINTS=1
export HOMEBREW_NO_AUTO_UPDATE=1

# cat
export MANPAGER="sh -c 'col -bx | bat -l man -p'"

# virtualenv
export PYENV_VIRTUALENV_DISABLE_PROMPT=1

# gpg
export GPG_TTY=$(tty)

# pyenv
export PYENV_ROOT="$HOME/.pyenv"

# Go
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

# kubeconfig
export KUBECONFIG=$HOME/.kube/config

# McFly
export MCFLY_FUZZY=3
export MCFLY_RESULTS=50
export MCFLY_DISABLE_MENU=TRUE
export MCFLY_HISTORY_LIMIT=10000

# The next line updates PATH for the Google Cloud SDK.
if [ -f '/Users/jackson/google-cloud-sdk/path.zsh.inc' ]; then
    . '/Users/jackson/google-cloud-sdk/path.zsh.inc'
fi

# --- Evals ---

# brew
eval "$(/opt/homebrew/bin/brew shellenv)"

# pyenv
# eval "$(pyenv init --path)"
command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

# rbenv
# eval "$(rbenv init - zsh)"

# McFly
eval "$(mcfly init zsh)"

# export NVM_DIR="$HOME/.nvm"
# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

# bun completions
[ -s "/Users/jackson/.bun/_bun" ] && source "/Users/jackson/.bun/_bun"

# bun
export BUN_INSTALL="$HOME/.bun"
export PATH="$BUN_INSTALL/bin:$PATH"

# Binds
bindkey "^[[1;3C" forward-word
bindkey "^[[1;3D" backward-word

Expected Behavior

New directory should match previous

Logs

No response

Anything else?

No response

marconetto commented 1 year ago

I had the same issue and I tried to create zsh and wezterm config files as minimum as possible to replicate the issue and simplify the debugging.

Unfortunately I don't know the answer but hope these files help.

Indeed, the right directory shows up if you create a pane (or tab), destroy it, and create it again.

wezterm.lua:

local wezterm = require 'wezterm'
local config = {}

config.keys = {
    {
        key = 'd',
        mods = 'CMD',
        action = wezterm.action.SplitHorizontal { domain =
        'CurrentPaneDomain' },
    },
    { key = 't', mods = 'CMD', action = wezterm.action.SpawnTab 'DefaultDomain' },
}

return config

zsh file a) if I uncomment those 'return' in set-title-precmd() and set-title-preexec(), I get the right behavior b) If I comment install_zsh_plugin "zsh-autosuggestions", it also works fine

function install_zsh_plugin(){
    GITHUBZSH=https://github.com/zsh-users
    plugname=$1
    PLUGDIR=$HOME/.zsh/$1
    [[ ! -d "$PLUGDIR" ]] && \
       git clone "$GITHUBZSH/$plugname" $PLUGDIR
    source $PLUGDIR/$plugname.zsh
}
function set-title-precmd() {
 #  return
  printf "\e]2;%s\a" "mytitle"
}

function set-title-preexec() {
  # return
  printf "\e]2;%s\a" "mytitle"
}

install_zsh_plugin "zsh-autosuggestions"
autoload -Uz add-zsh-hook

add-zsh-hook preexec set-title-preexec
add-zsh-hook precmd set-title-precmd
jacksongoode commented 12 months ago

@marconetto Thank you for the reply I'll try out your method!

marconetto commented 12 months ago

Hi @jacksongoode it would be nice indeed if you could check if these modifications solve the issue. At least we would help narrow down a simple case to replicate the problem.

jacksongoode commented 12 months ago

Hmm, so I don't have anything changing the tab title. My zimfw config does enable the https://github.com/zsh-users/zsh-autosuggestions plugin. But disabling it doesn't appear to fix the issue from my end. Getting rid of zim as the module manager does resolve this, so I'm guessing some module is being loaded. Here is my .zimrc (which should nearly be the default):

# Start configuration added by Zim install {{{
#
# This is not sourced during shell startup, and it's only used to configure the
# zimfw plugin manager.
#

#
# Modules
#

# Sets sane Zsh built-in environment options.
zmodule environment
# Provides handy git aliases and functions.
zmodule git
# Applies correct bindkeys for input events.
zmodule input
# Sets a custom terminal title.
zmodule termtitle
# Utility aliases and functions. Adds colour to ls, grep and less.
zmodule utility
# z jump
zmodule https://github.com/agkozak/zsh-z
# pyenv
zmodule pvenv
# Ruby
zmodule ruby

#
# Prompt
#

# Current dir
zmodule prompt-pwd
# Exposes to prompts how long the last command took to execute, used by asciiship.
zmodule duration-info
# Exposes git repository status information to prompts, used by asciiship.
zmodule git-info
# Theme
zmodule bira

#
# Completion
#

# Additional completion definitions for Zsh.
zmodule zsh-users/zsh-completions --fpath src
# Enables and configures smart and extensive tab completion.
# completion must be sourced after all modules that add completion definitions.
zmodule completion

#
# Modules that must be initialized last
#

# Fish-like syntax highlighting for Zsh.
# zsh-users/zsh-syntax-highlighting must be sourced after completion
zmodule zsh-users/zsh-syntax-highlighting
# Fish-like history search (up arrow) for Zsh.
# zsh-users/zsh-history-substring-search must be sourced after zsh-users/zsh-syntax-highlighting
zmodule zsh-users/zsh-history-substring-search
# Fish-like autosuggestions for Zsh.
zmodule zsh-users/zsh-autosuggestions
# }}} End configuration added by Zim install
jacksongoode commented 12 months ago

Ah @marconetto I just realized that:

# Sets a custom terminal title.
zmodule termtitle

Is causing the conflict which probably has the same interaction as your title formatting does. Disabling this resolves the issue for me. This is set by default in zimrc.

marconetto commented 12 months ago

great @jacksongoode!

perhaps wezterm makes use of the current title to define the current work directory when opening a new tab/pane... and changing the title via zsh is generating the problem. Let's see if wezterm core developers have any input here.

marconetto commented 11 months ago

Hi @jacksongoode

I was doing more research on the issue and I found this related one:

https://github.com/wez/wezterm/discussions/3718

The snippet code showed by Tyler solved the issue for me without making any removal of my current zsh configuration: https://github.com/wez/wezterm/discussions/3718#discussioncomment-6032441

Additional references from that issue: https://wezfurlong.org/wezterm/config/lua/config/default_cwd.html https://github.com/yenaras/zsh/blob/main/osc7_escape_sequence

And: https://wezfurlong.org/wezterm/shell-integration.html

So, it seems it is not a bug of wezterm. I guess you may close the issue.

github-actions[bot] commented 10 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.