christoomey / vim-tmux-navigator

Seamless navigation between tmux panes and vim splits
MIT License
5.24k stars 322 forks source link

Pane navigation broken within pipenv shell #230

Open NoahRJohnson opened 5 years ago

NoahRJohnson commented 5 years ago

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:

:TmuxNavigatorProcessList returns " S bash S pipenv "

Is there any way to work around this?

christoomey commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

strongoose commented 5 years ago

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 commented 4 years ago

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

rollue commented 4 years ago

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 commented 4 years ago

@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.

roblevy commented 3 years ago

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 commented 3 years ago

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

madhukar93 commented 3 years ago

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 commented 3 years ago

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 commented 1 year ago

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 commented 1 year ago

@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 commented 1 year ago

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 commented 3 months ago

Hello!! I have found this solution that is really straight forward in a pypa/pipenv issue: https://github.com/pypa/pipenv/issues/1407#issuecomment-395833951

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