Text shatters after pressing up arrow and changing bash prompt - bash

So I recently decided to change my bash prompt and I have a problem. After pressing up arrow a few times so I don't have to write the command I used like a minute ago the command suddenly breaks the whole prompt and the characters are randomly shattered.
I added this to my bash profile:
export PS1="[\e[32m]\u[\e[m]#[\e[32m]\s[\e[m]-[\e[32m]\W[\e[m] >> "
After pressing the up arrow like two or three times, the characters become totally scrambled like this:
do nano ~/.bash_profile-[]Downloads[] >> source ~/.bash_profile
pwd sudo nano
~/.bash_profile
Guys please help me, it's really annoying. Thanks.

Already answered in comments. Here is the answer:
You need \[...\] around non-printing characters, not [...]. That's why you have a bunch of [] scattered throughout your prompt.
Thanks

Related

MacOS Bash command line overlap

I am new to Bash and just set my own terminal theme. However, when I typed a long command, the command line overlapped in one line instead of wrapping into two. I looked up for solutions and solved the problem by making sure I added \[...\] when using non-printing characters in a bash prompt. Yet, when I move up and down my cursor on previous command line, the command line still overlaps like this:
katnano .bash_profile
while normally it should be:
katrinachan#katrina :~$ nano .bash_profile
My .bash_profile looks like this:
export PS1="\[\e[1;38;5;73m\u#\h\e[m\] :\[\e[1;38;5;214m\W\e[m\]\$ "
export CLICOLOR=1
export LSCOLORS=Cxfxcxdxbxegedabagacad
May I know what I am missing in my .bash_profile file? Thanks
[Solved]
Thank you! It works perfectly. Now my code is:
export PS1="[\e[1;38;5;73m]\u#\h[\e[m] :[\e[1;38;5;214m]\W[\e[m]\$ "
export CLICOLOR=1
export LSCOLORS=Cxfxcxdxbxegedabagacad
The overlap problem doesn't exist anymore.
The \[...\] should only bracket sequences which do not involve cursor movement on output. You have the \u#\h\e sequence inside these brackets where obviously it should be outside.

MobaXterm The fist character of bash prompt is showing up on a different line, how do I fix this?

When I type in a command in MobaXterm, the first character of my prompt shows up at the end of the next line, and the rest of the prompt is on the line after. It doesn't seem to affect anything performance-wise, but I can't seem to find anyone else with this issue. If anyone has any ideas as to what might be causing this, some help would be much appreciated.
screenshot of my terminal
On a MobaXterm window, Click on Settings->Configuration and then in the Terminal tab un-select Display separation line.
Click here for a picture
This is not due to a setting in MobaXterm, but rather it is due to the default prompt set in the version of bash cygwin installed by MobaXterm, which adds a newline.
You can change the prompt by setting PS1 in ~/.bash_profile (or if you set it up ~/.bashrc). For example I add this line and now there is no newline at the prompt:
PS1="\h [\e[33m]\w[\e[0m]> "

KSH shell prompt overwritten

I have customised my ksh shell prompt. It is as follows:
export PS1=$'\E[31;1m$(date +%H:%M:%S) \E[32;1m${PWD##*/} \E[33;1m$ \E[0m'
Sometimes when I press backspace the cursor will jump to the very beginning of the window and overwrite the prompt. I heard this can be a problem with escaping the colours however the examples I have seen online resemble what I have above.
Any idea what can be causing this?

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)
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:
in your terminal echo $PS1 - this will show you your current PS1 setting.
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\]\$'
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\]\$
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.
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

Zsh tab completion duplicating command name

I'm on OS X Mountain Lion, running the included ZSH shell (4.3.11) with Oh-My-ZSH installed over the top.
When using tab completion with commands such as homebrew, when ZSH lists the available commands, it is also duplicating the command. For example:
$ brew {tab}
will result in:
$ brew brew
[list of homebrew commands]
I'm unsure what is causing this error, as when I resize the terminal window, the first instance of the command name disappears.
If I hit backspace when the duplicates are displayed, I can only delete the second instance of the command, zsh won't let me backspace any further. Also, if I do remove the duplicate with backspace, zsh then acts as if there is no command typed at all.
My .zshrc along with all my other .configuration files can be found at https://github.com/daviesjamie/dotfiles
UPDATE: I found this post about someone having the same problem on Ubuntu. However, I don't understand the given solution, and I'm not even sure if it applies to my set up?
This effect also could be reproduced if you use any of fancy UTF-8 characters like arrow, "git branch" character and so on.
Just remove this chars from prompt and duplication will not occur.
Also adding
export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
to ~/.profile can help
The problem is likely to arise from misplaced %{ %} brackets that tell zsh that text inside has zero width. The only things that should be enclosed in them are escape sequences that change color or boldness of the text. If you are using new zsh (>=4.3.{unknown version}) I would even suggest to use %F{color}...%f, %K{color}...%k, %B...%b instead of %{${fg[green]}%} or what you have there.
The problem with them is that there is no way to query the terminal with a question like “Hey, I outputted some text. Where is the cursor now?” and zsh has to compute the length of its prompt by itself. When you type some text and ask zsh to complete zsh will say terminal to move cursor to specific location and type completed cmdline there. With misplaced %{%} brackets this specific location is wrong.
If you use iTerm on Mac, be sure to check "Set locale variables automatically" in your profile preferences. I had it unchecked for an SSH connection and it resulted in the same bug and I fixed it by leaving that option checked.
It's an old thread but I faced similar issue in my zsh setup with oh-my-zsh configuration.
Setting export LC_ALL=en_US.UTF-8 fixed the issue.
A lot of answers in a lot of places suggest the export LC_ALL=en_US.UTF-8 solution. This, however did not work for me. I continued to have this issue using oh-my-zsh on both Arch linux and PopOS.
The only solution that worked for me was this suggestion by romkatv on an issue on the oh-my-zsh github repository.
It turns out, at least in my case, that the autocomplete duplication issue would only show up if there was a non-ASCII character somewhere on the line (like an emoji). And ZSH would incorrectly assume that this non-ASCII character needs to take up 2 character spaces instead of 1.
So the solution that worked was to open up the .zsh-theme file of whatever theme you're using, find all non-ASCII characters and use %{%G%} to tell ZSH to only use one character width for that character
For example, the default oh-my-zsh theme robbyrussel contains 2 non-ASCII characters. The '➜' character in the prompt
PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )"
and the '✗' character in the prompt for git directories
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}✗"
Using %{%G<character>%} around the 2 non-ASCII characters like this
PROMPT="%(?:%{$fg_bold[green]%}%{%G➜%} :%{$fg_bold[red]%}%{%G➜%} )"
and this
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[blue]%}) %{$fg[yellow]%}%{%G✗%}"
is what finally fixed the issue for me.
So all you need to do is make a copy of the theme file you want to use and edit all the non-ASCII characters as shown above and you should hopefully never see the duplication issue again.
My solution to make both local and ssh work is something like a combination of #Marc's and #neotohin's answers:
Set export LANG=en_US.UTF-8 (simply uncomment that part in the template .zshrc; exporting LC_ALL, as in #neotohin's answer, instead of LANG may also work, I didn't try)
Uncheck "Set locale environment variables on startup" in the Terminal profile's "Advanced" section (reason: that setting sets LC_CTYPE=UTF-8 instead of en_US.UTF-8, which brakes the locale for me in ssh)

Resources