17 KiB
title, description, published, date, tags, editor, dateCreated
title | description | published | date | tags | editor | dateCreated |
---|---|---|---|---|---|---|
03 Terminal | true | 2024-11-24T11:45:11.626Z | markdown | 2023-04-30T06:05:33.890Z |
Installing ZSH
ZSH is a pretty cool lightweight shell that is very configurable, Install it with the following command
sudo pacman -S zsh
Now we are going to make ZSH the default shell with the following command
chsh -s $(chsh -l | grep -m1 zsh)
We are going to create zshrc config files in a central config folder for easy backups.
mkdir ~/Config
then create 2 files in there
touch ~/Config/zshrc-global ~/Config/zshrc-user
Now we need to create a new user zsh config folder
mkdir ~/.config/zsh
now we are going to symlink the config files to where zsh expects them to be
sudo ln -sf ~/Config/zshrc-global /etc/zsh/zshrc
ln -sf ~/Config/zshrc-user ~/.config/zsh/.zshrc
Now we need to set the dotfile location in zshenv
sudo vim /etc/zsh/zshenv
Add in the following line
export ZDOTDIR=~/.config/zsh
Save and exit
ZSH should be functional with a terrible prompt, so lets fix that first.
Installing a Powerline Font
We are gonna need the right fonts for all the icons
sudo pacman -S ttf-meslo-nerd
Change the terminal/monospace font to MesloLGS NF Regular in your terminal or DE settings
Configuring Guake
Guake is my terminal of choice, I like that it can split the window and it opens and closes fullscreen with a single button nice quick and easy. For KDE users there is Yauake, It is very much like Guake, but there are a ton more Guake clones out there, You should be able to follow this guide for most of them.
Install Guake with the following command
sudo pacman -S guake
Now open guake preferences from your menu.
Under General disable the tray icon and the startup popup
Under Main Window Disable the Tab Bar and put the height and width full
Under Appearance Set Meslo LGS Nerd Font Regular as your font, set Tango as your color scheme and set the transparency to your liking.
Under Keyboard Shortcuts I set "Toggle Guake Visibility" to `, this can give problems, in this case also set the hotkey in your DE to "guake", set "Split tab vertical" to CTRL + N, "Split tab horizontal" to CTRL + B, "Focus terminal above" to Ctrl + Up, "Focus terminal below" to Ctrl + Down, "Focus terminal on the left" to Ctrl + Left, "Focus terminal to the right" to Ctrl + Right, "Increase heigth" to Ctrl + Shift + Down, "Decrease Height" to Ctrl + Shift + Up, "Increase transparency" to Ctrl + Shift + Left and finally "Decrease transparancy" to Ctrl + Shift + Right
Ofcourse you can config anything else you like, but for me this is enough
Configuring Alacritty
I use Alacritty for a quick and fast terminal it works especially great with tiling window managers you can install it with the following command
sudo pacman -S alacritty
First we are going to create a config file for Alacritty
touch ~/Config/alacritty.yml
Now we are going to create the config folder alacritty expects
mkdir ~/.config/alacritty
And finally symlink the file to the location alacritty expects it to be.
ln -sf ~/Config/alacritty.yml ~/.config/alacritty/alacritty.yml
Now it is time to configure alacritty using the file
nvim ~/Config/alacritty.yml
We are gonna need some fonts to properly display the icons with our theme so add in the following lines, you can change the font, But I recommend putting this one first to follow along with the guide.
#MesloLGS font
font:
normal:
family: MesloLGS Nerd Font
style: Regular
bold:
family: MesloLGS Nerd Font
style: Bold
italic:
family: MesloLGS Nerd Font
style: Italic
bold_italic:
family: MesloLGS Nerd Font
style: Bold Italic
size: 11
Setting a nice color theme is vital, The same applies here, you can change it later, but I recommend just following along for now. There are tons of color schemes available.
#Tango color theme
colors:
primary:
background: '#000000'
foreground: '#ffffff'
normal:
black: '#000000'
red: '#cc0000'
green: '#4e9a06'
yellow: '#c4a000'
blue: '#3465a4'
magenta: '#75507b'
cyan: '#06989a'
white: '#d3d7cf'
bright:
black: '#555753'
red: '#ef2929'
green: '#8ae234'
yellow: '#fce94f'
blue: '#729fcf'
magenta: '#ad7fa8'
cyan: '#34e2e2'
white: '#eeeeec'
Spawn a new terminal in the current location with CTRL + SHIFT + N
key_bindings:
- { key: N, mods: Control|Shift, action: SpawnNewInstance }
Powerlevel10K
Powerlevel10K is a great theme for ZSH, it is very configurable and has a lot of cool features
Sadly it is unmaintained, but it works fine, we can clone the repo into our /usr/share folder.
sudo git clone https://github.com/romkatv/powerlevel10k.git /usr/share/zsh-theme-powerlevel10k/
Now we need to include the following lines in our zshrc-global file
#P10k
source /usr/share/zsh-theme-powerlevel10k/powerlevel10k.zsh-theme
And the following lines to our zshrc-user file
#P10k
[[ ! -f ~/.config/zsh/.p10k.zsh ]] || source ~/.config/zsh/.p10k.zsh
To configure Powerlevel10K you must run the following command
p10k configure
Have some patience and run trough the interactive configuration
Now we just need to symlink the config file to the expected location
ln -sf ~/Config/p10k.zsh ~/.config/zsh/.p10k.zsh
When you exit the terminal and start it again it should look all nice :)
Installing LSD
lsd is like ls with colors, nice icons and other visual cues that help you.
It is in the repos, simply install it with the following command
sudo pacman -S lsd
You can now run it with lsd, we will later alias ls to lsd in our zsh configuration.
try the following command for example
lsd -la ~
Adding ZSH Options and Keybinds
ZSH options change the default behavior so do keybinds
You can add them all or just the ones you like to your zshrc-global file
Give flag suggestions for programs
autoload -Uz compinit && compinit
Gives you a menu when navigating suggestions
zstyle ':completion:*' menu select
zstyle ':completion::complete:*' gain-privileges 1
History settings for ZSH
export HISTFILE=~/.config/zsh/.zsh_history
export HISTSIZE=1000000
export SAVEHIST=1000000
setopt EXTENDED_HISTORY
setopt HIST_IGNORE_ALL_DUPS
setopt HIST_FIND_NO_DUPS
setopt inc_append_history
Press up and down to search to matching history
bindkey "^[[A" history-beginning-search-backward
bindkey "^[[B" history-beginning-search-forward
Case insensitive tab completion, Also enables cd doc/fo/su to go to /documents/folder/subfolder
zstyle ':completion:*' matcher-list 'm:{a-z}={A-Z}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
I really don't see the use for Flow Control
unsetopt flow_control
Fix navigation keys
bindkey "^[[H" beginning-of-line
bindkey "^[[F" end-of-line
bindkey "^[[3~" delete-char
bindkey "^[[5~" beginning-of-line
bindkey "^[[6~" end-of-line
Adding Aliases
Aliases are simply alternatives for a string of text, as an easy example, v will type nvim, and sv will type sudo nvim. You can also chose to remove some or add more.
Simply add the following text to one of your ~/config/zshrc files, user will set it for you, and global will set it for everyone,
alias a="awk"
alias b="btrfs"
alias c="cryptsetup"
alias d="docker"
alias e="echo"
alias f="fdisk"
alias g="git"
alias h="history"
alias j="jobs -l"
alias k="kubectl"
alias l="ls -la"
alias m="man"
alias n="neofetch"
alias o="openssl"
alias p="pacman"
alias r="reboot"
alias s="sudo"
alias t="tail -f"
alias u="uname"
alias v="nvim"
alias w="whence"
alias sudo="sudo "
alias ffs="sudo !!"
alias distro="cat /etc/*-release"
alias hk="cat ~/config/hotkeys"
alias sv="sudo v"
alias md="mkdir -p"
alias tk="take"
alias rmd="rm -rf"
alias sgrep="grep -R -n -H -C 5 --exclude-dir={.git,.svn,CVS} "
alias hgrep="fc -El 0 | grep"
alias please='sudo'
alias phone='ssh houtworm@pinephone'
alias server='ssh houtworm@server'
alias game='gamemoderun mangohud'
alias ytflac="youtube-dl -x --audio-format flac --prefer-ffmpeg"
alias ytmkv="youtube-dl -F"
alias lol='lolcat'
alias cd1="cd .."
alias cd2="cd ../.."
alias cd3="cd ../../.."
alias cd4="cd ../../../.."
alias cd5="cd ../../../../.."
alias cd6="cd ../../../../../.."
alias cd7="cd ../../../../../../.."
alias cd8="cd ../../../../../../../.."
alias cd9="cd ../../../../../../../../.."
alias scls="systemctl list-unit-files"
alias scs="sudo systemctl status "
alias scre="sudo systemctl restart "
alias scst="sudo systemctl start "
alias scsp="sudo systemctl stop "
alias scen="sudo systemctl enable "
alias scenn="sudo systemctl enable now "
alias scdi="sudo systemctl disable "
alias fwd="firewall-cmd"
alias fwdlist="firewall-cmd --list-all-zones"
alias fwdre="firewall-cmd --reload"
alias gi="git init"
alias ga="git add *"
alias gc="git commit -m"
alias gp="git push"
alias me="ifconfig | grep "inet " | cut -b 9- | cut -d" " -f2"
alias allcolor="for i in {0..255}; do print -Pn "%K{$i} %k%F{$i}${(l:3::0:)i}%f " ${${(M)$((i%6)):#3}:+$"\n"}; done"
alias rainbow="printf "$BBLA\n$BRED\n$BGRE\n$BYEL\n$BBLU\n$BMAG\n$BCYA\n$BWHI\n\n""
alias dud="du -d 1 -h"
alias duf="du -sh *"
alias :q="exit"
alias ls="lsd"
alias ports="netstat -tulanp"
alias compr="gcc -Wall -Wextra -Werror *.c && ./a.out && rm a.out"
alias compra="gcc -Wall -Wextra -Werror *.c && ./a.out"
alias norme="norminette -R CheckForbiddenSourceHeader"
alias valg="gcc *.c -ggdb3 && valgrind --show-leak-kinds=all --leak-check=full --track-origins=yes ./a.out && rm a.out"
alias valga="gcc *.c -ggdb3 && valgrind --show-leak-kinds=all --leak-check=full --track-origins=yes ./a.out"
alias normsh="checkbashisms"
alias banned='sudo fail2ban-client banned | tr -t "[{" " \n" | tr -d ":]},"'
Adding ZSH Functions
ZSH functions are like tiny scripts that perform a task, you can add the ones you think are useful to you to one of the zshrc files, I suggest global so anyone can use them :)
Press Ctrl + Z to bring stuff to the background but also bring stuff back to the foreground instead of typing fg
backforeswitch () {
if [[ $#BUFFER -eq 0 ]]; then
BUFFER="fg"
zle accept-line -w
else
zle push-input -w
zle clear-screen -w
fi
}
zle -N backforeswitch
bindkey '^Z' backforeswitch
Press Esc twice to put sudo in front of your previous command.
sudoswitch() {
[[ -z $BUFFER ]] && zle up-history
if [[ $BUFFER == sudo\ * ]]; then
LBUFFER="${LBUFFER#sudo }"
elif [[ $BUFFER == $EDITOR\ * ]]; then
LBUFFER="${LBUFFER#$EDITOR }"
LBUFFER="sudoedit $LBUFFER"
elif [[ $BUFFER == sudoedit\ * ]]; then
LBUFFER="${LBUFFER#sudoedit }"
LBUFFER="$EDITOR $LBUFFER"
else
LBUFFER="sudo $LBUFFER"
fi
}
zle -N sudoswitch
bindkey "\e\e" sudoswitch
bindkey -M vicmd '\e\e' sudoswitch
Make the man pages all colorful
function man() {
env \
LESS_TERMCAP_mb=$(printf "\e[1;31m") \
LESS_TERMCAP_md=$(printf "\e[1;31m") \
LESS_TERMCAP_me=$(printf "\e[0m") \
LESS_TERMCAP_se=$(printf "\e[0m") \
LESS_TERMCAP_so=$(printf "\e[0;37;102m") \
LESS_TERMCAP_ue=$(printf "\e[0m") \
LESS_TERMCAP_us=$(printf "\e[4;32m") \
PAGER="${commands[less]:-$PAGER}" \
_NROFF_U=1 \
GROFF_NO_SGR=1 \
PATH=${HOME}/bin:${PATH} \
man "$@"
}
Creates a TAR archive of a file or folder.
function maketar() { tar cvzf "${1%%/}.tar.gz" "${1%%/}/"; }
Create a ZIP archive of a file or folder.
function makezip() { zip -r "${1%%/}.zip" "$1" ; }
Extracts any type of archive automagically
function extract {
if []; then
echo "Usage: extract <path/file_name>.<zip|rar|bz2|gz|tar|tbz2|tgz|Z|7z|xz|ex|tar.bz2|tar.gz|tar.xz>"
else
if [] ; then
case $1 in
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.tar.xz) tar xvJf $1 ;;
*.lzma) unlzma $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) unrar x -ad $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xvf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*.xz) unxz $1 ;;
*.exe) cabextract $1 ;;
*) echo "extract: '$1' - unknown archive method" ;;
esac
else
echo "$1 - file does not exist"
fi
fi
}
The git riddle
function iacp() {
if []
then
echo "Usage: iacp <name> <link> <comment>"
else
git init
git add *
git commit -m "$3"
git remote add $1 $2
git push --set-upstream $1 master
fi
}
The git push
function acp() {
if []
then
echo "Usage: acp <comment>"
else
git add *
git commit -m "$1"
git push
fi
}
take or tk = mkdir -p and cd in one
function take() {
md "$1"
cd "$1"
}
Really clear the screen with Ctrl + L
clearbetter () {
BUFFER="clear"
zle accept-line -w
}
zle -N clearbetter
bindkey '^L' clearbetter
Reload ZSH with Ctrl + S
resourcezsh () {
BUFFER="exec -l zsh"
zle accept-line -w
BUFFER="clear"
zle accept-line -w
}
zle -N resourcezsh
bindkey '^S' resourcezsh
Exit with Ctrl + Q
ctrlqexit () {
BUFFER=":q"
zle accept-line -w
}
zle -N ctrlqexit
bindkey '^Q' ctrlqexit
Unban IPs with fail2ban
unban () {
sudo fail2ban-client unban $@
}
ZSH Plugins
To add plugins to ZSH we simply need to download them and source the main .zsh file of that plugin in one of your zshrc files. Below are the ones I use
Fast Syntax Highlighting is great, it gives your input color based on brackets, if it is correct or not, etc
git clone https://github.com/z-shell/F-Sy-H /usr/share/zsh/plugins/F-Sy-H
Now simply source the .zsh file in your global zshrc by adding the following line to it
source /usr/share/zsh/plugins/F-Sy-H/F-Sy-H.plugin.zsh
Alias Tips helps you remember the aliases you set, if you don't use a set alias it will notify you of the alias in a way that doesn't annoy you.
git clone https://github.com/djui/alias-tips /usr/share/zsh/plugins/alias-tips
Now simply source the .zsh file in your global zshrc by adding the following line to it
source /usr/share/zsh/plugins/alias-tips/alias-tips.plugin.zsh
ZSH Autosuggestions is nice, it tries to complete a command based on your history
git clone https://github.com/zsh-users/zsh-autosuggestions /usr/share/zsh/plugins/zsh-autosuggestions
Now simply source the .zsh file in your global zshrc by adding the following line to it
source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
Also add the following line to the /etc/zsh/zshenv file to drastically speed up this plugin
export ZSH_AUTOSUGGEST_MANUAL_REBIND
Z is a pretty cool plugin for fast navigation just type z nameoffolder and it will jump to it based on history.
git clone https://github.com/agkozak/zsh-z /usr/share/zsh/plugins/z
Now simply source the .zsh file in your global zshrc by adding the following line to it
source /usr/share/zsh/plugins/z/zsh-z.plugin.zsh
Also be sure to set the ZSHZ_DATA variable in /etc/zsh/zshenv by adding the following line to it
export ZSHZ_DATA=~/.config/zsh/.z
You can install any more you like, just be sure it doesn't slow down your shell.
Updating ZSH Plugins with Pacman
Updating these plugins is important for compatibility and new features.
Create a file for the script
vim ~/Scripts/update-zshplugins.sh
Add in the following content
git -C /usr/share/zsh/plugins/F-Sy-H pull
git -C /usr/share/zsh/plugins/alias-tips pull
git -C /usr/share/zsh/plugins/zsh-autosuggestions pull
git -C /usr/share/zsh/plugins/z pull
create a file in the pacman hooks directory for zsh plugins
sudo vim /usr/share/libalpm/hooks/zsh.hook
Add in the following text
[Trigger]
Operation = Upgrade
Type = Package
Target = *
[Action]
Description = Update ZSH Plugins
When = PostTransaction
Exec = /bin/bash /home/USERNAME/Scripts/update-zshplugins.sh
Save and exit and try to update your system, if you are lucky you have an update and you can see the script in action after the update.
ZSH Hotkeys
Just a simple overview of all the hotkeys you can use Up and Down Arrow = Browse history matching current line Ctrl + A = Go to start of line Ctrl + B = Move 1 character back Ctrl + C = Cancel, Stop the current operation. Ctrl + D = Quit the session Ctrl + E = Go to end of line Ctrl + F = Move 1 character forward Ctrl + G = Nothing Ctrl + H = DOUBLE Delete Character before the cursor Ctrl + I = Nothing Ctrl + J = Nothing Ctrl + K = Remove everything behind the cursor Ctrl + L = clear screen Ctrl + M = Nothing Ctrl + N = Browse History matching current line Ctrl + O = Nothing Ctrl + P = Browse History matching current line Ctrl + Q = Quit, Types exit and presses enter. Ctrl + R = Search in History Ctrl + S = Nothing Ctrl + T = Nothing Ctrl + U = Delete everything before the cursor Ctrl + V = Nothing Ctrl + W = Delete the word before the cursor Ctrl + X = Nothing Ctrl + Y = Undo Ctrl + Z = toggle program to background and foreground
Installing Neofetch
Neofetch is a cool tool that shows some system information and a asci art logo of your distro. Install it with the following command
sudo pacman -S neofetch
Now you can run it by just typing neofetch in a terminal :)