Last active
February 28, 2019 08:14
-
-
Save c02y/ec35f9bc228cc931bea3404f027dcbf5 to your computer and use it in GitHub Desktop.
alias to abbr, and tweak function fu
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
0. abbr lesst 'less ~/.tmux.conf' | |
0.1 abbr lessf 'less ~/.config/fish/config.fish' | |
0.2 abbr lesse 'less ~/.emacs.d/init.el' | |
1. replace alias to abbr | |
1.1 exclute such as rg, ls, ll, lla, lah, rm, cp, mv, du, less, wget, emx, time, ping, find | |
fine they will be used in other functions | |
2. delete $argv[1] part in m* abbrs | |
3. delete t-c* abbrs | |
4. make abbr containing abbr to function | |
5. set -gx FISH_CONFIG_PATH ~/.config/fish/config.fish | |
6. replace all config.fish and ~/.config/fish/config.fish with $FISH_CONFIG_PATH | |
7. tweak fu function to handle abbr | |
8. remove abbrs of egrep/fgrep | |
9. `bind \cl 'clear; commandline -f repaint; path_prompt'` for path_prompt without tmux running | |
10. find to find -L | |
11. rm . abbr/alias | |
12. add function sudo | |
13. abbr ls. 'ls -A' to abbr lsa 'ls -A' | |
14. abbr lessv 'less ~/.vim/vimrc' | |
13. add function agv | |
14. add function elpac | |
16. add auto-source function | |
17. remove echo part in varclear function | |
18. add `varclear PATH`, replace . with source in fsr function | |
19. replcae status part with one line in if | |
21. redefine debx | |
22. redefine lsx | |
23. replace `if type -q ag` to `if command -sq ag` | |
24. change `abbr vimf ...` to `abbr vimf $FISH_CONFIG_PATH` | |
25. abbr condas 'binstar search -t conda' # [packagename] | |
26. using condai to install ncurses, emacs, tig, tmux, the_silver_searcher(ag), source-highlight, fish | |
22. | |
function lsx -d 'cp the full path of a file/dir to sytem clipboard' | |
if test $DISPLAY | |
if test -f $argv -o -d $argv | |
readlink -fn $argv | xc | |
xc -o | |
echo \n---- Path Copied to Clipboard! ---- | |
end | |
else | |
echo DISPLAY is not available! | |
end | |
end | |
21. | |
function debx -d 'extract the deb package' | |
set pkgname (basename $argv[1] .deb) | |
mkdir -v $pkgname | |
if command -sq dpkg # check if dpkg command exists, replace which | |
dpkg -x $argv[1] $pkgname | |
else | |
set dataname (ar t $argv[1] | ag data) | |
if not ar p $argv[1] $dataname | tar Jxv -C $pkgname ^/dev/null # failed, $status != 0 | |
if not ar p $argv[1] $dataname | tar zxv -C $pkgname ^/dev/null # failed, $status != 0 | |
rm -rfv $pkgname | |
return | |
end | |
end | |
end | |
echo ----in $pkgname ---- | |
end | |
16. | |
function auto-source --on-event fish_preexec -d 'auto source config.fish if gets modified!' | |
if not set -q FISH_CONFIG_TIME # if FISH_CONFIG_TIME not set, status != 0 | |
set -g FISH_CONFIG_TIME (date +%s -r $FISH_CONFIG_PATH) | |
else | |
set FISH_CONFIG_TIME_NEW (date +%s -r $FISH_CONFIG_PATH) | |
if test "$FISH_CONFIG_TIME" != "$FISH_CONFIG_TIME_NEW" | |
fsr | |
set FISH_CONFIG_TIME (date +%s -r $FISH_CONFIG_PATH) | |
end | |
end | |
end | |
19. | |
in function pk: | |
change | |
kill -9 $pid | |
if test $status != 0 # Operation not permitted | |
to | |
if not kill -9 $pid # failed to kill, $status != 0 | |
change | |
kill -9 $i | |
if test $status != 0 # Operation not permitted | |
to | |
if not kill -9 $i # failed to kill, $status != 0 | |
change | |
kill -9 $pid_of_index | |
if test $status != 0 # kill failed | |
to | |
if not kill -9 $pid_index # kill failed, $status != 0 | |
change | |
kill -9 $pid_part | |
if test $status -eq 1 # kill failed | |
to | |
if not kill -9 $pid_part # kill failed, $status != 0 | |
in function po | |
change | |
timeout 1 ping... | |
if test $status != 0 | |
to | |
if not timeout 1 ping... # failed, $status != 0 | |
in function pl (two parts) | |
change | |
timeout 1 ping... | |
if test $status != 0 | |
to | |
if timeout 1 ping... # failed, $status != 0 | |
in function ut | |
ip link ls... | |
if not ip link ...# failed, $status != 0, | |
... | |
14. | |
function elpac -d 'clean old packages in .emacs.d/elpa/' | |
set -l elpa_path ~/.emacs.d/elpa | |
# ls contains color which will affect the $pkg string, * part means only directories | |
for pkg in (command ls $elpa_path) | |
if echo $pkg | grep -q '[0-9]' # check if pkg $contains version number, $status = 0 | |
set -l pkg_ver (echo $pkg | sed 's!.*-!!') | |
set -l pkg_name (echo $pkg | sed 's/[0-9.]*$//g') | |
set pkg_com (echo $pkg_name(echo $pkg_ver | cut -c1)) | |
if test (command ls $elpa_path | grep "^$pkg_com" | wc -l) -gt 1 | |
set first (command ls $elpa_path | grep "^$pkg_com" | head -1 | sed "s/$pkg_com//g") | |
set second (command ls $elpa_path | grep "^$pkg_com" | sed -n "2p" | sed "s/$pkg_com//g") | |
if echo $first$second | grep -q '[a-zA-Z]' | |
continue # files/dirs like let-alias-1.0.5 and let-alias-1.0.5.signed | |
end | |
echo pkg: $pkg, first: $first, second:$second | |
if test (echo "$first < $second" | bc) -eq 1 | |
echo $pkg_com$first is lt | |
echo $pkg_com$second | |
set old $first | |
else if test (echo "$first > $second" | bc) -eq 1 | |
echo $pkg_com$first is gt | |
echo $pkg_com$second | |
set old $second | |
end | |
command rm -rf $elpa_path/$pkg_com*$old | |
echo | |
end | |
end | |
end | |
end | |
2. | |
abbr me 'm ~/.emacs.d/init.el' | |
abbr mh 'm /etc/hosts' | |
abbr m2 'm ~/Recentchange/TODO' | |
abbr mf 'm ~/.config/fish/config.fish' | |
12. | |
function sudo -d 'make sudo accpet the defined alias/functions' | |
if functions -q $argv[1] | |
set argv fish -c "$argv" | |
end | |
command sudo $argv | |
end | |
13. | |
function agv -d 'ag sth. in ~/Recentchange/TODO' | |
ag $argv[1] ~/.vim/vimrc | |
end | |
4. | |
function ok -d 'open file with okular in terminal without blockint it' | |
bash -c "(nohup okular \"$argv\" </dev/null >/dev/null 2>&1 &)" | |
end | |
function ima -d 'open file with gwenview in terminal without blocking it' | |
bash -c "(nohup gwenview \"$argv\" </dev/null >/dev/null 2>&1 &)" | |
end | |
function op -d 'open file with xdg-open in terminal without block it' | |
bash -c "(nohup xdg-open \"$argv\" </dev/null >/dev/null 2>&1 &)" | |
end | |
7. | |
function fu -d 'fu command and prompt to ask to open it or not' | |
# $argv could be builtin keyword, function, alias, file(bin/script) in $PATH, abbr | |
# And they all could be defined in script or temporally (could be found in any file) | |
set found 0 | |
# Check `type` output, NOTE: `type` doesn't support abbr | |
if type $argv ^/dev/null # omit the result once error(abbr or not-a-thing) returned, $status = 0 | |
set found 1 # for not-a-thing | |
set result (type $argv) | |
end | |
# NOTE: $argv may also be defined as an abbr like rm command | |
abbr --show | grep -w "abbr $argv" # Space to avoid the extra abbr starting with $ARGV | |
if test $status = 0 | |
# in case $argv existes in both `type` and `abbr --show` | |
if test $found = 1 -a (echo (grep -w -E "^alias $argv|^function $argv" $FISH_CONFIG_PATH)) | |
echo "$argv is in both `type` and `abbr --list`, found definition in $FISH_CONFIG_PATH" | |
abbrc | |
# continue, use the result of `type` | |
else # only exists in `abbr --show` | |
set found 1 | |
set result (abbr --show | grep -w "abbr $argv") | |
end | |
else if test $status != 0 -a $found != 1 | |
echo "$argv is not a thing!" | |
return | |
end | |
set result_1 (printf '%s\n' $result | head -1) | |
if test (echo $result_1 | grep -w -E "abbr $argv|is a function with definition") # defined in fish script | |
if test (echo $result_1 | grep -E "is a function with definition") | |
# 1. function or alias -- second line of output of fu ends with "$path @ line $num_line" | |
set -l result_2 (printf '%s\n' $result | sed -n "2p") | |
set def_file (echo $result_2 | awk -v x=4 '{print $x}') | |
if test "$def_file" = "-" # alias, no definition file is printed | |
set def_file $FISH_CONFIG_PATH | |
end | |
set num_line (grep -w -n -E "^alias $argv|^function $argv" $def_file | cut -d: -f1) | |
if not test $num_line # empty | |
echo "$argv is an alias/functions in `alias/functions` but not defined in $def_file, may be defined temporally or in other file!" | |
if test $def_file = $FISH_CONFIG_PATH | |
functions -e $argv | |
echo "$argv is erased!" | |
end | |
return | |
end | |
else # 2. abbr | |
set def_file $FISH_CONFIG_PATH | |
set num_line (grep -w -n "^abbr $argv" $def_file | cut -d: -f1) | |
if not test $num_line # empty | |
echo "$argv is an abbr in `abbr --show` but not defined in $FISH_CONFIG_PATH, may be defined temporally or in other file!" | |
if test $def_file = $FISH_CONFIG_PATH | |
abbr -e $argv | |
echo "$argv is erased!" | |
end | |
return | |
end | |
end | |
echo | |
read -n 1 -p 'echo "Open the file containing the definition? [y/N]: "' -l answer | |
if test "$answer" = "y" -o "$answer" = " " | |
vim $def_file +$num_line | |
end | |
else if test (echo $result_1 | grep -i "is a builtin") | |
# 3. $argv in builtin like if | |
return | |
else # 4. $argv is a file in $PATH | |
set -l file_path (echo $result_1 | awk 'NF>1{print $NF}') | |
file $file_path | grep "symbolic link" # print only $argv is symbolic link | |
file (readlink -f $file_path) | grep -E "ELF|script|executable" # highlight | |
if test (file (readlink -f $file_path) | grep "script") # script can be open | |
echo | |
read -n 1 -p 'echo "Open the file for editing?[y/N]: "' -l answer | |
if test "$answer" = "y" -o "$answer" = " " | |
vim $file_path | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment