Unix as IDE | Tom Ryder

Find command

Find without an argument find everything in the current folder.

find | sort

-ls flag adds ls -l style listing -k 11 tells to sort by 11th column of output (on filenames)

find -ls | sort -k 11

Find names based on shell pattern:

find -name '*.md'

Find files matching a particular path:

find -path '*test*'

Use -iname or -ipath for case insensitivity.

Find markdown files that have been edited in the last 5 days (or +5 for everything before 5 days):

find -name '*.md' -mtime -5

Apply a command on the find result, e.g. open all resulting files in vim for editing:

find -name '*.md' -exec vim {} \;

or terminate with +. The difference is not very clear to me. Ryder says that + puts "all of the results on one invocation of the command."

find -name '*.md' -exec vim {} +

Example of me finding the directory of my personal website based on a pattern, and open that directory in Vim:

find . -type d -iname "*personal*" -exec vim {} \;

N.B. using | xargs is way faster according to this blog:

time find . -type f -name "*.txt" -exec rm {} \;
0.35s user 0.11s system 99% cpu 0.467 total

time find ./foo -type f -name "*.txt" | xargs rm
0.00s user 0.01s system 75% cpu 0.016 total

Searching through files

Search recursively (-R) and case insensitive (-i) through current folder and subfolders: grep -iR somevar .

Excluding git files with -v. F is here used because we pass a fixed string and not a regex pattern.

grep -iR somevar . | grep -vF .git

Reading output from commands

:r!ls

The read function also accepts files directly!

:r ~/test.md

Things I often forget but should not

Use Ctrl-Z to quickly move a vim session to the back, use fg to bring it back. (Use this i.c.w. Vim!)