How to save a file with sudo in VIM

:w !sudo tee %


1
Ali

Zsh function to list file tree

Add this to your .zshrc

lst() {
    if [ "$1" != "" ] # or better, if [ -n "$1" ]
    then
        ls -R $1 | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    else
        ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
    fi
}


Then you can run

$ lst spec

It should list all sub directory in spec folder in tree format.

You could change spec to any path you want.

or just use tree

<3

Tino

Change system names on macOS

From term (or equivalent), copy and paste:

To change your public Host Name (i.e., jeff.oozou.com): sh sudo scutil --set HostName

To change your local Host Name (i.e., jeff.local): sh sudo scutil --set LocalHostName

To change your Finder Computer Name (i.e., Jeff's MBP): sh sudo scutil --set ComputerName

Finally, flush your DNS cache to force refresh: sh dscacheutil -flushcache

1
Jeff

Create your own git commands (aliases)

Ever wanted to shorten a git command? You can go with shell alilas for sure but you have to always include context so that you don't forget what each ailas stands for but you can define your own commands in git using aliases.

git config --global alias.  


For example; I'm too lazy to type git status so I want to shorten this to git s

git config --global alias.s status


And here we go now you can just type git s and get the status of current repository.

Ali

Amazon AWS S3 CLI copy with set perms

Open term (or equivalent) and paste the following:

S3 copy

  • to upload a world-readable file:
aws s3 cp source.file s3://bucket/path/dest.file --acl public-read


Options for --acl parameters are:

  • private - visibility to owner only
  • public-read - readable by all
  • public-read-write - readable and writable by all
Jeff

Add new types to Rails 5 attributes API

The Rails 5 attributes API allows us to build form objects very easily.

I had previously been using an after_initialize callback to downcase emails, with the following code I can define an :email date type and values are automatically cast when set.

This has the advantage of casting subsequent calls to set the email value.

class EmailType < ActiveModel::Type::String
  def cast(value)
    return super unless value.is_a?(String)
    super(value.downcase)
  end
end

ActiveModel::Type.register(:email, EmailType)


class Foo
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :email, :email
end


> Foo.new(email: 'EMAIL@MAIL.com').email
=> 'email@mail.com'


Joe

Use FFmpeg and Apple Script to compress videos

Build an application you can drag-and-drop videos onto to compress them significantely:

Make sure you have ffmpeg installed (brew install ffmpeg).

Create a new Apple Script app: AppleScript on open filelist repeat with i in filelist tell application "Terminal" do script "ffmpeg -i " & POSIX path of i & " -vcodec h264 -acodec mp2 " & POSIX path of i & "_compressed.mp4" end tell end repeat end open

Const

Scope or scope in Rails

# app/models/user.rb
...
enum role: { user: 0, author: 1, admin: 2, robot: 3 }

scope :human, -> { user.or(author) }
scope :terminator, -> { admin.or(robot) }


Produces

User.human.to_sql
#=> "SELECT \"users\".* FROM \"users\" WHERE (\"users\".\"role\" = 0 OR \"users\".\"role\" = 1)"


Joe

HTML forms that submit to another action

I've known for a long time that you can submit values with submit buttons so you can track which button was used to submit the form e.g.

language-html

However, today I needed to actually submit the form to a completely differnt action. Turns out you can do this with formaction attributes

language-html

1
Joe

Get the password of the wifi you're on

wifi_password() {
    airport="/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport"
    ssid="`$airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`"
    pwd="`security find-generic-password -D 'AirPort network password' -ga \"$ssid\" 2>&1 >/dev/null`"
    echo $pwd
}


$ wifi_password

password: "!forHackers"


Ali