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

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.

Related

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

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?

Shell script issue with sed

I am trying to run the following shell script on windows using sh preprocess.sh on cmd
#!/bin/bash
cd ./raw_data
# echo "download pretrained word embeddings: glove.42B.300d.zip"
# wget https://nlp.stanford.edu/data/wordvecs/glove.42B.300d.zip
# echo "unzip glove.42B.300d.zip"
# unzip glove.42B.300d.zip
echo "add vocab size and embedding size to the head of glove.42B.300d.txt"
n=$(sed -n '$=' glove.42B.300d.txt)
sed -i "1i$n 300" glove.42B.300d.txt
cd ..
echo "process raw data"
python process_raw.py
echo "prepare training, valid, testing data"
if [ ! -d "./log" ]; then
mkdir log
fi
python preprocess.py > log/preprocess.log
echo "Finished! preprocess.log is saved in log directory."
However, I am getting the following error
sed.exe: invalid option -- i
Can anybody please suggest a solution?

running multiline bash command over ssh does not work

I need to run a multi-line bash command over ssh, all possible attempt exhausted but no luck --
echo "3. All files found, creating remote directory on the server."
ssh -t $id#$host bash -c "'
if [[ -d ~/_tmp ]]; then
rm -rf ~/_tmp/*
else
mkdir ~/_tmp
fi
'" ;
echo "4. Sending files ..."
scp ${files[#]} $id#$host:~/_tmp/ ;
Here is the output --
user#linux:/tmp$ ./remotecompile
1. Please enter your id:
user
2. Please enter the names of the files that you want to compile
(Filenames *must* be space separated):
test.txt
3. All files found, creating remote directory on the server.
Password:
Unmatched '.
Unmatched '.
Connection to host.domain.com closed.
Please note, I do not want to put every 2-3 lines of bash if-then-else-fi commands into separate files.
What is the right way to do it?
Use an escaped heredoc to have its literal contents passed through. (Without the escaping, ie. using just <<EOF, shell expansions would be processed locally -- making for more interesting corner cases if you used variables inside your remotely-run code).
ssh "$id#$host" bash <<'EOF'
if [[ -d ~/_tmp ]]; then
rm -rf ~/_tmp/*
else
mkdir ~/_tmp
fi
EOF
If you want to pass arguments, doing so in an unambiguously correct manner gets more interesting (since there are two separate layers of shell parsing involved), but the printf '%q' builtin saves the day:
args=( "this is" "an array" "of things to pass" \
"this next one is a literal asterisk" '*' )
printf -v args_str '%q ' "${args[#]}"
ssh "$id#$host" bash -s "$args_str" <<'EOF'
echo "Demonstrating local argument processing:"
printf '%q\n' "$#"
echo "The asterisk is $5"
EOF
This works for me:
ssh [hostname] '
if [[ -d ~/_tmp ]]; then
rm -rf ~/_tmp
else
mkdir ~/_tmp
fi
'

Bash completion command

in my function I have
# Tab completion
if [ "$1" = "--complete" ]; then
shift
# Commands
for line in $(compgen -ac "notes_$1" | sed 's/^notes_//'); do
echo $line
done
# Notes
for line in $(cd $NOTES_DIR; ls $1* 2>/dev/null); do
echo $line
done
return
this should list all commands which start with notes_ and all files in some directory, and it does when called like notes --complete. However, when i try to use tab completion
complete -C 'notes --complete "$COMP_LINE"' notes
it shows only the files not the commands?
Add
echo "--$1--"
after
shift
The problem is that in
compgen -ac "notes_$1"
the contents are not what you expect
I guess $1 has the value notes when you do
$ notes <TAB>
And there is no notes_notes* alias or command in your PATH
Check that adding an alias notes_notesWHATEVER and trying again autocompletion

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