-
Notifications
You must be signed in to change notification settings - Fork 10
/
lfrc
240 lines (212 loc) · 5.59 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
230
231
232
233
234
235
236
237
238
239
240
# lf doc - https://godoc.org/github.com/gokcehan/lf
# lfrc example - https://github.com/gokcehan/lf/blob/master/etc/lfrc.example
# ========== General settings ==========
set shell bash
set shellopts '-eu'
set ifs "\n"
set scrolloff 10
set icons
# set drawbox
set incsearch
set smartcase
set ignorecase
set anchorfind
set findlen 1
set tabstop 4
set info size
set sixel
set preview
set previewer ~/.config/lf/previewer
set cleaner ~/.config/lf/cleaner
set dircounts
setlocal ~/Downloads sortby "atime"
setlocal ~/Downloads reverse
# ========== Commands ==========
# Override the default open for file opening (binded to 'l' by default)
cmd open &{{
case $(file --mime-type "$(readlink -f $f)" -b) in
text/*|application/json|inode/x-empty|application/octet-stream) lf -remote "send $id \$$EDITOR \$fx";;
*) for f in $fx; do open $f > /dev/null 2> /dev/null & done;;
esac
}}
cmd mkdir $mkdir -p "$(echo $* | tr ' ' '\ ')"
# Create a directory with the selected items
cmd new-folder-with-selection ${{
set -f
printf "Directory name: "
read newd
mkdir -- "$newd"
mv -- $fx "$newd"
}}
cmd chmod ${{
printf "\nMode Bits: "
read ans
for file in "$fx"
do
chmod $ans $file
done
lf -remote 'send reload'
}}
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;;
*.tar) tar xvf $f;;
*.zip) unzip $f;;
*.rar) unrar x $f;;
*.7z) 7z x $f;;
*) echo "Unsupported format";;
esac
}}
cmd tar ${{
set -f
mkdir $1
cp -r $fx $1
tar czf $1.tar.gz $1
rm -rf $1
}}
cmd zip ${{
set -f
mkdir $1
cp -r $fx $1
zip -r $1.zip $1
rm -rf $1
}}
cmd delete ${{
set -f
printf "\n$fx\n"
printf "delete?[y/N]"
read ans
if [[ $ans == "y" ]]; then
rm -rf $fx
fi
}}
cmd trash !{{
set -f
printf "\nItems to be trashed:\n$fx\n\nTrash? [y/N]"
read ans
if [[ $ans == "y" ]]; then
trash -F $fx && echo "Trash complete!"
elif [[ $ans == "n" ]]; then
echo "Canceled!"
else
echo "Failed! Use y to trash."
fi
}}
cmd select-all :unselect; invert
# Bulk rename on selected files or all the non-hidden files in the current directory if no selection
cmd bulk-rename ${{
old="$(mktemp)"
new="$(mktemp)"
if [ -n "$fs" ]; then
fs="$(basename $fs)"
else
fs="$(ls)"
fi
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"
}}
# Copy the file names (including extension) of the selections separated by \n
cmd copy-filename ${{
names="$(echo $fx | tr ' ' '\n' | xargs -I{} basename {})"
echo $names | tr ' ' '\n' | pbcopy
}}
# Copy the absolute paths of selections separated by \n
cmd copy-absolute-path ${{
echo $fx | tr ' ' '\n' | pbcopy
}}
# toggle the selections in fzf
cmd fzf-select ${{
IFS=' '
exclude=$(cat $HOME/exclude | sed 's/^/--exclude /' | tr '\n' ' ')
selections=$(fd --max-depth 1 --hidden --follow $exclude | fzf \
--multi \
--header ':: ENTER (toggle the selections), CTRL-R (recursively, no depth limit), CTRL-L (limited to depth 1)' \
--bind "ctrl-r:reload(fd --hidden --follow $exclude),ctrl-l:reload(fd --max-depth 1 --hidden --follow $exclude)" \
--prompt 'Files> ') || true
if [[ -n $selections ]]; then
# Escape white spaces and format multi-line string to a single line one
targets=$(echo "$selections" | sed 's/ /\\ /g' | tr '\n' ' ')
lf -remote "send $id toggle $targets"
fi
}}
# cd into the selected directory in fzf
cmd fzf-cd ${{
IFS=' '
exclude=$(cat $HOME/exclude | sed 's/^/--exclude /' | tr '\n' ' ')
select=$(fd --type d --hidden --follow $exclude | fzf --no-multi --header ':: ENTER (cd into the selected directory)' --prompt 'Dirs> ') || true
if [[ -n $select ]]; then
lf -remote "send $id cd $select"
fi
}}
# zoxide
cmd z %{{
result="$(zoxide query --exclude $PWD $@ | sed 's/\\/\\\\/g;s/"/\\"/g')"
lf -remote "send $id cd \"$result\""
}}
cmd zi ${{
result="$(zoxide query -i | sed 's/\\/\\\\/g;s/"/\\"/g')"
lf -remote "send $id cd \"$result\""
}}
# Git for dotfiles
cmd dot ${{
git --git-dir=/Users/rockyzhang/dotfiles/ --work-tree=/Users/rockyzhang $@
}}
# ========== Mappings ==========
# Remove some defaults
map gh
map d
map y
map <space>
map zh
map z. set hidden!
map <tab> :toggle;down
map yy copy
map yn copy-filename
map yP copy-absolute-path
map <enter> shell
map d delete
map T trash
# list the size of each item in the current directory
map U !printf "\n";du -csh *
map <c-f> fzf-select
map <c-j> fzf-cd
# give a name and then make a directory
map <c-n> push :mkdir<space>
map <a-n> new-folder-with-selection
map x cut
# give a name and then use neovim to edit it
map V push :!nvim<space>
# Navigate among the parent directories
map [ push hkl
map ] push hjl
# Rename
map r
map A rename # at the very end
map I push A<c-a> # at the very beginning
map rn push A<c-u> # rename the filename
map re push A<c-f><c-k> # rename the extension
map <c-r> bulk-rename
# Use <space> as the leader key
# compress (give a name like abc and it will compress the selected items to abc.zip)
map <space>c push :zip<space>
# extract
map <space>x extract
# Open the directory in Finder on macOS
map <space>f &if [[ -d $f ]]; then open $f; fi
cmap <tab> cmd-menu-complete
cmap <backtab> cmd-menu-complete-back