Skip to content

Commit c21d3cf

Browse files
committed
Query vim cwd rather than store it
Allows :cd from within vim
1 parent fe16966 commit c21d3cf

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

tmux-vim

+9-5
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,12 @@ _vim_pane_id() {
108108
_tmux_fetch_or_store tmux_vim_pane "$@"
109109
}
110110

111-
_vim_last_dir() {
112-
_tmux_fetch_or_store tmux_vim_last_dir "$@"
111+
_vim_cwd() {
112+
local vim_pane_id=$( _vim_pane_id )
113+
local vim_pid=$( tmux lsp -F '#{pane_id}=#{pane_pid}' |\
114+
grep ^$vim_pane_id= | cut -d= -f2 )
115+
116+
lsof -p $vim_pid -a -d cwd -Fn | grep ^n | cut -c 2-
113117
}
114118

115119
# _vim_send_keys [keystrokes...]
@@ -308,7 +312,6 @@ _vim_start() {
308312
# 0:1.1: [100x88] [history 0/10000, 0 bytes] %2
309313
# ^^^^^ $tmux_vim_pane pane_id ^^
310314
_vim_pane_id $( $tmux lsp -a | grep ^$tmux_vim_pane: | grep -o '%[0-9]\+' )
311-
_vim_last_dir "$PWD"
312315

313316
# Swap the panes if necessary. tmux only does right/bottom
314317
[[ $do_swap == 1 ]] && $tmux swap-pane -D
@@ -332,7 +335,8 @@ tmux_vim() {
332335
# If we are now in a different directory than $TDIR, we want to make
333336
# vim switch to this directory temporarily before opening the files.
334337
# This obviates any relative path computations.
335-
[[ "$PWD" != "$( _vim_last_dir )" ]] && _vim_op cd "$PWD"
338+
local vim_cwd=$( _vim_cwd )
339+
[[ "$PWD" != "$vim_cwd" ]] && _vim_op cd "$PWD"
336340

337341
# Rather than :edit each file in turn, use :badd for all but the last
338342
# file, and then use :edit on that.
@@ -350,7 +354,7 @@ tmux_vim() {
350354

351355
_vim_op edit "$1"
352356

353-
[[ "$PWD" != "$( _vim_last_dir )" ]] && _vim_op cd -
357+
[[ "$PWD" != "$vim_cwd" ]] && _vim_op cd -
354358
fi
355359

356360
$tmux select-pane -t $( _vim_pane_id )

0 commit comments

Comments
 (0)