dotfiles/.custom_bash_scripts
Sheldon Lee 4feed1672f Added function to set working directory.
If a working directory is set with wdir(), bashrc will cd into the
path stored in a file written in /tmp. A new terminal will then
have a shell in that directory.

cwdir() deletes the file in /tmp and pwdir() prints the working
directory from said file if it exists.
2022-06-18 19:48:34 +08:00

129 lines
2.4 KiB
Bash

#!/bin/sh
# init for dotfiles.
initdots()
{
config='/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME'
config checkout HEAD
source $HOME/.bashrc
}
# nnn
# for nnn file manager
n ()
{
# Block nesting of nnn in subshells
if [ -n $NNNLVL ] && [ "${NNNLVL:-0}" -ge 1 ]; then
echo "nnn is already running"
return
fi
# The default behaviour is to cd on quit (nnn checks if NNN_TMPFILE is set)
# To cd on quit only on ^G, remove the "export" as in:
# NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd"
# NOTE: NNN_TMPFILE is fixed, should not be modified
export NNN_TMPFILE="${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd"
# Unmask ^Q (, ^V etc.) (if required, see `stty -a`) to Quit nnn
# stty start undef
# stty stop undef
# stty lwrap undef
# stty lnext undef
nnn -A -x "$@"
if [ -f "$NNN_TMPFILE" ]; then
. "$NNN_TMPFILE"
rm -f "$NNN_TMPFILE" > /dev/null
fi
}
# cd into config dir
cfdir()
{
cd $HOME/.config/$1
}
# sshfs into newnumyspace
nnms_fs()
{
dir_name="newnumyspace"
path="${HOME}/dev"
if [ ! -d $path/$dir_name ]
then
mkdir $path/$dir_name
echo "Created new directory $dir_name at $path"
fi
sshfs unn_w19014537@newnumyspace.co.uk:/home/unn_w19014537/ $path/$dir_name/
if [ $? -eq 0 ]
then
echo "Mounted at $path/$dir_name."
fi
}
# pacman list sizes
pacsizes()
{
pacman -Qi $1 | awk '/^Name/{name=$3} /^Installed Size/{size=$4$5; print size, name;}' | sort -h
}
# view markdown files in zathura
viewmd()
{
[ -z $1 ] && return
[ ! -f $1 ] && return
pdfname=$(basename $1 .md).pdf
echo $pdfname
if [ -f $pdfname ]; then
zathura $pdfname
else
pandoc -t pdf $1 | zathura -
fi
}
# generate markdown files from markdown
makemd()
{
[ -z $1 ] && return
[ ! -f $1 ] && return
pandoc -t pdf $1 -o $(basename $1 .md).pdf
}
# view zathura in tabbed
tzathura()
{
# rm tabbbed.xid if not running
! pgrep -f tabbed && rm /tmp/tabbed.xid
if [ ! -f /tmp/tabbed.xid ]; then
tabbed -c -d > /tmp/tabbed.xid
fi
zathura "$@" -e $(</tmp/tabbed.xid) & disown
}
wdir()
{
wdir="$PWD/$1"
[ -d "$wdir" ] && echo "$wdir" > "$WDIR_PATH" && echo "set $wdir"
}
pwdir()
{
if [ -f "$WDIR_PATH" ]
then
cat "$WDIR_PATH"
else
echo "Working directory not set."
fi
}
cwdir()
{
if [ -f "$WDIR_PATH" ]
then
rm -f "$WDIR_PATH"
echo "Cleared working directory."
else
echo "Working directory not set."
fi
}