Skip to content

Instantly share code, notes, and snippets.

@seancolsen
Last active July 30, 2017 20:17
Show Gist options
  • Save seancolsen/6a4425936a6f112a50b2872fb136d90f to your computer and use it in GitHub Desktop.
Save seancolsen/6a4425936a6f112a50b2872fb136d90f to your computer and use it in GitHub Desktop.
Useful text snippets for random programming stuff

Useful snippets

A scratch pad of things that are hard to remember

vim

save with sudo

:w !sudo tee %

bash

Flags

  • Exit on first error
  • Throw error if referencing an undefined variable
set -e
set -u

reference

get current directory inside of script

SCRIPTPATH=$( cd "$(dirname "$0")" ; pwd -P )

run last command as sudo

sudo !!

GNU Screen

change current working directory

screen -X eval "chdir $PWD"

apt-get

Free up disk space

sudo apt-get clean
sudo apt-get autoclean
sudo apt-get autoremove

Text manipulation

Multi-line recursive find/replace

perl -0777 -i -pe 's|^(.*)\n={3,}$|# $1|gm' `find . -name '*.md'`

perl regex modifiers

  • g - match globally, i.e. replace ALL occurrences
  • m - treat string as multi-line. Then ^ $ work line-by-line instead of whole doc
  • i - case insensitive
  • s - make dot match a newline
  • x - ignore whitespace and allow comments after # on each line

reference

convert a text file to ASCI

iconv -f utf-8 -t ascii//translit

(use with pipes)

Drush

set default theme

drush vset theme_default seven

chmod

Intelligently set permission on files and directories

s find . -type d -exec chmod 755 {} \; && s find . -type f -exec chmod 644 {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment