#Dev Notes

Vim mini cheatsheet

// dev notes

Vim mini cheatsheet

  • Insert modes:
    • A to insert at end of line
    • a to insert after next char
    • i to insert at current cursor
  • Visual modes:
    • v character, V: line
      • to select text
      • d to cut, y to copy (yank)
      • Move cursor to position and P to paste before or p to paste after
  • Normal mode:
    • y{motion} for copying and cutting in normal mode
    • Y: yanks current line, including newline char at the end
    • yiw: yanks current word without surrounding whitespace
    • yaw: yanks current word with surrounding whitespace
    • Cutting can be done with d{motion}
  • Deleting lines
    • dd to delete current line
    • :10d to delete Line 10
    • :10,20d to delete from Line 10 to Line 20
  • 10gg or :10 + Enter to go to Line 10
  • x delete character after cursor, X for before
  • u to undo
  • . repeat last change

PostgreSQL

// dev notes

DB scripts the hard (dumb?) way

  • Since I’m completely new to this, I’m learning by writing DB scripts from scratch. The three basic scripts I have when bootstrapping apps: reset.sh, schema.sh and seed.sh
# reset.sh
printf '===Reset DB===\n'

psql -U DB_USERNAME -d DB_NAME << EOF
  DROP TABLE IF EXISTS table_1 table_2;
EOF

printf '===DB Reset===\n'
# schema.sh
psql -U DB_USERNAME -d DB_NAME << EOF
  CREATE TABLE users (
    user_id serial PRIMARY KEY,
    user_first_name text DEFAULT '',
    user_last_name text DEFAULT '',
    user_username text UNIQUE DEFAULT '',
    user_photo_url text DEFAULT '',
    user_auth_date text DEFAULT '',
    session_id text DEFAULT ''
  );
EOF

printf '===Loading Schema===\n'
# seed.sh
printf '===Seeding Data: USERS===\n'

psql -U DB_USERNAME -d DB_NAME << EOF
  INSERT INTO 
    users (user_first_name, user_last_name, user_username)
  VALUES 
    ('Edison', 'Chee', 'edisonchee');
EOF

printf '===Done Seeding TG_USERS===\n\n'

References

Learn CS

// dev notes

On bugs

  • Humans have a tendency to blame their incompetence on something else. ‘The software I wrote has a bug; the computer is dumb!’.
  • If your software crashes unexpectedly, it was really because you didn’t handle it.

CS106B

  • Truth and beauty. Working software is not enough; well-designed and well-engineered code matters.
  • Everything in Java lives inside a class, whereas there are functions in C/C++ that don’t and hence allow you to do stuff in a more procedural way.
  • Function prototypes are a way of telling the compiler about something that’s coming up later.
  • <<: string insertion operator.
  • Integer division has truncation built into it.
  • Only two types of user-defined types in cpp: Enums and Records/Structs.
  • Default parameter passing is pass-by-value. Add & to declaration for pass-by-reference.

Getting warm with Machine Learning

// dev notes

My current role as a Design Technologist is centred around building prototypes to describe ideas, stimulate conversations, and even provoke wilder imaginations. Designing for the future can sometimes mean building things that are not solving an immediate problem or pain point.

Anyhow, I’ve been diving into ML for quite some time, and am taking notes along the way as I grow from “copy, paste & pray” to understanding what’s really going on. Instead of taking an online course, I learn better while trying to build something first and catching up on the theory later. So, this page is merely a place for me to braindump stuff I’ve learned.

Hugo, Gitlab Pages & Cloudflare

// dev notes

Some tips & gotchas I collected while migrating from Jekyll to Hugo + Gitlab Pages + Cloudflare:

  • Gitlab Pages doesn’t allow users to remove the default gitlab.io URL even if you’ve added your custom domains. Make sure you have a canonical reference in every page.
  • hugo server --baseURL http://192.168.1.2 --bind 192.168.1.2 if you want to make your local testing available on local wifi network.
  • It’s a good practice to end baseURL in your config.toml with a trailing slash.
  • When setting up Cloudflare, Gitlab docs talk about pointing www to your gitlab.io url as a CNAME. I personally prefer pointing it to my main domain, and setting up a 301 in Page Rules.
  • Speaking about Cloudflare, HTML files are not automatically cached. Again, this has to be specifically defined in Page Rules.
  • Use trailing slashes in all your internal links so Cloudflare knows it’s a directory. www.example.com/blog will trigger a 302 to www.example.com/blog/ and an origin pull, causing a long TTFB everytime! I’ve taken it for granted with many years of URL rewrites when running my own server. A List Apart has a good article talking about it.
  • For quite a while I was wondering why my favicons never got cached. I’m assuming adding type="image/x-icon" will fix this.
  • Adding a hash to CSS files is a pretty bad idea for a static website. Maybe it works well for SPAs if you need to do cache-busting.
  • It got really tiring to keep typing the whole string, and it gets worse when your IP changes:
# use an alias in your .zshrc file
alias hs='hugo serve --baseURL http://${ipconfig getifaddr en0}:1313 --bind ${ipconfig getifaddr en0}'

# or if you get a substitution error
alias hs='hugo serve --baseURL http://$(eval ipconfig getifaddr en0):1313 --bind $(eval ipconfig getifaddr en0)'

# put it in a function so you can get it anytime in the terminal
get_ip() {
  eval ipconfig getifaddr en0
}

Setting up a new environment

// dev notes

Customising terminal

# .zshrc.local
if [ -f ~/.zshrc.functions ]; then
    source ~/.zshrc.functions
else
    print "404: ~/.zshrc.functions not found."
fi
  • neat tricks
    • open image.jpg opens in default application (Preview on macOS)
    • + shift + P to install ‘code’ command in PATH. Now you can open any file in code via terminal: code text.md

Useful libs

  • imagemagick
  • youtube-dl
  • ffmpeg

Postgres

  • gist
  • Find location of pg_hba.conf by going into psql and typing SHOW hba_file
  • Likely need to change auth method from peer to md5

Redis with UNIX sockets