Why do I have multiple command line input arrows using zsh? - terminal

here is my zshrc
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
export ZSH="/Users/gansaikhanshur/.oh-my-zsh"
ZSH_THEME="powerlevel10k/powerlevel10k"
plugins=(
git
zsh-syntax-highlighting
)
source $ZSH/oh-my-zsh.sh
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
I am using Iterm2 with Oh-my-Zsh. yet I have 3 command line input line arrows as such.
I want only the green one (which is supposed to be the default with powerlevel10k). Why is that? How can I fix this?

Related

Some .zshrc settings aren't loaded

I have a MacBook with Iterm2 with Zshell (zsh) and one of the add-ons I have is the command line fuzzy finder (fzf), but despite being added to my .zshrc it doesn't work. If I manually load it with source ~/.fzf.zsh it works, and if I then reload my .zshrc source ~/.zshrc it doesn't work again.
What could be the reason?
Here's most of my .zshrc file (see the line: # add fuzzy find):
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# add fuzzy find
[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
# add syntax highlighting
export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR=/usr/local/share/zsh-syntax-highlighting/highlighters
source $(brew --prefix)/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
# Path to your oh-my-zsh installation.
export ZSH="/Users/xxxx/.oh-my-zsh"
ZSH_THEME="powerlevel10k/powerlevel10k"
plugins=(git)
source $ZSH/oh-my-zsh.sh
DEFAULT_USER=`whoami`
# To customize prompt, run `p10k configure` or edit ~/.p10k.zsh.
[[ ! -f ~/.p10k.zsh ]] || source ~/.p10k.zsh
alias pip=/usr/local/bin/pip3
Something you do in .zshrc following source ~/.fzf.zsh breaks it. Make sure ~/.fzf.zsh occurs after that, most easily accomplished by moving it to the end of the file.

How can I remove the "empty space" in the terminal at launch?

I use iterm2 with oh-my-zsh (macOS) and I want start "terminal code" on the 1st line (now 5th line). Can you please tell me, How can I do this?
This is my ~/.zshrc:
export ZSH=/Users/ivbutsykin/.oh-my-zsh
ZSH_THEME="powerlevel9k/powerlevel9k"
POWERLEVEL9K_PROMPT_ADD_NEWLINE=true
plugins=(
zsh-autosuggestions
)
source $ZSH/oh-my-zsh.sh
test -e "${HOME}/.iterm2_shell_integration.zsh" && source "${HOME}/.iterm2_shell_integration.zsh"
local user_symbol="$"
if [[ $(print -P "%#") =~ "#" ]]; then
user_symbol = "#"
fi
POWERLEVEL9K_MULTILINE_LAST_PROMPT_PREFIX="%{%B%F{black}%K{yellow}%} $user_symbol%{%b%f%k%F{yellow}%} %{%f%}"
POWERLEVEL9K_VCS_MODIFIED_BACKGROUND='red'
echo -e "\033]6;1;bg;red;brightness;18\a"
echo -e "\033]6;1;bg;green;brightness;26\a"
echo -e "\033]6;1;bg;blue;brightness;33\a"
You can get rid of 3 empty lines by replacing echo -e with echo -ne. To get rid of the remaining empty line you need to upgrade to powerlevel10k.

terminal title not setting within screen

Currently, I'm setting terminal title within screen command, but the bash script gives me:
Cannot exec 'source /etc/profile && title.set root#test': No such file or directory
And I can run above command successful directly from the command line, here are my scripts:
/usr/local/bin/s
#!/bin/bash
if [ $1 ]
then
screen -D -R $1 -m "source /etc/profile && title.set `whoami`#$1"
else
screen -R
fi
/etc/profile
...
# Source global bash config
if test "$PS1" && test "$BASH" && test -z ${POSIXLY_CORRECT+x} && test -r /etc/bash.bashrc; then
. /etc/bash.bashrc
fi
function title.set() {
if [[ -z "$ORIG" ]]; then
ORIG=$PS1
fi
TITLE="\[\e]2;$*\a\]"
PS1=${ORIG}${TITLE}
}
# Termcap is outdated, old, and crusty, kill it.
unset TERMCAP
# Man is much better than us at figuring this out
unset MANPATH
...
So What's going wrong here?
The keyword source is a bash built-in command, i.e., something for which there is not necessarily an actual file to exec (another built-in command). You can only exec something that is a file — like bash, e.g., something like this:
screen -D -R $1 -m bash -c "source /etc/profile && title.set `whoami`#$1"

Shell script to iterate files. Got error on line 3

#!/bin/bash
for dir in /home/username/git/*/
do
for file in "$dir"/*
do
if [[ -f $file ]]
then
echo "$file"
fi
done
done
When I try to run it. I got
syntax error near unexpected toke' `do
'rocTest.sh: line 3: `do
Why?
Use "$file" (with quotes) consistently to deal with "problematic" file names; in particular if [[ -f $file ]] should be
if [[ -f "$file" ]] ...
Note that bash is not always in /bin (e.g. FreeBSD places it in /usr/local/bin); for wider portability, either use
#!/usr/bin/env bash
or #!/bin/sh and make sure to remove bash-isms (e.g. use checkbashisms on Debian/Ubuntu). E.g. write if test -f "$file" instead of [[ -f "$file" ]]

Bash script: only echo line to ~/.bash_profile once if the line doesn't yet exist

I wrote a bash git-install script. Toward the end, I do:
echo "Edit ~/.bash_profile to load ~/.git-completioin.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> ~/.bash_profile
The problem is, if you run the script more than once, you end up appending this line multiple times to ~/.bash_profile. How do I use bash scripting with grep or sed (or another option you may recommend) to only add the line if it doesn't yet exist in the file. Also, I want to add the line to ~/.profile if that file exists and ~/.bash_profile doesn't exist, otherwise just add it to ~/.bash_profile.
Something like this should do it:
LINE_TO_ADD=". ~/.git-completion.bash"
check_if_line_exists()
{
# grep wont care if one or both files dont exist.
grep -qsFx "$LINE_TO_ADD" ~/.profile ~/.bash_profile
}
add_line_to_profile()
{
profile=~/.profile
[ -w "$profile" ] || profile=~/.bash_profile
printf "%s\n" "$LINE_TO_ADD" >> "$profile"
}
check_if_line_exists || add_line_to_profile
A couple of notes:
I've used the . command instead of source as source is a bashism, but .profile may be used by non-bash shells. The command source ... is an error in .profile
I've used printf instead of echo because it's more portable and wont screw up backslash-escaped characters as bash's echo would.
Try to be a little more robust to non-obvious failures. In this case make sure .profile exists and is writable before trying to write to it.
I use grep -Fx to search for the string. -F means fixed strings, so no special characters in the search string needs to be escaped, and -x means match the whole line only. The -qs is common grep syntax for just checking the existence of a string and not to show it.
This is proof of concept. I didn't actually run this. My bad, but it's Sunday morning and I want to go out and play.
if [[ ! -s "$HOME/.bash_profile" && -s "$HOME/.profile" ]] ; then
profile_file="$HOME/.profile"
else
profile_file="$HOME/.bash_profile"
fi
if ! grep -q 'git-completion.bash' "${profile_file}" ; then
echo "Editing ${profile_file} to load ~/.git-completioin.bash on Terminal launch"
echo "source \"$HOME/.git-completion.bash\"" >> "${profile_file}"
fi
How about:
grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile || echo "source ~/.git-completion.bash" >> ~/.bash_profile
or in a more explicit (and readable) form:
if ! grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile; then
echo "Updating" ~/.bash_profile
echo "source ~/.git-completion.bash" >> ~/.bash_profile
fi
EDIT:
You should probably add an additional newline before your one-liner, just in case ~/.bash_profile does not end in one:
if ! grep -q '^source ~/\.git-completion\.bash$' ~/.bash_profile; then
echo "Updating" ~/.bash_profile
echo >> ~/.bash_profile
echo "source ~/.git-completion.bash" >> ~/.bash_profile
fi
EDIT 2:
This is a bit easier to modify and slightly more portable:
LINE='source ~/.git-completion.bash'
if ! grep -Fx "$LINE" ~/.bash_profile >/dev/null 2>/dev/null; then
echo "Updating" ~/.bash_profile
echo >> ~/.bash_profile
echo "$LINE" >> ~/.bash_profile
fi
The -F and -x options are specified by POSIX and were suggested in several other answers and comments.
# Decide which profile to add to
PROFILE=~/.bash_profile
if ! [ -e "$PROFILE" ] && [ -e ~/.profile ]; then
PROFILE=~/.profile
fi
# Add to profile if it doesn't appear to be there already. Err on the side of
# not adding it, in case user has made edits to their profile.
if ! grep -s 'git-completion\.bash' "$PROFILE"; then
echo "Editing $PROFILE to load ~/.git-completion.bash on Terminal launch"
echo "source ~/.git-completion.bash" >> "$PROFILE"
fi

Resources