How can I avoid certain lines from .bashrc and .inputrc from being loaded in tmux?

StackOverflow https://stackoverflow.com/questions/22253884

  •  11-06-2023
  •  | 
  •  

Pergunta

I have these lines in my .inputrc:

"(": "\C-v()\ei"
"[": "\C-v[]\ei"
"{": "\C-v{}\ei"
"\"": "\C-v\"\C-v\"\ei"
"\'": "\C-v\'\C-v\'\ei"

This autocloses quotes and brackets in a terminal. But it causes an inconvenience in a tmux session: when I send text containing quotes to frome one pane (vim) to another pane (bash / python / R etc), every quote is turned into two, very annoying.

Is it possible to disable these lines in (and only in) tmux?

Foi útil?

Solução

tmux sets the TMUX environment variable, so in .bashrc (or .profile or whatever):

if [ '' = "$TMUX" ] ; then
  echo not in TMUX
else
  echo in TMUX
fi

You can set INPUTRC to override the default .inputrc location, so you could have a tmux one and a non-tmux one, and export a suitable INPUTRC value in .bashrc depending on TMUX. You could even concoct a suitable .inputrc (e.g. in /tmp) for that session based on a "common" file and a "non-tmux session" file.

Unfortunately tmux exports TMUX, and so subshells started from a tmux session will have TMUX set regardless. Not found a way round that yet.

Outras dicas

I ended up doing this in ~/.bashrc:

if [[ '' = "$TMUX" ]]
then
    set -o vi
    bind -m vi-insert '"(" "\C-v()\ei"'
    bind -m vi-insert '"[" "\C-v[]\ei"'
    bind -m vi-insert '"{" "\C-v{}\ei"'
    bind -m vi-insert '"\"" "\C-v\"\C-v\"\ei"'
    bind -m vi-insert '"\047" "\C-v\047\C-v\047\ei"'
else
    echo Welcome to Tmux!
fi

UPDATE

Adopting user3392484 's suggestion, I found this much better:

if [[ '' = "$TMUX" ]]
then
    export INPUTRC=~/.inputrc
else
    export INPUTRC=~/.tmux.inputrc
    echo Welcome to Tmux!
fi
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top