Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pane navigation broken within pipenv shell #230

Open
NoahRJohnson opened this issue Mar 1, 2019 · 16 comments
Open

Pane navigation broken within pipenv shell #230

NoahRJohnson opened this issue Mar 1, 2019 · 16 comments

Comments

@NoahRJohnson
Copy link

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?

@christoomey
Copy link
Owner

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 :)

@yevgenybulochnik
Copy link

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.

@thomasmatecki
Copy link

thomasmatecki commented Apr 20, 2019

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)?$'"

@rollue
Copy link

rollue commented May 10, 2019

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

@strongoose
Copy link

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.

@marcomayer
Copy link

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

@rollue
Copy link

rollue commented Nov 23, 2019

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

@marcomayer
Copy link

@mhoonjeon @ouroboros8 I ended using a version of #195 (comment) 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.

@roblevy
Copy link

roblevy commented Feb 9, 2021

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.

@dirn
Copy link

dirn commented Feb 13, 2021

There's some information in python-poetry/poetry#2792 (comment) about why the new terminals are created.

@madhukar93
Copy link

madhukar93 commented Jul 22, 2021

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

@christoomey
Copy link
Owner

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.

@mauricekraus
Copy link

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'

@gegnew
Copy link

gegnew commented Jun 6, 2023

@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/python@3.11/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'

@jakubbortlik
Copy link

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"'.

@noapoleon
Copy link

Hello!! I have found this solution that is really straight forward in a pypa/pipenv issue: pypa/pipenv#1407 (comment)

As suggested just adding "pipenv" to the regex of the is_vim function seems to work although it needs to be done on both the tmux and the vim/nvim plugin instances before it will work.

Never done a Pull Request before, I'll look into it tomorrow, in the meantime here's the fix

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests