vim-tmux-navigator icon indicating copy to clipboard operation
vim-tmux-navigator copied to clipboard

Pane navigation broken within pipenv shell

Open NoahRJohnson opened this issue 5 years ago • 16 comments

Hi Chris,

I'm a fan of this plugin. The only issue I've run into is when running vim inside of a pipenv shell.

os: Fedora 29 vim version: 8.1 tmux version 2.8

Steps to replicate:

  • Create or attach a tmux session, e.g. 'tmux new -s test'
  • Load a pipenv virtual environment via 'pipenv shell'
  • Open any two files 'vim -O file1 file2'
  • Observe Ctrl-h and Ctrl-l pane navigation doesn't work

:TmuxNavigatorProcessList returns " S bash S pipenv "

Is there any way to work around this?

NoahRJohnson avatar Mar 01 '19 20:03 NoahRJohnson

Hmm, this is interesting. I did a quick check locally (not a python user myself so I haven't run into this) and was able to reproduce the issue. Unfortunately, this seems to be the behavior of pipenv shell. I also checked if the older vim checking functionality using tmux display-message -p '#{pane_current_command}' would work, but it seems to suffer from the same odd process listing and "vim" is nowhere to be found.

I don't have much to offer in the way of suggestions as those are the two methods we've historically used to detect Vim being active don't seem to work in pipenv shell. If you're able to find an alternative approach that we could use, I'd be happy to dig in further, but for now I'm unfortunately stumped. I'll keep this issue open in the hope that some kind internet person might come along with a suggestion :)

christoomey avatar Mar 02 '19 21:03 christoomey

I have encountered the same issue, my current workaround is aliasing envact='source $(pipenv --venv)/bin/activate' in my .zshrc to activate the virtualenv vs using the subshell. You end up getting a caution warning from pipenv when installing dependencies, but otherwise seems to work fine. Also not sure what the consequences of activating the virtualenv this way vs using the subshell.

yevgenybulochnik avatar Mar 17 '19 23:03 yevgenybulochnik

I'm running into the same issue with an (oddly) almost identical setup as @NoahRJohnson.

Just to make sure I understand, the crux of the issue seems to be that pipenv shell changes the name of the terminal:

(tmux)$ tty
/dev/ttys000
(tmux)$ pipenv shell
Loading .env environment variables…
Launching subshell in virtual environment…
(virtual-env)$ tty
/dev/ttys003

I'm pretty novice with all of these tools, so in the hopes of inspiring someone else, would checking if vim is running with an approach similar to this work?

tty | xargs ps -o state= -o comm= -t | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

thomasmatecki avatar Apr 20 '19 20:04 thomasmatecki

Has anybody found fixes to this problem? I have just ran into the same problem after switching from pyenv-virtualenv to pipenv. :(

rollue avatar May 10 '19 14:05 rollue

A sort-of-workaround which may be obvious but didn't occur to me initially: you can run vim inside the virtual environment without using pipenv shell by running pipenv run vim ...

This works around this issue if you just want vim to run inside the virtualenv so that things like pylint will work and don't care about being in a pipenv shell when you exit vim.

strongoose avatar May 30 '19 13:05 strongoose

I got the same issue but couldn't find a workaround yet :/

marcomayer avatar Nov 17 '19 15:11 marcomayer

A sort-of-workaround which may be obvious but didn't occur to me initially: you can run vim inside the virtual environment without using pipenv shell by running pipenv run vim ...

This works around this issue if you just want vim to run inside the virtualenv so that things like pylint will work and don't care about being in a pipenv shell when you exit vim.

I can confirm this works for now. So I added zsh alias alias prn="pipenv run nvim" and use this every time I need nvim. @ouroboros8 @marcomayer

rollue avatar Nov 23 '19 11:11 rollue

@mhoonjeon @ouroboros8 I ended using a version of https://github.com/christoomey/vim-tmux-navigator/issues/195#issuecomment-555943814 that works for me. That basically uses "tmux set -a" with $TMUX_PANE to globally set a variable/list that then is used by tmux to figure out if vim is running on the active pane.

marcomayer avatar Nov 25 '19 10:11 marcomayer

I just want to confirm that this problem also happens with poetry shell (https://python-poetry.org/)

As for pipenv, instead of starting a full-on Poetry shell, I can do:

poetry run vim

and vim-tmux-navigator works again.

roblevy avatar Feb 09 '21 17:02 roblevy

There's some information in https://github.com/python-poetry/poetry/issues/2792#issuecomment-672730038 about why the new terminals are created.

dirn avatar Feb 13 '21 19:02 dirn

I just want to confirm that this problem also happens with poetry shell (https://python-poetry.org/)

As for pipenv, instead of starting a full-on Poetry shell, I can do:

poetry run vim

and vim-tmux-navigator works again.

this should be added to the README

madhukar93 avatar Jul 22 '21 09:07 madhukar93

Hey folks - I'm not a Python user myself so I don't feel entirely comfortable summing up all that's at play here, but if anyone wanted open a PR with a suggested update to the ## Troubleshooting section re: these work arounds, I'd be happy to work from that.

christoomey avatar Jul 24 '21 13:07 christoomey

Hey, I have successfully applied a workaround by filtering for poetry as well (this should work with pipenv in the same way). For those who want to try it, here is my tmux configuration.

is_vim="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
is_poetry="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?poetry$'"

bind -n C-h run "($is_vim && tmux send-keys C-h) || tmux select-pane -L"

bind -n C-j run "($is_vim && tmux send-keys C-j)  || ($is_poetry && tmux send-keys C-j) || tmux select-pane -D"

bind -n C-k run "($is_vim && tmux send-keys C-k) || ($is_poetry && tmux send-keys C-k)  || tmux select-pane -U"

bind -n C-l run  "($is_vim && tmux send-keys C-l) || tmux select-pane -R"

bind-key -n 'C-\\' if-shell "$is_vim" 'send-keys C-\\\\'  'select-pane -l'

mauricekraus avatar Nov 16 '22 21:11 mauricekraus

@mauricekraus I don't find that this works; can you elaborate?

Edit: I guess for me the Poetry shell shows up like:

S<+  /opt/homebrew/Cellar/[email protected]/3.11.3/Frameworks/Python.framework/Versions/3.11/Resources/Python.app/Contents/MacOS/Python

Edit:

Building on @mauricekraus, this works for me:

is_poetry="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE 'Frameworks\/Python.framework'"


bind -n C-h run "($is_vim && tmux send-keys C-h) || ($is_poetry && tmux send-keys C-h) || tmux select-pane -L"

bind -n C-j run "($is_vim && tmux send-keys C-j)  || ($is_poetry && tmux send-keys C-j) || tmux select-pane -D"

bind -n C-k run "($is_vim && tmux send-keys C-k) || ($is_poetry && tmux send-keys C-k)  || tmux select-pane -U"

bind -n C-l run  "($is_vim && tmux send-keys C-l) || ($is_poetry && tmux send-keys C-l) || tmux select-pane -R"

bind-key -n 'C-\\' if-shell "$is_vim" 'send-keys C-\\\\'  'select-pane -l'

gegnew avatar Jun 06 '23 10:06 gegnew

Hi guys, the reason why mauricekraus' workaround doesn't completely solve the issue for me is that when I activate the poetry shell in one tmux pane, navigation does not work in that pane unless a vim window (which could then process the C-j binding) is active in that pane. For the workaround to fulfills my needs, I had to add some bash mappings, and so my current settings are the following (notice that I use Alt+keys instead of Control+keys for pane/window navigation): My ~/.tmux.conf contains these lines:

is_vim="echo '#{pane_tty}' > ~/tty.log; ps -o state= -o comm= -t '#{pane_tty}' \
    | tee ~/ps.log | grep -iqE '^([^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?)$'"
is_poetry="ps -o state= -o comm= -t '#{pane_tty}' | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?poetry$'"

bind -n M-h run "($is_vim && tmux send-keys M-h) || ($is_poetry && tmux send-keys M-h) || tmux select-pane -L"
bind -n M-j run "($is_vim && tmux send-keys M-j) || ($is_poetry && tmux send-keys M-j) || tmux select-pane -D"
bind -n M-k run "($is_vim && tmux send-keys M-k) || ($is_poetry && tmux send-keys M-k)  || tmux select-pane -U"
bind -n M-l run  "($is_vim && tmux send-keys M-l) || ($is_poetry && tmux send-keys M-l) || tmux select-pane -R"
bind-key -n 'M-p' if-shell "$is_vim" 'send-keys M-p'  'select-pane -l'

One of my nvim configs contains these lines:

-- Navigation
vim.cmd [[let g:tmux_navigator_no_mappings = 1]]
vim.keymap.set({"n", "t"}, "<A-h>", "<cmd>TmuxNavigateLeft<cr>", { silent = true, desc = "Navigate left" })
vim.keymap.set({"n", "t"}, "<A-j>", "<cmd>TmuxNavigateDown<cr>", { silent = true, desc = "Navigate down" })
vim.keymap.set({"n", "t"}, "<A-k>", "<cmd>TmuxNavigateUp<cr>", { silent = true, desc = "Navigate up" })
vim.keymap.set({"n", "t"}, "<A-l>", "<cmd>TmuxNavigateRight<cr>", { silent = true, desc = "Navigate right" })
vim.keymap.set({"n", "t"}, "<A-p>", "<cmd>TmuxNavigatePrevious<cr>", { silent = true, desc = "Navigate to previous" })

And I've added the following key bindings to my ~/.bashrc:

bind -x '"\eh":"tmux select-pane -L"'
bind -x '"\ej":"tmux select-pane -D"'
bind -x '"\ek":"tmux select-pane -U"'
bind -x '"\el":"tmux select-pane -R"'
bind -x '"\ep":"tmux select-pane -l"'

I use alacritty as my terminal emulator and it interprets "M-" as if I pressed "Esc-". In case you use Control+keys, then you'd need to modify the bindings like this `bind -x '"\C-h":"tmux select-pane -L"'.

jakubbortlik avatar Jun 27 '23 09:06 jakubbortlik