Socks
Socks

Reputation: 115

Why is my mac terminal acting buggy when I use the up arrow key?

I normally use the up arrow key to scroll through previous commands in my terminal. I've noticed that over the past few months, when I do this, I do not get previous commands but instead a 'buggy behavior'. For example, when I press the up arrows key, it might enter gibberish and apparently random spaces and a small part of my command. Does anybody have any ideas as to what might be causing this? Sometimes it works, sometimes it doesn't work. It's starting to get very annoying. (fyi, I'm using yosemite and osx if that has any relevance)

Upvotes: 5

Views: 4062

Answers (3)

robneal
robneal

Reputation: 40

I've had this problem for a while myself on my work computer I was able to resolve it when changed from ksh shell to the bash shell.

I'd recommend this Lynda course it explains most of what you need but simply type bash to change shell.

https://www.lynda.com/Mac-OS-tutorials/Unix-Mac-OS-X-Users/78546-2.html

Upvotes: 0

SofieGraham
SofieGraham

Reputation: 341

I know this is an old question, but I recently had the same issue. The problem is likely your PS1 prompt. You may not be fully enclosing your non-printing characters with \[ ... \]

To solve:

  1. in your terminal echo $PS1 - this will show you your current PS1 setting.
  2. Make sure that any options (such as color options) are enclosed by \[ and \] and set it. e.g. PS1='\[\e[95m\]\u \[\e[93m\]\w:\[\e[92m\]\$'
  3. Save your new PS1 permanently in your .bash_profile or wherever you keep your bash settings.

Example:

My faulty PS1 was \[\e[95m\]\u \[\e[93m\]\w: \e[92m\]\$

I was missing the opening encloser, \[ after the \w:

My corrected PS1 is \[\e[95m\]\u \[\e[93m\]\w:\[\e[92m\]\$

Upvotes: 7

William Dye
William Dye

Reputation: 328

In my case, the up-arrow stopped working properly for me in OSX shortly after I changed my .bashrc to use "HISTSIZE=-1". My unconfirmed guess is that the bash version is too old to handle the -1 option, so bash stopped recording a history entirely. My up-arrow key was being interpreted correctly, but there was no history to retrieve. Once I changed my HISTSIZE, the up-arrow (and the history command) in new terminals started working again.

To be clear, I can't say if your problem is from the same cause. I don't recall getting gibberish in the history, though I did see some odd behavior such as an errant "." somehow appearing in ls commands. Still, if someone is having problems with using the up-arrow to retrieve previous bash commands, it seems worth a shot. Try checking your HISTSIZE setting, and/or invoke the history command directly, to make sure that you have a history for the up-arrow to retrieve.

Upvotes: 3

Related Questions