Skip to content

Latest commit

 

History

History
168 lines (104 loc) · 4.45 KB

dev.md

File metadata and controls

168 lines (104 loc) · 4.45 KB

Development

Developer notes.

Notes

Terminal

Tab Title

To set the title of a terminal tab,

$ echo -n -e "\033]0;{{title}}\007"

To package as a command-line tool, add the following to the platform-specific configuration file for configuring user environments (e.g., .bash_profile, .profile, .bashrc.

tab() {
    echo -n -e "\033]0;$*\007"   
}

which can then be invoked

$ tab title

ls

Directory Tree

To generate a directory tree,

$ ls -R ./root/directory | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/--/|/'

where

  • -R: recursively list subdirectories.
  • s/[^-][^\/]*\//--/g: replace directory path segments with --.
  • s/^/ /: indent.
  • s/--/|/: replace the first -- with a vertical bar.

git

git diff

To list changes without context,

$ git diff -U0 | grep '^[+-]' | grep -Ev '^(--- a/|\+\+\+ b/)'

which selects for all lines beginning with either a + or - character and then removes lines listing the filename.

Search Commits

To search all commits for a particular string,

$ git rev-list --all | xargs git grep -F 'string'

where -F indicates to search for a fixed string. To search using a regular expression, use -P (see git grep --help).

Find and Replace

Exclusion

To exclude paths when using find, use membership inversion

$ find -type f -name foo.txt -regextype posix-extended -regex '.*/foo/([^b]+|(b([^a]|$)|ba([^r]|$)))+/.*'

Note that, when passing the above regular expression through Make, the $ symbol needs to be escaped (e.g., $$).

Multi-file

To perform a multi-file find and replace,

$ perl -pi -w -e 's/search/replace/g;' $(find ./search/directory -type f)

where

  • -e: execute the command
  • -w: write warnings
  • -p: execute for each file
  • -i: edit in-place

If you encounter an error due to too many arguments, use xargs.

$ find ./search/directory -type f | xargs perl -pi -w -e 's/search/replace/g;'

If running a search from the top-level directory, be sure to exclude any hidden directories (including .git), the top-level node_modules directory, and the ./deps directory from the search. This may require using absolute file paths.

$ find "$PWD" -type f '!' -path "$PWD/.*" '!' -path "$PWD/deps/*" '!' -path "$PWD/node_modules/*" | xargs perl -pi -w -e 's/search/replace/g;'

A few comments:

  • For simple cases, sed may be faster.
  • Be very careful when performing a multi-file find and in-place replace. Perform dry-runs and confirm expected results on a small file subset before performing on many files. You have been warned.

Reorganization

Multi-directory

To move directories from one directory to another directory,

$ find $PWD/path/to/parent/directory -type d -depth 1 -regex ".*" | while read -r dir; do mv "${dir}" "$PWD/path/to/parent/destination/directory/$(basename ${dir})"; done

To rename multiple directories using a pattern,

$ find $PWD/path/to/parent/directory -type d -depth 1 -regex ".*" | while read -r dir; do mv "${dir}" "$PWD/path/to/parent/destination/directory/`echo $(basename ${dir}) | sed s/search/replace/`"; done