christoomey / vim-tmux-navigator

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

Tmux -> tmux or tmux -> vim not working #234

Open mefp opened 5 years ago

mefp commented 5 years ago

I'm having what seems to be the opposite problem to some other issues here. If inside of vim, I can use the key bindings to navigate to a tmux pane. However, if I try to get back into vim or to another tmux pane, nothing works.

OS: Mac OS 10.14.5 (Mojave) Terminal App v2.9.5 echo $TERM output in tmux session = screen-256color Tmux 2.9a Vim 8.1.950

~/.tmux.conf

set -g mouse on

# remap prefix from 'C-b' to 'C-a'
unbind C-b
set-option -g prefix C-a
bind-key C-a send-prefix

# split panes using | and -
bind \ split-window -h
bind - split-window -v
unbind '"'
unbind %

# reload config file (change file location to your the tmux.conf you want to use)
bind r source-file ~/.tmux.conf

# don't rename windows automatically
set-option -g allow-rename off

# -------------
#  COLORS
# -------------
set -g default-terminal "screen-256color"
# set -g status-bg colour240
# set -g status-fg white

# -------------
#  PLUGINS - keep at bottom
# -------------
# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
set -g @plugin 'christoomey/vim-tmux-navigator'
run '~/.tmux/plugins/tpm/tpm'

# Other examples:
# set -g @plugin 'github_username/plugin_name'
# set -g @plugin 'git@github.com/user/plugin'
# set -g @plugin 'git@bitbucket.com/user/plugin'

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run -b '~/.tmux/plugins/tpm/tpm'

~/.vimrc

" ----------------
"  GENERAL
"  ---------------
set nocompatible            " be IMproved, required by Vundle
filetype off                " required by Vundle 
syntax on       
set hidden                  " allows for use of split windows
set wildmenu                " Better command-line completion
set showcmd                 " Show partial commands in the last line of the screen
set ignorecase              " Use case insensitive search... 
set smartcase               " ...except when using capital letters
set backspace=indent,eol,start
set autoindent
set ruler
set laststatus=2            " Always display the status line
set visualbell              " Use visual bell instead of beeping...
set t_vb=                   " ...but no flash or beep
set mouse=a                 " Enable use of the mouse for all modes
set cmdheight=2             " Set the command window height to 2 lines
set number                  " Display line numbers on the left
set notimeout ttimeout ttimeoutlen=200 " Quickly time out on keycodes, but never time out on mappings
set clipboard=unnamed       " use OSX clipboard
set cursorline              " highlight current line where cursor is

"text
set encoding=utf-8
set guifont=Roboto\ Mono\ for\ Powerline:h15

set swapfile
set dir=~/swp

set foldmethod=indent
set foldlevel=99
let g:SimplyFold_docstring_preview=1    " see docstrings for folded code (w/ plugin)

let NERDTreeIgnore=['\.pyc$', '\~$']    "ignore files in NERDTree

" virtual env plugin settings
let g:virtualenv_auto_activate=1
let g:virtualenv_directory = '~/venvs/'

" ale completion/linting settings
let g:ale_linters={'python': ['flake8', 'mypy', 'pylint', 'pyls']}
let g:ale_completion_enabled = 1
set completeopt+=noinsert
inoremap <silent><expr> <Tab>
      \ pumvisible() ? "\<C-n>" : "\<TAB>"

" gutentag settings
let g:gutentags_ctags_exclude = ['*.css', '*.html', '*.pyc']
let g:gutentags_cache_dir = '~/.vim/gutentags'

" -------------
" INDENTATION 
" -------------
set shiftwidth=4
set softtabstop=4
set expandtab
set shiftwidth=4
set tabstop=4

"-------------------
" KEYS AND MAPPINGS
" ------------------
let mapleader = ","

"split navigations...
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

nnoremap <space> za         " enable folding with spacebar

nnoremap <leader>r :exec '!python' shellescape(@%, 1)<CR>

" set a debug trace
nnoremap <leader>s yyP^Cimport ipdb; ipdb.set_trace(context=5)<Esc>

" see the python docs
nnoremap <buffer> <leader>d :<C-u>execute "!pydoc3 " . expand("<cword>")<CR>

" go-to-definition via gutentags + ctrlp plugins
map <silent> <leader>jd :CtrlPTag<cr><c-\>w

" ---------
"  COLORS
" ---------
let g:solarized_termcolors=256
set t_Co=256

if has('gui_running')
  set background=light
  colorscheme solarized
else
  colorscheme zenburn
endif

call togglebg#map("<F5>")

" ----------------
"  LANGUAGE PREFS
" ----------------
"  Python
au BufNewFile,BufRead *.py
    \ set tabstop=4
    \ softtabstop=4
    \ shiftwidth=4
    \ textwidth=79
    \ expandtab
    \ autoindent
    \ fileformat=unix

" JS/HTML/CSS
au BufNewFile,BufRead *.js, *.html, *.css
    \ set tabstop=2
    \ softtabstop=2
    \ shiftwidth=2

"" -----------
"  VUNDLE
" -----------
set rtp+=~/.vim/bundle/Vundle.vim   " set the runtime path to include Vundle and initialize
call vundle#begin()

" Plugins (to install run :PluginInstall)
Plugin 'VundleVim/Vundle.vim'   " let Vundle manage Vundle, required

""code folding
Plugin 'tmhedberg/SimpylFold'

""indentation for python
Plugin 'vim-scripts/indentpython.vim'

""commenting
Plugin 'https://tpope.io/vim/commentary.git'

"" linting
Plugin 'w0rp/ale'

"" colors
Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'

" file tree
Plugin 'scrooloose/nerdtree'

" super searching
Plugin 'kien/ctrlp.vim'

" git
Plugin 'tpope/vim-fugitive'

" powerline
Plugin 'vim-airline/vim-airline'
Plugin 'vim-airline/vim-airline-themes'

" virtual environment support
Plugin 'https://github.com/plytophogy/vim-virtualenv.git'

" tag generation and management
Plugin 'ludovicchabant/vim-gutentags'

" Tim Pope's surround
Plugin 'tpope/vim-surround'

" For saving sessions (with Tmux)
Plugin 'tpope/vim-obsession'

" for navigating panes in tmux and vim
Plugin 'christoomey/vim-tmux-navigator'

call vundle#end()            " required
filetype plugin indent on    " required
mefp commented 5 years ago

update: I found an .inputrc file I had forgotten about but removing it did not change things.

christoomey commented 5 years ago

Can you check the output of tmux list-keys | grep 'C-[hjkl]' in your shell? I want to confirm that the tmux bindings are properly being set by tpm.

Related, can you check the output of :verbose nmap <C-j> in Vim?

Lastly, on the Vim side it looks like you have an explicit set of key bindings:

"split navigations...
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

These will conflict with tmux-navigator and should be removed. I'd be interested in the output of :verbose nmap <C-j> both before you remove the extra mappings, and after for comparison.

mefp commented 5 years ago

Thank you @christoomey for pointing that out and for the response.

Output of tmux list-keys | grep 'C-[hjkl']:

bind-key    -T copy-mode    C-k               send-keys -X copy-end-of-line
bind-key    -T copy-mode-vi C-h               send-keys -X cursor-left
bind-key    -T copy-mode-vi C-j               send-keys -X copy-selection-and-cancel

Output of :verbose nmap <C-j> in Vim: Before removing existing bindings

  <NL>        * :TmuxNavigateDown<CR>
        Last set from ~/.vim/bundle/vim-tmux-navigator/plugin/tmux_navigator.vim line 20

After commenting-out existing bindings (and restart)

n  <NL>        * :TmuxNavigateDown<CR>
        Last set from ~/.vim/bundle/vim-tmux-navigator/plugin/tmux_navigator.vim line 20
ghost commented 5 years ago

I think got this to work following the instructions here: https://thoughtbot.com/blog/vim-splits-move-faster-and-more-naturally

I changed: Plugin 'christoomey/vim-tmux-navigator' to Bundle 'christoomey/vim-tmux-navigator'

and I removed let mapleader = " " from .vimrc


I am having this same problem, including the same output fortmux list-keys | grep 'C-[hjkl'] and :verbose nmap <C-j> as mefp

OS: Mac OS 10.14.5 (Mojave) Terminal App v2.9.5 tmux 2.9a vim version 8.1.1517

~/.tmux.conf

is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind-key -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind-key -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind-key -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind-key -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"
bind-key -T copy-mode-vi C-h select-pane -L
bind-key -T copy-mode-vi C-j select-pane -D
bind-key -T copy-mode-vi C-k select-pane -U
bind-key -T copy-mode-vi C-l select-pane -R
bind-key -T copy-mode-vi C-\ select-pane -l

~/.vimrc

set nocompatible              " required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required
Plugin 'gmarik/Vundle.vim'

" add all your plugins here (note older versions of Vundle
" used Bundle instead of Plugin)

" ...

Plugin 'tmhedberg/SimpylFold'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'vim-syntastic/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'Valloric/YouCompleteMe'
Plugin 'scrooloose/nerdtree'
Plugin 'christoomey/vim-tmux-navigator'
Plugin 'kien/ctrlp.vim'
" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

set incsearch
set ruler
set number

function! MoveLeft()
  let newpos = getpos('.')
  let newpos[2] -= 1
  if (newpos[2] < 1)
    let newpos[2] = 1
  endif
  call setpos('.', newpos)
  return ""
endfunction
inoremap ( ()<C-R>=MoveLeft()<CR>

let mapleader = " "

let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>
let g:SimpylFold_docstring_preview=1
syntax on

imap kj <Esc>

let python_highlight_all=1
syntax on

command Nt NERDTreeToggle
matthewepler commented 5 years ago

PS - copied this config over to my Ubuntu machine and it worked fine. I assume the issue is related to MacOS.

mschwartz commented 4 years ago

Same problem here on macOS

I just added these lines to the bottom of my vimrc:

nmap <C-j> :TmuxNavigateDown<cr>
nmap <C-k> :TmuxNavigateUp<cr>
nmap <C-l> :TmuxNavigateRight<cr>
imap <C-h> :TmuxNavigateLeft<cr>
imap <C-j> :TmuxNavigateDown<cr>
imap <C-k> :TmuxNavigateUp<cr>
imap <C-l> :TmuxNavigateRight<cr>

What is weird is that the TmuxNavigate* commands work if I enter them manually.

cew8 commented 4 years ago

@mschwartz Works like a charm, been looking for this

mschwartz commented 4 years ago

Sorry, but it still doesn’t work for me.

Oddly, if I ssh into a Linux box and tmux + vim there, it works. Same iterm2, just different host.

Ctrl j does move from vim to a shell pane and back.

It seems to be something specific to vim or nvim running within tmux within iterm2 on Catalina.

cew8 commented 4 years ago

@mschwartz Is it on purpose that you left out

nmap <C-h> :TmuxNavigateLeft<cr>

mschwartz commented 4 years ago

Oddly, it magically started working today.

It could be I rebooted, updated other software, etc.

I REALLY appreciate this plugin. I use i3wm. And vim+tmux for all my editing. alt+hjkl change applications (windows) and ctrl-hjkl work within tmux session.

I cannot fathom using a gui version of vim anymore. There simply isn't any editing setup as powerful and efficient as vim and tmux and this plugin.

Thanks!

mschwartz commented 4 years ago

D'oh. Scratch that last comment.

It still fails here. I wasn't running in tmux, and outside tmux the keys work.

So, bug still exists.

nmap <C-h> :TmuxNavigateLeft<cr>
nmap <C-j> :TmuxNavigateDown<cr>
nmap <C-k> :TmuxNavigateUp<cr>
nmap <C-l> :TmuxNavigateRight<cr>
imap <C-h> :TmuxNavigateLeft<cr>
imap <C-j> :TmuxNavigateDown<cr>
imap <C-k> :TmuxNavigateUp<cr>
imap <C-l> :TmuxNavigateRight<cr>

map <C-h> :TmuxNavigateLeft<cr>
map <C-j> :TmuxNavigateDown<cr>
map <C-k> :TmuxNavigateUp<cr>
map <C-l> :TmuxNavigateRight<cr>
matthewepler commented 4 years ago

Not sure what I did to get it working but it is for me (OSX Mojave 10.14.6)

.vimrc (other stuff omitted for brevity)

" for navigating panes in tmux and vim
Plugin 'christoomey/vim-tmux-navigator'

call vundle#end()            " required

.tmux.conf (some stuff omitted for brevity)

# Smart pane switching with awareness of vim splits
bind -n C-k run-shell 'tmux-vim-select-pane -U'
bind -n C-j run-shell 'tmux-vim-select-pane -D'
bind -n C-h run-shell 'tmux-vim-select-pane -L'
bind -n C-l run-shell 'tmux-vim-select-pane -R'
bind -n "C-\\" run-shell 'tmux-vim-select-pane -l'
mschwartz commented 4 years ago

I'm on Catalina.

jhscheer commented 4 years ago

I had the same issue as OP and solved it by switching from TPM to snippet in tmux.conf. macOS Catalina, Mac Terminal, tmux 3.0a, NVIM v0.4.3

inscapist commented 4 years ago

Fwiw, I fixed this a month ago and my issue has to do with pipenv.

I changed from this (from the README):

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

to this: https://github.com/sagittaros/dotfiles/blob/master/bin/isvim2

It has worked perfectly ever since. Ymmv