Customizing Terminal on Mac - macos

I was wondering if there was any way to adjust how the terminal on Mac looks. Mainly, is there any way I could change the '$' after my name to be something like '>' after my name. Thanks!

To get a non-permanent preview you can just do
export PS1=">"
To make this permanent add this command to your .bash_profile.
To add the date, directory or other information, you can add any of the following to the text inside the quotes after PS1= :
\d – Current date
\t – Current time
\h – Host name
# – Command number
\u – User name
\W – Current working directory
\w – Current working directory with full path

Related

Bash prompt changes when using arrow keys sometimes

When I use my terminal (iTerm 2 Mac) with my PS1 set to "\[\e[38;5;117m\W \e[39;38;5;104m\$\e[39;0m\] " and I use the arrow keys to go through my bash history it sometimes changes my prompt from ~ $ to just the first character of it and whatever command I'm looking at. For example, going to rvim .bashrc from randomDir $ ls. This problem also persists in the default terminal app.
\W and \$ should not go inside the \[...\], since bash will know how much space each takes up on the terminal.
PS1="\[\e[38;5;117m\]\W \[\e[39;38;5;104m\]\$\[\e[39;0m\] "
Only the characters that make up the ANSI escape sequence (which only instruct the terminal to change colors, without displaying a single additional character) are enclosed in \[...\].
Putting them inside \[...\] tells bash to ignore their contribution to the length of the prompt, leading to incorrect redraws.

MAC OS prompt won't change to show my full path

I have the following in my .bash_profile:
PS1='\h:\w$ '
But my prompt looks like this:
laptop:~$
What setting do I need to modify so that the "~" instead prints out the correct path... in this instance it should be /Users/jay/
The jay account is the default user, so is there a way to change this?
This is expected. Tilde is a very well known shorthand for active user's home folder.
If you look at the help for Bash
man bash
and then type ( you may need to hit 'n' key a couple of times to get to the section about PROMPTING)
/PROMPTING
You will notice that it says
\w the current working directory, with $HOME abbreviated with a tilde
\W the basename of the current working directory, with $HOME abbreviated with a tilde
That being said, if you really want it to print the full path instead, you can use another variable: $PWD (Peek Working Directory) to replace your \w or \W
So, type:
echo $PS1
If for example that returns
\h:\W \u\$
Type
PS1='\h:$PWD \u\$ '
That should change it (it works on my 10.11)

Bash prompt changed in terminal

Suddendly my bash prompt changed to
[80-254-70-241]myusername # ~ $
when in my home directory.
I have the following command in my .bash_profile export PS1="\[\033[32m\][\h]\u # \W \$\[\033[0m\]
I think the expression in square brackets supposed to be the name of the current directory.
What does this number 80-254-70-241 mean and how do I change it back?
The string "80-254-70-241" comes from the parameter "\h", which denotes your hostname. I think you got an IP and a hostname from DHCP so the prompt changed accordingly.
If you think this is annoying, you can replace "\h" with your preferred hostname (hardcoded).

$MSYSTEM variable in bashrc

NOTE
I am using Windows 7. On installing msysgit and GitHub for Windows, I found that git bash can be called from the folders. I opened up the terminal and first thing I wanted was to change how it displays in the console.
Here is what echo $PS1 gave me:
\[\033]0;$MSYSTEM:\w\007 \033[32m\]\u#\h \[\033[33m\w$(__git_ps1)\033[0m\] $
I've been modifying my bash's PS1 for some time now, and know most of the content there is. But I have never ever seen $MSYSTEM before.
Google resulted in nothing except some results about using it to set $MSYSTEM=MINGW32 which of course isn't the case here.
So, what does MSYSTEM variable do? Also, when I create a file .bashrc and put this line there; the terminal now shows a blank-space just before my username. This is because of the empty space in this segment \007 \033 but it was absent before. Here are the screenshots when I use a custom .bashrc and when I don't:
Without bashrc
With bashrc
I know why the newline is absent from my customized terminal; but the questions are:
What is $MSYSTEM?
Why is the first blank-space space missing in first case?
It sets the Window title to the value of MSYSTEM variable. As far as the
space, it looks like you messed up the final newline, notice carefully
export PS1='\[\033]0;$MSYSTEM:\w\007
\033[32m\]\u#\h \[\033[33m\w\033[0m\]
$ '
in the variable above each start of a new line insert a literal newline
character into the PS1.
How to change the title of an xterm

Weird behavior in mac os x terminal

I started using this code from Mark Dotto (http://markdotto.com/2013/01/13/improved-terminal-hotness/) to make my terminal is bit sexier.
I just copied the code without editing it, so in my .bash_profile I added:
export PS1='\[\e[0:35m⌘\e[m \e[0:36m\w/\e[m \e[0:33m`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`\e[m\]'
Everything's working, but there is a weird thing: when I type 3 characters or less then I hit backspace, it deletes everything, even the informations on the left (the path and git branch).
This could be okay, but the problem is that when I keep typing after that, the command I started typing is still here (but hidden).
I guess you didn't understand so I'll try to show some code:
# this is what my prompt looks like
~/my/path/ (branch) |
# I start typing a command
~/my/path/ (branch) ls|
# now I hit backspace once
|
# everything is removed
# but if I type something else then hit return
git st|
# it throws an error as the `l` from the previous command is still here
-bash: lgit: command not found
I have absolutely know idea how this bash_profile works, anybody can help? Thanks
there appears to be some incorrect syntax in your PS1 variable that's causing some unexpected errors. try this revision instead:
export PS1='\[\e[36m\]\w \[\e[33m\]`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /` \[\e[0m\]'
(note: i left the git ... grep ... sed pipeline alone and only edited the parts related to the prompt itself.)
edit - take out the 0: parts and the colors actually work. (i.e. \[\e[36m\] instead of \[\e[0:36m\])
and here's a breakdown of what's going on there:
\[\e[36m\] - this block sets a foreground text color (light blue/tealish)
\w - current working directory
\[\e[33m\] - sets a different text color (yellow)
git ... grep ... sed - retrieves your current git branch
\[\e[0m\] - resets the text color to white so you're not typing commands in yellow
if you don't care about colors, prompts are a fairly trivial thing. the color blocks make it a bit more complex, and (as you've seen) error prone.
First of all: Make sure you are using the BASH shell.
I am on Mountain Lion on a MacBook and the PS1 command sort of, kind of works. My prompt looks like this:
⌘ ~/SVN-Precommit-Kitchen-Sink-Hook.git/ (master) _
I guess the question is what do you want your prompt to do. BASH prompts can embed a whole bunch of escape sequences that can do all sorts of neat things that in Kornshell would take a wee bit of hacking.
Type man bash on the command line, and find the PROMPTING heading. You should see something like this:
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a com-
mand, and the secondary prompt PS2 when it needs more input to complete a command. Bash allows these
prompt strings to be customized by inserting a number of backslash-escaped special characters that
are decoded as follows:
\a an ASCII bell character (07)
\d the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
the format is passed to strftime(3) and the result is inserted into the prompt string;
an empty format results in a locale-specific time representation. The braces are
required
\e an ASCII escape character (033)
\h the hostname up to the first `.'
\H the hostname
\j the number of jobs currently managed by the shell
\l the basename of the shell's terminal device name
\n newline
\r carriage return
\s the name of the shell, the basename of $0 (the portion following the final slash)
\t the current time in 24-hour HH:MM:SS format
\T the current time in 12-hour HH:MM:SS format
\# the current time in 12-hour am/pm format
\A the current time in 24-hour HH:MM format
\u the username of the current user
\v the version of bash (e.g., 2.00)
\V the release of bash, version + patch level (e.g., 2.00.0)
\w the current working directory, with $HOME abbreviated with a tilde
\W the basename of the current working directory, with $HOME abbreviated with a tilde
\! the history number of this command
\# the command number of this command
\$ if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\ a backslash
\[ begin a sequence of non-printing characters, which could be used to embed a terminal
control sequence into the prompt
\] end a sequence of non-printing characters
Let's take a simple prompt. I want to display my user name, the system I'm on, and my current directory. I can set PS1 like this:
PS1="\u#\h:\w$ "
This will give me:
david#davebook:~$ _
The \u is my user name (david), the \h is my machine name (davebook), and the \w displays the current directory I'm in relation to my $HOME directory.
You can also embed commands in the prompt too:
PS1="\$(date) \u#\h:\w$ "
Now the date and time will be embedded in my prompt:
Fri Feb 1 09:45:53 EST 2013 david#DaveBook:~
Sort of silly (I should have formatted the date. Besides, BASH already has built in sequences for the date), but you get the idea.
I recommend that you build your own damn prompt. If you're a git user, and you're using command lines comfortably, you can probably make a nice prompt yourself to look the way you want. You can use the \$(command) syntax to include interactive commands that get executed with each new PS command. You can use ANSI escape codes to color different parts of your prompt, or make them do fantastic stuff.
Build your prompt slowly and bit-by-bit. Create a shell script that will set PS1, and source it in like this:
$ echo "PS='\u#\h:\w\$ " > prompt.sh
$ chmod a+x prompt.sh
$ . prompt.sh
Then, add more and more features to your prompt until you get it to work the way you want.
Personally, I avoid over fancy prompts simply because they tend to fall apart sometime when you least expect it. For example, I use VI sequences for editing, and that prompt simply falls completely apart whenever I try to edit my command line.
Fancy prompts remind me of programs like Talking Moose which are really cool for the first few minutes, then start getting really, really annoying after that.

Resources