-
Notifications
You must be signed in to change notification settings - Fork 0
/
lfrc
229 lines (203 loc) · 6.19 KB
/
lfrc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# {{{1 set options
# enable file icons
set icons
# interpreter for shell commands
set shell fish
# file previewer
set previewer ~/scripts/lf/lf_preview
# set '-eu' options for shell commands
# These options are used to have safer shell commands. Option '-e' is used to
# exit on error and option '-u' is used to give error for unset variables.
# Option '-f' disables pathname expansion which can be useful when $f, $fs, and
# $fx variables contain names with '*' or '?' characters. However, this option
# is used selectively within individual commands as it can be limiting at
# times.
# set shellopts '-eu'
# set internal field separator (IFS) to "\n" for shell commands
# This is useful to automatically split file names in $fs and $fx properly
# since default file separator used in these variables (i.e. 'filesep' option)
# is newline. You need to consider the values of these options and create your
# commands accordingly.
# set ifs "\n"
# show relative line number
set relativenumber
# leave some space at the top and the bottom of the screen
set scrolloff 10
# {{{1 commands
# https://github.com/gokcehan/lf/wiki/Ranger#rifle
# This relies on rifle.py from ranger
# Open
cmd open ${{
set file_info (file -Lb --mime $fx)
set empty_file (string match -qr inode/x-empty $file_info; and echo 1; or echo 0)
set binary_info (string match -r charset=\.+ $file_info)
# whether this file type is binary
if string match -qr -- binary $binary_info; and test $empty_file = 0
# binary file and not empty, open with GUI softwares
if type -q open
# use 'open' command for mac OS
open $fx
else
# rifle -p 0 $fx
end
else
# text file
$EDITOR $fx
end
}}
# https://www.reddit.com/r/ranger/comments/k6kkwr/how_to_open_a_file_with_another_app_without/
cmd open-with ${{
# rifle -l $fx
read -p "echo Open with: " method
# rifle -p $method $fx
if test -n $method
$method $fx
end
}}
# map r open-with
# define a custom 'open' command
# This command is called when current file is not a directory. You may want to
# use either file extensions and/or mime types here. Below uses an editor for
# text files and a file opener for the rest.
# cmd open ${{
# test -L $f && f=$(readlink -f $f)
# case $(file --mime-type $f -b) in
# text/*) $EDITOR $fx;;
# *) for f in $fx; do setsid $OPENER $f > /dev/null 2> /dev/null & done;;
# esac
# }}
# define a custom 'rename' command without prompt for overwrite
# cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
# map r push :rename<space>
cmd on-cd &{{
# update z.lua cd frequency
# env varialbes are found in fish shell(tried out)
# reference to https://github.com/skywind3000/z.lua/blob/master/ranger_zlua.py
test -n "$ZLUA_SCRIPT" and $ZLUA_SCRIPT --add $PWD
# Configure __fish_git_prompt
set -g __fish_git_prompt_show_informative_status true
set -g __fish_git_prompt_showcolorhints true
# add git branch info to lf prompt
set -l git (__fish_git_prompt)
set -l fmt "\033[32;1m%u@%h\033[0m:\033[34;1m%d\033[0m\033[1m%f\033[0m$git\033[0m"
# update lf prompt
lf -remote "send $id set promptfmt \"$fmt\""
}}
# move current file or selected files to trash folder
# (also see 'man mv' for backup/overwrite options)
# cmd trash %set -f; mv $fx ~/.trash
cmd trash %{{
set -l fx (string split \n $fx)
trash-put $fx; and echo "File(s) $fx moved to trash."
}}
cmd delete ${{
printf "$fx\n"
read -p "printf \"Delete above files? [y/n] \"" ans
if test $ans = "y"
set -l fx (string split \n $fx)
if type -q trash-put
trash-put $fx
else
rm -rf $fx
end
echo "File(s) $fx deleted."
end
}}
# extract the current file with the right command
# (xkcd link: https://xkcd.com/1168/)
# cmd extract ${{
# set -f
# case $f in
# *.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar xjvf $f;;
# *.tar.gz|*.tgz) tar xzvf $f;;
# *.tar.xz|*.txz) tar xJvf $f;;
# *.zip) unzip $f;;
# *.rar) unrar x $f;;
# *.7z) 7z x $f;;
# esac
# }}
# compress current file or selected files with tar and gunzip
cmd tar ${{
set -l fx (string split \n $fx)
mkdir $argv[1]
cp -r $fx $argv[1]
tar czf $argv[1].tar.gz $argv[1]
rm -rf $argv[1]
}}
# compress current file or selected files with zip
cmd zip ${{
set -l fx (string split \n $fx)
mkdir $argv[1]
cp -r $fx $argv[1]
zip -r $argv[1].zip $argv[1]
rm -rf $argv[1]
}}
# cmd bulk-rename ${{
# old="$(mktemp)"
# new="$(mktemp)"
# [ -n "$fs" ] && fs="$(ls)"
# printf '%s\n' "$fs" >"$old"
# printf '%s\n' "$fs" >"$new"
# $EDITOR "$new"
# [ "$(wc -l < "$new")" -ne "$(wc -l < "$old")" ] && exit
# paste "$old" "$new" | while IFS= read -r names; do
# src="$(printf '%s' "$names" | cut -f1)"
# dst="$(printf '%s' "$names" | cut -f2)"
# if [ "$src" = "$dst" ] || [ -e "$dst" ]; then
# continue
# fi
# mv -- "$src" "$dst"
# done
# rm -- "$old" "$new"
# lf -remote "send $id unselect"
# }}
# use fzf to search z.lua jump targets
cmd zj ${{
clear
read -p "printf 'z.lua jumps to: '" location
set -l target_path ""
if test -n "$location"
set target_path (~/scripts/lf/z-echo.fish $location)
end
if test -n "$target_path"
lf -remote "send $id cd \"$target_path\""
else
printf "Can't find related path from z.lua.\n"
read -p "printf '[Enter] to quit '"
end
}}
# use fd and fzf to jump to sub-directory
cmd fdj ${{
clear
set target_path (fd . $PWD -t d | command fzf)
if test -n "$target_path"
lf -remote "send $id cd \"$target_path\""
end
}}
# {{{1 mappings
# use enter for shell commands
map <enter> shell
# execute current file (must be executable)
map x $$f
map X !$f
# dedicated keys for file opener actions
map o &mimeopen $f
map O $mimeopen --ask $f
map a zj
map A fdj
# make sure trash folder exists
%mkdir -p ~/.trash
# use '<delete>' key for either 'trash' or 'delete' command
map <delete> trash
map D delete
# map <delete> delete
map b clear
# create file or directory
map c push $mk<space> # mk function defined in fish shell config
# preview
# set previewer ~/scripts/scope-lf-wrapper.sh
# map i $LESSOPEN='| ~/scripts/scope-lf-wrapper.sh %s' less -R $f
# open selected file/directory with input command
cmd open-with %"$@" "$fx"
map ` push :open-with<space>
# vim: set fdm=marker: