-
Notifications
You must be signed in to change notification settings - Fork 3
Tips
The following function will search files matching given patterns and let you choose a file and copy its path to the system clipboard. The job of searching files will be done with lf command so the function accepts arguments as the exact same way as lf does.
_f() {
select a in "QUIT!" $( lf $* ); do
[ "$a" = "QUIT!" ] && break;
echo -n "$a" | _pbcopy; ### @@@ Customization Point!
echo "$a"; break;
done;
};
A usage example is as follows:
$ _f /usr/share/zoneinfo Sing --
1) QUIT!
2) /usr/share/zoneinfo/Asia/Singapore
3) /usr/share/zoneinfo/Singapore
#?
What differs to lf
is that the function prints each of paths found and let you interactively select which path you want. And furthermore, it will copy the path you choose to the system clipboard. ( If your system already has proper tools for that. Refer to this for further discussion. )
See the code marked with @@@ Customization Point!
in the function. What it does is copying the path the user has selected into the system clipboard. But, where has _pbcopy
thing come from? Actually when you load lf.sh
script into your shell session by . lf.sh
or source lf.sh
command, these helper shell functions _pbcopy
or _pbpaste
will also be loaded into your shell session and you can use them freely.
If you are an OS X user, you may be familiar with pbcopy
or pbpaste
shell commands. These shell functions do the same job as pbcopy
/pbpaste
of OS X do. ( Those shell functions will be available on other systems like Linux or Cygwin, too )
You can compress the function above into a more concise Bash alias like so:
alias sf='_f() { select a in "QUIT!" $( lf $* ); do [ "$a" = "QUIT!" ] && break; echo -n "$a" | _pbcopy; echo "$a"; break; done; }; _f'
Copy and paste that alias to your .bashrc
and reload it. Then it's ready for your service!
$ sf /usr/share/zoneinfo Sing --
1) QUIT!
2) /usr/share/zoneinfo/Asia/Singapore
3) /usr/share/zoneinfo/Singapore
#?
Also you can change that customization point for whatever needs. Say, you want to find files from a simple pattern (at which lf
is really good) and select a file among them and edit that file right away. Just replace that customization point with the following code.
vim "$a"
Again, you can define an alias for that task like so:
alias sv='_f() { select a in "QUIT!" $( lf $* ); do [ "$a" = "QUIT!" ] && break; vim "$a"; break; done; }; _f'
The idea is similar. But, we use lfi
instead of lf
because case insensitive pattern matching is more useful in this case.
alias whattime='_f() { select a in "QUIT!" $( cd /usr/share/zoneinfo; lfi . $* -- ); do [ "$a" = "QUIT!" ] && break; TZ="$a" date "+DATE: %y/%m/%d%nTIME: %H:%M:%S"; break; done; }; _f'
For instance, when you want to find out what time it is in New York city
, then use.
$ whattime new y
1) QUIT!
2) America/New_York
#? 2
DATE: 17/06/15
TIME: 17:21:00
You don't have to remember the exact name of the target timezone thanks to lfi
. Typing new y
(and case insensitive) is enough instead of typing America/New_York
.
One downside is you may not be able to directly find out the current time in your specific favorite city because
/usr/share/zoneinfo
doesn't contain every major city in the world. Say, you want to directly know the current time inSan Francisco, CA
, but you can't. So sad.
$ whattime san fran
1) QUIT!
#? 1
You can use
whattime los
(forAmerica/Los_Angeles
) instead.
✔️ Edit All Files Found by lf or lfi
By simply typing lfs or lff, you can print out all the file paths found by the previous call of lf or lfi
.
And using vim -p [files ...]
, you can load multiple files into Vim tab pages at once.
e.g)
$ lf tools/ data .sh
tools/data-management/cvzdata-console.sh
tools/data-management/cvzdata2csv.sh
tools/data-management/export-data.sh
tools/data-management/import-data.sh
$ vim -p $( lfs )
4 files to edit
Or, more simply, vim -p $( lf tools/ data .sh )
will do the same job, but you may want to first check if the found files by lf
are exactly what you need because mostly you wouldn't want to load any of irrelevant files into the editing session.
Most common pattern of using lf or lfi
is specifying a file extension at the end like lf src .java
. But some files may have a long file extension and you may feel stupid to type all that long extension. You can use --
notation in that case.
For instance, when you want to find out all .properties
files through src
folder, you may do like so:
$ lf src .properties
Or more cleverly, you may do the same job with:
$ lf src .pro --
Of course, it may depend on the situation. Say, the folder may contain .prop
files as well as .properties
files and then lf
will also return those irrelevant files. But a few of .prop
files may not bother you, or even if there are too many of them, you can type just a little more like lf src .prope --
and that's it.