Split .custom_bash_scripts into a multiple files.

The files are in ~/.config/scripts/functions.
This commit is contained in:
Sheldon Lee 2022-06-26 08:43:03 +08:00
parent 2f1f2e7cb9
commit d9405c5166
6 changed files with 111 additions and 139 deletions

13
.bashrc
View File

@ -40,17 +40,8 @@ then
source ~/.bash_aliases
fi
# custom scripts
if [ -f ~/.custom_bash_scripts ]
then
source ~/.custom_bash_scripts
fi
# nnn bookmarks
if [ -f ~/.nnn_bookmarks ]
then
source ~/.nnn_bookmarks
fi
# source custom functions.
for f in ~/.config/scripts/functions/*; do source "$f"; done
if [ -f $WDIR_PATH ]
then

View File

@ -0,0 +1,30 @@
#!/bin/bash
# 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
}

View File

@ -0,0 +1,7 @@
#!/bin/bash
# pacman list sizes
pacsizes()
{
pacman -Qi $1 | awk '/^Name/{name=$3} /^Installed Size/{size=$4$5; print size, name;}' | sort -h
}

View File

@ -0,0 +1,38 @@
#!/bin/bash
#
# This is a collection of functions that manages a file /tmp as defined by
# $WDIR_PATH set in .bashrc. They basicaly manage a 'working directory', which
# the path of is stored in said file in /tmp.
#
# The .bashrc reads the path to the directory from the file and cd's into it
# when a new shell is created.
# Set the working directory.
wdir()
{
wdir="$PWD/$1"
[ -d "$wdir" ] && echo "$wdir" > "$WDIR_PATH" && echo "set $wdir"
}
# Print the workind directory.
pwdir()
{
if [ -f "$WDIR_PATH" ]
then
cat "$WDIR_PATH"
else
echo "Working directory not set."
fi
}
# Clear the workind directory.
cwdir()
{
if [ -f "$WDIR_PATH" ]
then
rm -f "$WDIR_PATH"
echo "Cleared working directory."
else
echo "Working directory not set."
fi
}

View File

@ -0,0 +1,34 @@
#!/bin/bash
# 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
}

View File

@ -1,128 +0,0 @@
#!/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
}