THE ISSUE
I have a .bash_profile with the following settings:
# Git configuration
## Branch name in prompt
source ~/.git-prompt.sh
PS1='[\W$(__git_ps1 " (%s)")]\$ '
export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'
These settings output the following prompt:
[my-git-repo(master)]$ ls
index.html
[my-git-repo(master)]$
Not bad. You have the base working directory (\W), branch indication—nice and minimal. However, I want the prompt to look like this because it's easier for me to visually parse large outputs:
[my-git-repo(master)]$ ls
index.html
[my-git-repo(master)]$
MY ATTEMPTED SOLUTION
So, to achieve this, I changed my .bash_profile to:
# Git configuration
## Branch name in prompt with newlines
source ~/.git-prompt.sh
PROMPT_COMMAND='PROMPT_COMMAND='\''PS1="\n[\W$(__git_ps1 " (%s)")]\$ "'\'
export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'
However, the new prompt looked like this:
[my-git-repo]$ ls
index.html
[my-git-repo]$
As you can see, there is no newline after ls (my attempts to incorporate it failed), there is a newline after index.html, and the branch indication (master) has disappeared.
So close, yet I'm not entirely sure what I'm doing wrong. Help is appreciated!
You need to make use of PS0 (supported in bash 4.4 or later) to add a blank line after your input. You can also simply add a newline to the beginning of your PS1.
PS0='\n'
PS1='\n[\W$(__git_ps1 " (%s)")]\$ '
Prior to 4.4, you would need to install a complicated DEBUG trap to ensure that it only adds a newline before the first affected command is executed; I'm not going to try to do that correctly.
Related
MacOS Ventura 13.1
M2 Silicon
Trying to set up a new mac. On my existing Mac, I have a .zshrc, and in it, I have the following:
## begin Git branch prompt
git_branch_test_color() {
local ref=$(git symbolic-ref --short HEAD 2> /dev/null)
if [ -n "${ref}" ]; then
if [ -n "$(git status --porcelain)" ]; then
local gitstatuscolor='%F{red}'
else
local gitstatuscolor='%F{green}'
fi
echo "${gitstatuscolor} (${ref})"
else
echo ""
fi
}
setopt PROMPT_SUBST
PROMPT='%/ $(git_branch_test_color)%F{none} $ '
# add 24h time the right side
RPROMPT='%D{%m-%d-%Y %k:%M:%S}'
## end Git branch prompt
And I would get something like:
/Users/jmac/Development/repos/p1 (development) $ 02-16-2023 19:20:56
(development) is in red, because I have not checked in my changes and the full path is there for me to see.
On the new Mac, there is no .zshrc by default, so I added the code to the .zprofile file, and it's not working the same. I see the date/time on the right, but I don't see the full path and the prompt looks like this:
/Users/jmac p1 % 02-16-2023 19:20:56
Any ideas? prompt modification is not my forte.
The short answer: create a ~/.zshrc file and move that code into it.
The slightly longer answer: something is overriding the value in the PROMPT variable. There isn't anything in the default zsh installation that'll turn the $ in your prompt into a %.
That 'something' could be in a few different forms. There may be code that is modifying the PS1 variable, which is essentially a synonym for PROMPT. Or there may be something hidden in scripts or functions called from ~/.zprofile (for example, oh-my-zsh does this, albeit usually from ~/.zshrc).
Some options
Try to trace what's happening in the zprofile file. One way to do that is to run these commands:
setopt xtrace
. ~/.zprofile
The main challenge here is often the sheer volume of the output; it may be difficult to find the spot where the prompt is being set.
Move the PROMPT assignment to the bottom of ~/.zprofile. Then your assignment should override whatever is setting the value earlier in the process.
Move the PROMPT assignment into ~/.zshrc. This is a better place for the assignment anyway, since .zshrc is only loaded for interactive shells, and setting the prompt is only used in interactive shells.
The code in ~/.zshrc is loaded after the code in ~/.zprofile, so this has a similar effect to the prior option.
If none of these options have any effect, then you'll need to look for other startup files, e.g. ~/.zlogin. There's a nice overview of how the various dot files are handled in zsh in this answer.
Whenever I execute a command using an alias, this command is not stored in the shell's command history.
So if I run history these commands do not appear in the list.
Nor do they appear when I press CTRL + r for reverse searching the command history.
When I press the keyboard's arrow up for scrolling through the last commands, I will see an aliased command only if it was the last command I ran. Other aliased commands are will not be displayed.
For example:
$ cd my-repo
$ gs # an alias to git status
$ history
Outputs the following:
2374 cd my-repo
(the gs command is not displayed)
A few notes:
gs is only an example. The issue is far more annoying in more complex commands since I have to retype them all over again instead of executing them from history (e.g. k get pods | grep <pod_name>, where k=kubectl).
gs is defined so: alias gs=' git status'.
I also have a few functions in ~/.alias, e.g.:
mkcd () {
mkdir -pv $1
For some reason, mkcd (or any other function in the alias file) is included in the history.
I do not mind if it prints out gs or expands to git status, I'll take any of the two...
I am using zsh with oh-my-zsh on macOS (Monterey). My shell aliases are defined in ~/.alias which is sourced in ~/.zshrc (source ~/.alias).
This happens both in iTerm2 and in the default Mac terminal.
Thank you for taking the time to help :-)
I will assume that your example alias is exactly what you have in your ~/.alias file.
So you have aliases like this (notice the space character in front of git command):
alias gs=' git status'
There is an shell option called HIST_IGNORE_SPACE which is doing exactly what you are experiencing - in short it will not add command to the history when it starts with space character. Example:
echo 'This command will make it to the history.'
echo 'This poor command will be forgotten.'
You can check your current options using setopt or specifically:
setopt | grep 'histignorespace'
So there are two ways how you can fix this - either by fixing your aliases to not start with space or, If you really don't want this functionality at all, by unsetting it in your ~/.zshrc like this:
unsetopt HIST_IGNORE_SPACE
I'm trying to customize my integrated terminal shell prompt in vscode, and was successfully able to change the theme (so that I can see my current working directory and branch I'm on), however now I want to remove the first portion 'anhlucci#Anhs-MacBook-Pro'. How do I do that?
I use Ubuntu with bash, and I only add the following lines to the end of ~/.bashrc:
if [ "$TERM_PROGRAM" = "vscode" ]; then
PS1='\[\033[01;34m\]\w\[\033[00m\]\$ '
fi
I found that vscode sets TERM_PROGRAM environment variable, and then use it to modify PS1 only to vscode.
The command line prompt is not dictated by Visual Studio Code, but by bash. The prompt is dictated by the PS1 variable in bash. You can view it as follows:
echo "$PS1"
To give you an idea of how that works, this is how my prompt looks like:
[hongli#Leticia Projects]$
My $PS1 looks like this:
[\u#\h \W]\$
Things like \u and \h are formatters that are substituted with a specific value. \u is for the current username, \h is for the hostname.
I'm guessing your $PS1 contains something like \u#\h in the beginning. Remove that and reset the PS1 variable, for example like this:
PS1='[\W]\$ '
Finally, you need to persist this in your bash configuration file so that the next time you start your shell it will show that same prompt. The bash config file is typically ~/.bashrc or ~/.profile depending on the exact Linux distribution you use. Make sure you set $PS1 in there.
Using iTerm2 with zsh and it isn't recognizing my aliases. Sometimes I have to work in an IDE and can't just easily vim something and the stupid people thought it a good idea to name their applications like MyReallyLongApplicationName.app and since .html files open by default in browsers, I have to:
open -a MyReallyLongApplicationName.app something.html
I have an alias in my .zshrc like:
alias ide="open -a MyReallyLongApplicationName.app"
But zsh won't recognize my aliases. I tried another one just to see if it was me but none of the aliases I create are recognized. Just get "zsh: command not found: ide" or whatever.
So I'm not sure what I'm doing wrong and I've been searching around all day trying to fix things in zsh and the like. As a note, I'm not a pro at Linux/Unix systems so if you're too technical and vague I probably won't understand what you're telling me.
Thanks!
if you do a very simple alias in zsh, does it work? open your .zshrc file, and add the following line:
alias ls='ls -GpF'
after adding that line, type this line in your Terminal:
source ~/.zshrc
tell us what happens. Also, just for shiggles, make sure you are using single quotes vs. double quotes, I have seen that make a difference in the past on different versions of shells/OS/whatnot.
Add "source ~/.bash_profile" to your ~/.zsh config file.
Put this line:
/source: 'source ~/.bash_profile' into ~/.zshrc
After saving changes in ~/.zshrc file, open a new shell window and execute the command in it.
Sometimes the simple solution is what we need...
Add "source ~/.bash_profile" to your ~/.zshrc config file
echo source ~/.bash_profile >> ~/.zshrc
I needed to manually add the alias to my zsh config file and then run the source command on it.
echo alias this='some command' >> ~/.zshrc
source ~/.zshrc
In my case issue was space b/w aliasName and equalTo. you should have to remove those space.
bad assignment
alias keu = 'k exec -it utils bash'
correct one
alias keu='k exec -it utils bash'
What I did, in this case, was created a separate file to store all my aliases. I found this way to be cleaner and easier maintained.
My aliases file is simply called aliases and within my .zshrc I have the following:
# Linking my aliases file
source ~/foldername/aliases
Make sure the double quotes are actual double quotes and not some other character which looks like double quotes.
I was editing ~/.zsh-aliases in OSX - TextEdit, which, when hitting the double quotes key substituted it for another special double quotes character, which is not what ZSH expects.
After editing the alias file with Sublime and replacing the old double quotes with actual double quotes everything runs just fine.
Hope this helps.
I had all my aliases on ~/.bash_profile, so i added at the last line on ~/.zshrc the following line: . ~/.bash_profile and it worked for me.
You should put alias at the end of ~/.zshrc file.
you can use below command to do that:
echo alias this='some command' >> ~/.zshrc
after that run
source ~/.zshrc
then, open a new terminal and execute the command in it.
I'm using both bash and zsh with one .bashrc, .bash_aliases and .zshrc file.
Put this in you .zshrc to load bash files:
# shortcut to refresh .zshrc
alias refz="source ~/.zshrc"
# Load bash files to zsh
test -f $HOME/.bashrc && . $HOME/.bashrc
test -f $HOME/.bash_aliases && . $HOME/.bash_aliases
If you have many bash aliases and functions you may will have some error messages like:
/proc/self/fd/13:12310: bad option: -t
caused by bash specific lines in.bash_aliases or .bashrc files
You can skip those problematic ones using:
if [ -n "$BASH" ] ;then
lines to ignore by zsh
fi
For example kubectl autocompletion
# To fix error massage .bashrc:16: command not found: shopt
# Check if bash is the current shell, if not, skip it
if [ -n "$BASH" ] ;then
# kubectl and bash completions
if [ -x "$(command -v kubectl)" ]; then
source <(kubectl completion bash)
complete -F __start_kubectl k
fi
if ! shopt -oq posix; then
if [ -f /etc/profile.d/bash_completion.sh ]; then
. /etc/profile.d/bash_completion.sh
fi
fi
fi
# Instead I need to put this line somewhere in my zshrc
# to have kubectl autocompletion replacing the skipped bash one:
plugins=(git git-flow brew history node npm kubectl)
# To fix error message .bash_aliases:4: parse error: condition expected: =
# Change these to this syntax to be used by zhs
# Not compatible with zsh:
if [ $HOSTNAME = "x1" ]; then
# Compatible with bash and zsh:
if [[ $HOSTNAME == "x1" ]]; then
I had a bad alias line that should have been obvious.
There is no error checking in these zsh custom scripts, so you may, like me, waste a couple of precious hours trying to find out why your custom aliases or functions are not loading in iTerm2.
I am using MacOS Ventura 13.1 and iTerm 2 v3.4, zsh 5.8.1.
I reloaded and using . ~/zshrc and followed a lot of the suggestions above.
I finally copied my list of aliases and function to the ~/.zshrc file.
Another ~/.zshrc gave me a bad pattern error.
The fix for my case was as follows.
In line with a Unicode string or something that is escaped and requires ' ' (single quotes), you must use the other quote character (double quotes) to encapsulate the alias sting.
In my case, I had entered.
alias fixitemerrow='printf '\e[?2004l''
The fix for this is:-
alias fixitemerrow=" printf '\e[?2004l' "
Do not add spaces in your alias assignment. This is here just for illustration.
Spaces would also require double encapsulation " ' ' ".
Need to create a profile for .zshrc and register the alias into it. (only if the profile isn't available)
cd ~
touch .zshrc && open .zshrc
add all the alias in .zshrc file
source ~/.zshrc
close and re-open terminal and run the alias.
I've set a bash prompt like this:
PS1='\W\[\e[31m\]$(git branch &>/dev/null; if [ $? -eq 0 ]; then \
echo " $(git branch | grep '^*' |sed s/\*\ //)"; fi)'
I want to make it default in order not to enter it every time I open the terminal.
(I'm using OSX lion / Terminal Version 2.2.1)
Solution: I put the PS1=... line in ~/.bash_profile as Hai Vu suggested.
Thank you all for your answers
Put the PS1=... line in ~/.bash_profile and it should work.
In general, look also for /etc/profile, /etc/bashrc, ~/.sh_profile, or ~/.profile ( usually for root it's .profile, and for non-root .bash_profile ), etc. In various versions of bash and host platforms, successful hit may vary :) Theoretically, bashrc and bash_profile (shrc, sh_profile) are for different purpose, rc file for shell configuration, and profile for various settings not related to shell itself, they're processed in fixed order, including system-wide and per-user configuration.
And I'm not sure, if executing an external command like "git" and piping to sed, grep and more is wise to use with prompt setup.
Disclaimer: I know nothing about OSX...
As I stated in my comment, your ~/.bashrc file is the place to put the command.
Depending on how the the shell is invoked you might need to add the following lines to
the file ~/.bash_login
if [ -f ~/.bash_login ]; then
. .bashrc
fi
And again, don't do the sed-thingie yourself to get the git-branch name into your prompt.
USe the amazing bash-script from here.
BTW, this is how my prompt looks like (using the script above):
PS1='\[\e]0;\w\a\]\n\[\e[32m\]\u#\h\[\e[31m\] $(__git_ps1 "%s") \[\e[0m\]\[\e[33m\]\w\[\e[0m\]\n\$ '