Code Snippets

These are a collection of code snippets for common use cases. Processes that are often done repeatedly can be automated, and things that I repeat often or search for consistently are shared here.

Git ignore local changes to tracked files

Language: Shell
git update-index --assume-unchanged <file-to-ignore>Copy to Clipbord
            

Check all checkboxes on page with Chrome DevTools

Language:
(function() {
    var aa = document.querySelectorAll("input[type=checkbox]");
    for (var i = 0; i < aa.length; i++){
        aa[i].checked = true;
    }
})()Copy to Clipbord
            

ABCJS chords minimal example

Language:
X: 1
T: Title
M: 4/4
L: 1/8
K: C
|"C"x8|Copy to Clipbord
            

Identify all images in directory

Language: Shell
#!/bin/bash

for f in *; do mogrify -identify $f; doneCopy to Clipbord
            

Download images from CSV with wget

Language: Shell
for link in `cat CSV_FILE | cut -d, -f1` 
do
     wget $link -O /home/tempfolder/
doneCopy to Clipbord
            

Add custom post type to WordPress RSS

Language: PHP
/**
* add custom post types to main rss feed
**/
function myfeed_request($qv) {
if (isset($qv['feed']) && !isset($qv['post_type']))
$qv['post_type'] = array('post', 'your-custom-post-type-slug');
return $qv;
}
add_filter('request', 'myfeed_request');Copy to Clipbord
            

Delete files after n days Linux

Language: Shell
find /path/to/files* -mtime +5 -exec rm {} \;Copy to Clipbord
            

Add audio to video using FFmpeg

Language: Shell
ffmpeg -i input.mp4 -i input.mp3 -c:v copy -map 0:v:0 -map 1:a:0 output.mp4
Copy to Clipbord
            

Get background images

Language: JavaScript
Array.from(document.getElementsByClassName('member-image')).map(i => i.style.backgroundImage.slice(0, -2).slice(5, i.style.backgroundImage.length))Copy to Clipbord
            

Delete and reinstall MySQL Ubuntu

Language: Shell
#!/bin/bash

sudo apt-get remove --purge mysql*
sudo apt-get purge mysql*
sudo apt-get autoremove
sudo apt-get autoclean
sudo apt-get remove dbconfig-mysql
sudo apt-get dist-upgrade
sudo apt-get install mysql-serverCopy to Clipbord
            

Deregister Gravity Stylesheets and Scripts from specific pages

Language: PHP
<?php

function deregister_scripts() {
    //Change this conditional to target whatever page or form you need.
    if (is_front_page()) {
        // Deregister Stylesheets
        wp_deregister_style("gforms_formsmain_css");
        wp_deregister_style("gforms_reset_css");
        wp_deregister_style("gforms_ready_class_css");
        wp_deregister_style("gforms_browsers_css");

        // Deregister Scripts
        wp_deregister_script("gforms_conditional_logic_lib");
        wp_deregister_script("gforms_ui_datepicker");
        wp_deregister_script("gforms_gravityforms");
        wp_deregister_script("gforms_character_counter");
        wp_deregister_script("gforms_json");
        wp_deregister_script("jquery");
    }
}

add_action("gform_enqueue_scripts", "deregister_scripts");Copy to Clipbord
            

Run Linux process in the background, detached from user

Language: Shell
#!/bin/bash

nohup your_command &Copy to Clipbord
            

Fix postmeta auto increment issue WordPress

Language: SQL
ALTER TABLE wp_postmeta MODIFY meta_id bigint(20) unsigned NOT NULL auto_increment;Copy to Clipbord
            

Count lines of code in a git repository

Language: Shell
git ls-files | xargs wc -lCopy to Clipbord
            

Count words in HTML file

Language: Shell
#!/bin/bash

sed "s/<[^>]*>//g" file.html | wc -wCopy to Clipbord
            

List installed Debian packages by size

Language: Shell
#!/bin/bash

dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -nCopy to Clipbord
            

cwebp batch process

Language: Shell
#!/bin/bash

for file in *.jpg ; do cwebp -q 100 "$file" -o "${file%.jpg}.webp"; doneCopy to Clipbord
            

Import MySQL on command line

Language: Shell
#!/bin/bash

mysql -u username -p dbname < dbexport.sqlCopy to Clipbord
            

Tagline link LilyPond

Language: LilyPond
composer = "Jared Forth"
copyright = "Copyright © Jared Forth"
tagline = \markup {
    \with-url
    #"https://jaredforthmusic.com"
    \line {
      "jaredforthmusic.com"
    }
}Copy to Clipbord
            

Connect to FTP with wget

Language: Shell
#!/bin/bash

wget -r --user="username" --password="password ftp://server.nameCopy to Clipbord
            

Unzip all zip files in Bash

Language: Shell
#!/bin/bash

unzip \*.zipCopy to Clipbord
            

Language: Text
### Keybase proof

I hereby claim:

  * I am jaredforth on github.
  * I am jaredforth (https://keybase.io/jaredforth) on keybase.
  * I have a public key ASCfVzDntJpUT-6Qu6xv4tHMcIHkeAhE7eFHg0LUV3wv6Ao

To claim this, I am signing this object:

```json
{
  "body": {
    "key": {
      "eldest_kid": "01209f5730e7b49a544fee90bbac6fe2d1cc7081e4780844ede1478342d4577c2fe80a",
      "host": "keybase.io",
      "kid": "01209f5730e7b49a544fee90bbac6fe2d1cc7081e4780844ede1478342d4577c2fe80a",
      "uid": "32ff8472eca6829c0e121f03242ba419",
      "username": "jaredforth"
    },
    "merkle_root": {
      "ctime": 1596211938,
      "hash": "d28e8dbab0dcb8a91155af7677a980625d84a2488f535818425a84db1dd063a78469b038f023a39d7acbaf398154a8ed039dd7a0b44f5c127dd4d5b1ca5c28d7",
      "hash_meta": "f6c23852b2930a51d003c222d23e850b51c5aa16847759378c6f35564ca44ce0",
      "seqno": 17143127
    },
    "service": {
      "entropy": "wJpdDvS/NeqBTpECvwlM7Dpu",
      "name": "github",
      "username": "jaredforth"
    },
    "type": "web_service_binding",
    "version": 2
  },
  "client": {
    "name": "keybase.io go client",
    "version": "5.5.0"
  },
  "ctime": 1596211977,
  "expire_in": 504576000,
  "prev": "78ff3f2f578cd5bb2530b06666916964fd846bbcadc08ea2c99a47bc38c14798",
  "seqno": 4,
  "tag": "signature"
}
```

with the key [ASCfVzDntJpUT-6Qu6xv4tHMcIHkeAhE7eFHg0LUV3wv6Ao](https://keybase.io/jaredforth), yielding the signature:

```
hKRib2R5hqhkZXRhY2hlZMOpaGFzaF90eXBlCqNrZXnEIwEgn1cw57SaVE/ukLusb+LRzHCB5HgIRO3hR4NC1Fd8L+gKp3BheWxvYWTESpcCBMQgeP8/L1eM1bslMLBmZpFpZP2Ea7ytwI6iyZpHvDjBR5jEIDRnSbRYPEpJxwnRyMz3g/h5jlw6sDclOzLAwCntTVLmAgHCo3NpZ8RAMBgBDMZ9M18KoBHEbPLKGfJBvJGbWfbZXUS+OfekqHNakprkT7REjXanob5trsXSvcbnysvwFLGgsrlzpxLLD6hzaWdfdHlwZSCkaGFzaIKkdHlwZQildmFsdWXEIFlt/kYKf/gUTrrNyG0p7xM5SdtnFC7tpIFvmp+Td+o8o3RhZ80CAqd2ZXJzaW9uAQ==

```

And finally, I am proving ownership of the github account by posting this as a gist.

### My publicly-auditable identity:

https://keybase.io/jaredforth

### From the command line:

Consider the [keybase command line program](https://keybase.io/download).

```bash
# look me up
keybase id jaredforth
```Copy to Clipbord
            

Find txt record for a domain

Language: Shell
#!/bin/bash

nslookup -q=txt _acme-challenge.example.comCopy to Clipbord
            

Find GB of a directory in linux

Language: Shell
#!/bin/bash   

du -h yourdirectory/Copy to Clipbord
            

Dry run certbot with DNS challenge

Language: Shell
#!/bin/bash

certbot-auto certonly --dry-run --manual --preferred-challenges=dns -d example.com -d www.example.com Copy to Clipbord
            

Verify syntax of Apache config

Language: Shell
#!/bin/bash

apachectl configtestCopy to Clipbord
            

Check validity of OpenSSL cert

Language: Shell
#!/bin/bash

# check csr
openssl req -text -noout -verify -in server.csr

# check cert
openssl x509 -text -noout -in fullchain.pemCopy to Clipbord
            

OpenSSL generate new key and csr

Language: Shell
#!/bin/bash

openssl req \
       -newkey rsa:2048 -nodes -keyout domain.key \
       -out domain.csrCopy to Clipbord
            

Create circle with members of the set {0,1...11}

Language: TeX
\documentclass[letterpaper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}
\begin{document}

Use \verb`\xdef`

\begin{tikzpicture}[line cap=rect,line width=1pt]
  \filldraw [fill=white] (0,0) circle [radius=2cm];
  \edef\i{0}
  \foreach \angle [count=\xi] in {90,60,...,-240}
  {
    \draw[line width=.5pt] (\angle:1.9cm) -- (\angle:2cm);
    \node at (\angle:1.5cm) {\textsf{\i}};
    \pgfmathparse{int(\i+1)}
    \xdef\i{\pgfmathresult}
  }
  \foreach \angle in {0,90,180,270}
    \draw[line width=.5pt] (\angle:1.9cm) -- (\angle:2cm);
  %\draw (0,0) -- (120:0.8cm);
  %\draw (0,0) -- (90:1cm);
\end{tikzpicture}

\end{document}Copy to Clipbord
            

Create number line in LaTex with tikz package

Language: TeX
\documentclass[letterpaper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{arrows}

\begin{document}
\begin{tikzpicture}
  \draw[latex-latex] (-3.5,0) -- (3.5,0) ; %edit here for the axis
  \foreach \x in  {-3,-2,-1,0,1,2,3} % edit here for the vertical lines
    \draw[shift={(\x,0)},color=black] (0pt,3pt) -- (0pt,-3pt);
    \foreach \x in {-3,-2,-1,0,1,2,3} % edit here for the numbers
      \draw[shift={(\x,0)},color=black] (0pt,0pt) -- (0pt,-3pt) node[below] 
    {$\x$};
    \draw[*-o] (0.92,0) -- (2.08,0);
  \draw[very thick] (0.92,0) -- (1.92,0);
\end{tikzpicture}

\end{document}Copy to Clipbord