Last active
April 19, 2025 05:00
-
-
Save mitry/05fad6200c84970f0ade948b02c1eb9f to your computer and use it in GitHub Desktop.
my customized mksh startup script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # @(#) ~mitry/mkshrc for MkSh on Android | |
| # @url https://termbin.com/b8fn -- 2023-04-28 version | |
| # @url https://snippet.host/hzhrsr | |
| # If not running interactively, don't do anything | |
| [[ -o interactive ]] || return | |
| # [[ $- != *i* ]] && return | |
| # Prettify error messages: | |
| (( ${#0} > 10 )) && exec -a mksh $0 "$@" | |
| #: ${USER:=$(id -un)} ${HOSTNAME:=$(getprop ro.product.device)} | |
| : ${USER:=me} ${HOSTNAME:=mi3phone} ${HOME:=/sdcard} | |
| : ${SHELL:='~/bin/mksh'} | |
| #: ${TMPDIR:=/data/local/tmp} # chmod a=rwx,o+t $TMPDIR | |
| : ${TMPDIR:=~/../tmp} | |
| : ${TERM:=vt100} | |
| : ${LANG:=ru_RU.UTF-8} ${TZ:=Europe/Moscow} | |
| : ${TERMINFO:=~/.terminfo} | |
| #: ${ENV:=~/.mkshrc} | |
| # by mitry | |
| PATH=~/bin:$PATH | |
| (( USER_ID )) || HOME='/root' | |
| # by mitry: clear Java environment | |
| #unset BOOTCLASSPATH DEX2OATBOOTCLASSPATH SYSTEMSERVERCLASSPATH | |
| export HOME HOSTNAME MKSH SHELL TERM USER TMPDIR PATH TERMINFO | |
| export LANG TZ # LC_ALL=ru_RU.UTF-8 | |
| export COLORTERM=trucolor | |
| export LD_LIBRARY_PATH=~/lib | |
| export PREFIX=/data/data/hackpal.androidterm/app_HOME | |
| export CURL_CA_BUNDLE=~/usr/etc/tls/cacert.pem | |
| export MAGIC=/sdcard/.config/magic.mgc | |
| export WGETRC=~/.wgetrc | |
| # less environment not supported by busybox? | |
| export PAGER=less LESS='-FMRX~' LESSHISTFILE=- | |
| : export XDG_CONFIG_HOME=~/.config | |
| : export XDG_DATA_HOME=~/.local/share | |
| : export XDG_CACHE_HOME=~/.cache | |
| : export INPUTRC="$XDG_CONFIG_HOME/shell/inputrc" | |
| ## bat can be used as a colorizing pager for man, | |
| # by setting the MANPAGER environment variable: | |
| : export MANPAGER="sh -c 'col -bx | bat -l man -p'" | |
| export MANPATH="${HOME}/.local/share/man:${MANPATH}" | |
| # bat configuration: `bat --config-file` | |
| : export BAT_CONFIG_PATH=~/.bat.config | |
| : export BAT_THEME='Solarized (dark)' | |
| : export BAT_STYLE='grid,header-filename' | |
| # eval $(resize) ; export MANWIDTH=$COLUMNS | |
| : export GREP_COLORS='ms=01;31:mc=01;31:sl=:cx=:fn=:ln=:bn=:se=' | |
| # colored GCC warnings and errors | |
| : export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01' | |
| : export CLICOLOR=1 | |
| CDPATH=:~/storage:/sdcard:~ | |
| HISTFILE=~/.mksh_history | |
| : HISTSIZE=256 | |
| FCEDIT=nano | |
| #################### | |
| ## Source functions: | |
| #################### | |
| # [[ -r /etc/mkshrc ]] && source /etc/mkshrc | |
| for file in ~/.bashrc.d/*.sh; do | |
| source "$file" | |
| done; unset file | |
| ############## | |
| ## Command prompt: | |
| ############## | |
| : HOME=${HOME/%\/} # Remove trailing slash | |
| PS1=$'\1\r\1\e[1;37;48;5;166m\1' | |
| PS1+='${| local e=$?; (( e )) && REPLY+=" $e "; return $e }' | |
| PS1+=$'\1\e[0;38;5;108m\1' | |
| PS1+='$USER' | |
| PS1+=$'\1\e[37m\1@\1\e[0;38;5;108m\1' | |
| PS1+='$HOSTNAME' | |
| PS1+=$'\1\e[1;37m\1:\1\e[1;38;5;67m\1' | |
| PS1+='${PWD/#$HOME/\~}' | |
| PS1+=$'\1\r\1\n\e[0;37m\1' | |
| # PS1+=$(print -A "USER_ID? 0x2AF8 : 0x23") | |
| if (( USER_ID )); then | |
| PS1+='❯ ' # PS1+='➜ ' PS1+='⫸ ' PS1+='⚡ ' | |
| else | |
| PS1+='# ' | |
| fi | |
| PS1+=$'\1\e[m\1' | |
| #PS1+=$'\e]12;#990000\a' # cursor color and shape | |
| # Simple colored prompt: | |
| false && { | |
| PS1=$'\1\r\1\e[1;38;5;67m\1' | |
| PS1+='${PWD/#$HOME/\~}' | |
| PS1+=$'\1\e[1;' | |
| PS1+='$(($? ? 31:37))m' | |
| if (( USER_ID )); then | |
| PS1+=$'\1\$ \1\e[m\1' | |
| else | |
| PS1+=$'\1\# \1\e[m\1' | |
| fi | |
| # PS1+=$'\1\$(print -A "USER_ID? 0x2AF8 : 0x23") \1\e[m\1' | |
| } | |
| export PS1 | |
| ## Set text foreground/background to RGB color | |
| # \e[38;2;<R>;<G>;<B>m | |
| # \e[48;2;<R>;<G>;<B>m | |
| ############## | |
| ## Directory listing colors | |
| ############## | |
| # [[ -r ~/.dircolors ]] && eval $(dircolors -b ~/.dircolors) | |
| test -r ~/.dircolors && eval $(dircolors -b $_) | |
| ############## | |
| ## Aliases | |
| ############## | |
| ## Directory aliases | |
| # @usage cp x ~alias/subdir | |
| alias -d bin=~/bin | |
| alias -d sd=/sdcard | |
| alias -d c=~sd/.config | |
| alias -d d=~sd/Download | |
| alias -d md=~sd/Documents/MD-Notes | |
| alias -d x=~bin | |
| alias -d in=~c/2inst | |
| alias cls='clear&&clear' | |
| : alias cls='\\builtin print -n \\e[H\\e[2J' | |
| alias bb='~bin/busybox' | |
| : alias bb='/system/bin/toybox' | |
| alias cp='cp -vi' | |
| alias mv='mv -vvi' | |
| alias rm='rm -vi' | |
| alias ln='ln -v' | |
| alias du='du -h' | |
| alias df='df -h' | |
| alias mkdir='mkdir -vp' | |
| alias rsync='rsync -vrPlu' | |
| # `mount` output pretty and human readable: | |
| alias mount='mount |column -t' | |
| alias ,='\\builtin cd -' | |
| alias ..='\\builtin cd ..' | |
| alias ...='\\builtin cd ../..' | |
| alias ....='\\builtin cd ../../..' | |
| alias .....='\\builtin cd ../../../..' | |
| alias cd..='\\builtin cd ..' | |
| alias cd-='\\builtin cd -' | |
| ## ls setup | |
| # | |
| alias ls='ls --color=auto' | |
| alias l='ls -hGF --time-style iso' la='l -A' ll='l -l' lo='l -lA' l.='l -d .*' | |
| # type tree &>/dev/null || alias tree="\\command ls -R | grep ':$' | sed -e 's/:$//;s/[^─][^\/]*\//──/g;s/^/ /;s/─/├/'" | |
| type exa &>/dev/null && { # exa / eza -- ls replacements | |
| alias l='exa --git --classify --group-directories-first --color-scale --time-style iso' la='l -a' lo='l -la' | |
| alias tree='l --tree' | |
| export EXA_COLORS="xx=38;5;138:sn=38;5;138:sb=1;38;5;173:da=38;5;67:uu=38;5;108:du=38;5;108" | |
| } | |
| # toybox grep supports colored output: | |
| alias grep='grep --color=auto' | |
| : alias dd='dd status=progress' | |
| alias w='\\builtin whence' | |
| alias e='\\builtin echo' # not print, as it more curious about args | |
| alias h='\\builtin fc -l' | |
| alias nn=nano | |
| alias now='date +"%a %d %b %R"' | |
| alias path='\\builtin echo ${PATH//:/"\n"}' | |
| # install all from the `~in` aliased catalog | |
| alias inst='install -sm 755 -t ~/bin ~in/*' | |
| alias inst-lib='install -m 755 -t ~/lib ~in/*.so*' | |
| # Re-run last command as root | |
| : alias 's!'='su -c $(fc -lnr -1)' | |
| alias doch='su -c "$(\builtin fc -ln -1)"' | |
| alias plz=doch | |
| alias www='wget -qO-' | |
| alias drill='drill -c ~/etc/resolv.conf' | |
| # IP addresses | |
| alias myip='drill -Q myip.opendns.com @resolver1.opendns.com' | |
| : alias myip='dig -r +short myip.opendns.com @resolver1.opendns.com' | |
| : alias myip='www http://ip4only.me/api/ | cut -d, -f2' | |
| : alias localip="ipconfig getifaddr en0" | |
| ## termbin.com -- Netcat-based command line pastebin. | |
| # @usage: echo just testing! | nc termbin 9999 | |
| alias tb='nc termbin.com 9999' | |
| ## Android apps: | |
| : alias pml='pm list packages' | |
| : alias start='am start --user 0 -a android.intent.action.VIEW -d' | |
| : alias play='am start --user 0 -n org.videolan.vlc/org.videolan.vlc.gui.video.VideoPlayerActivity -a android.intent.action.VIEW -d' | |
| ############## | |
| ## Key binding | |
| ############## | |
| bind ^L=clear-screen | |
| : bind ^T=backward-word | |
| : bind ^G=forward-word | |
| bind ^P=search-history-up | |
| bind ^N=search-history-down | |
| : bind ^X,=prev-hist-word | |
| bind ^[p=up-history | |
| bind ^[n=down-history | |
| : bind ^[,=set-mark-command | |
| : bind ^[w=kill-region | |
| : bind ^[^[=list | |
| : bind ^X^V=version | |
| # unbind <*> : fix by mitry for mksh-R41 | |
| : bind -m '*=*' | |
| : bind *=auto-insert | |
| ############## | |
| # Functions | |
| ############## | |
| sudo() su -c "$@" | |
| ## Highlighting `--help` messages with the batcat | |
| # and bind this function to Esc-e key | |
| _help() { | |
| \command "$@" --help 2>&1 | bat -plhelp --file-name "$@" | |
| } | |
| bind -m '^[e=^A _help ^[f^K^J' | |
| ## Explaining Shell Commands via manker.com | |
| # @usage boy 'cmd -o | ...' one quoted command to explain it | |
| # @usage boy ls -lH | |
| boy() { # synonyms for `man`: boy, chap, dude, gay... | |
| curl -Gso- "https://www.mankier.com/api/v2/explain/?cols=${COLUMNS}" --data-urlencode "q=$*" | bat -pl help | |
| } | |
| ## Get man page from mankier.com | |
| # @TODO fix pandoc params | |
| manker() { | |
| curl -LGso- --data-urlencode "q=$*" https://www.mankier.com/ \ | |
| | pandoc -t markdown -f html --normalize --columns ${COLUMNS} \ | |
| | bat -l markdown | |
| } | |
| ## Weather console report | |
| # @args: Location / ?M&format=4 / ?0M / :help | |
| wttr() www -T5 http://ru.wttr.in/${@:-?M0} | |
| ## Online cheat sheets / TL;DR lookup | |
| cheat() www -T5 http://cheat.sh/${@:-:help}?style=pastie | |
| alias tldr=cheat | |
| ## httpbin -- сервис отладки HTTP запросов и ответов | |
| httpbin() www http://httpbin.org/${@:-ip} | |
| ## Starwars in ascii-art: | |
| : alias starwars='telnet towel.blinkenlights.nl' | |
| ## Remount RW/RO filesystem | |
| # @usage rw | |
| # @usage rw /mnt/point | |
| # @usage rw / ro | |
| rw() su -c mount -o remount,${2:-rw} ${1:-/system} | |
| ## Set *xterm title | |
| title() print -n "\e]0;${*:-$USER @ $HOSTNAME}\07" | |
| ## Отцентровать сообщение на экране | |
| # @usage center "hello world" | |
| # @usage while read; do center "$REPLY" ; done < file.txt | |
| center() printf '%*s%s\n' $(( ( $COLUMNS - ${#1} ) / 2 )) ' ' "$1" | |
| ## Run `dig` and display the most useful info | |
| digga() dig +nocmd "$1" any +multiline +noall +answer | |
| ## Display unique lines (@TOTEST) | |
| dedup() awk '! x[$0]++' $@ | |
| ## Count files or directories in directory | |
| # @usage count /path/to/dir/* | |
| # @usage count /path/to/dir/*/ | |
| count() \\builtin print "$#" | |
| ######## | |
| umask 022 | |
| : if [[ -o login ]] | |
| : then | |
| [[ -d ~ ]] && cd ~ | |
| (( !$? )) && { | |
| : clear | |
| #[[ -r ~/etc/motd ]] && print -e "\e[2;37m$(<~/etc/motd)\e[m\n" | |
| test -r ~/etc/motd && print -e "\e[2;37m$(<$_)\e[m\n" | |
| } | |
| : fi | |
| # test -r ~/.mkshrc && source $_ | |
| : # EOF # vim:filetype=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment