We have a bunch of servers set up with set -o vi in their ~/.bashrc.

It would be nice to be able to ssh into these boxes and automagically run set -o emacs as a timesave.

I've seen a solution indicating that such additions should be made on the remote host ~/.bashrc but we cannot modify these for various reasons.

Another solution suggests using:

spawn ssh -t user@host "set -o emacs; /bin/bash -i"

..but I understand this requires running the expect shell instead of plain bash.

Is there a simple way to achieve this goal? Any suggestions would be very much appreciated.

  • Is there an issue changing ~/.bashrc? – Karlson Nov 13 '12 at 16:38
  • What is the expect shell? I know of the expect TCL-based command language often (mis?)used to automate secure logons and such - is this what you mean? Also - and this is a shot in the dark - but how do you find ssh user@host '( set -o ; /bin/bash -i /otherrc )' works for you? – mikeserv Mar 30 '14 at 14:02
ssh -t user@host bash -o emacs -i
  • This doesn't work unfortunately. – user490123 Nov 13 '12 at 18:27
  • 1
    Because there's a command in .bashrc that overrides it? Add --norc, or --rcfile yourfile.rc instead. – Jim Paris Nov 13 '12 at 19:19
  • As I said in my original post there is set -o vi in every server .bashrc. I did as you suggested, inserting --norc immediately after bash in your above command. The parameter was clearly applied since the bash prompt presented appeared different, however emacs keybindings were still not enabled. – user490123 Nov 14 '12 at 13:12
  • 1
    If you have set -o vi in your profile files too (or they source .bashrc), you might need: ssh -t user@host bash --norc --noprofile -o emacs -i – Jim Paris Nov 14 '12 at 15:53
  • Thank you for yet another suggestion Jim. Unfortunately this one didn't work either! I'm thinking this may not be possible, it's certainly frustrating to have tried so many approaches without success. – user490123 Nov 14 '12 at 15:58

Start bash with the --rcfile option and specify a different bashrc. For example, write a file called ~/.bashrc-emacs that contains

. ~/.bashrc
set -o emacs

and run

ssh -t user@host bash --rcfile=.bashrc-emacs

You can use --norc to skip reading /etc/bash.bashrc and ~/.bashrc altogether. Bash is in emacs mode by default.

If you do want to read the remote .bashrc and you don't want to write to any file on the server, I think expect is your best bet.

  • Or even something like grep -v 'set -o vi' /etc/bash.bashrc | ( exec bash -i <&0 ) might work. – mikeserv Mar 30 '14 at 14:08

I realize you mentioned not to edit .bashrc. However, I hope this might help someone else. I have been looking for a good answer to a similar problem for many years. I like to use vi but don't want to change the default for everyone else.

Edit /etc/ssh/ssh_config in your local configuration and add the following line:


You can use any environment variable but EDITOR seems like a good choice. In my case EDITOR is set to vim.

In the remote system, edit /etc/ssh/sshd_config and add the following line:

AcceptEnv EDITOR

Edit .bashrc or .bash_profile and add the following:

if [ "$EDITOR" = "vim" ]; then
         set -o vi;

For the use case in this question, replace vi with emacs. Note: You might need to restart sshd in the target system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.