Change Ruby version for single command with rbenv

You can switch Ruby version temporarily with rbenv local

But it'd need to be switched back after you're done. Specially for a single command it's kind of pain. In those cases, you can use RBENV_VERSION env variable.

RBENV_VERSION=2.6.3 ruby -v

This might be useful in a shell script or so.


Go through a file Git history (GUI)

$ gitk --follow file


Reset Postgres table sequence after DELETE

When deleting recent records from a table, you can reset it's sequence counter if auto-increment field continuity is important to you. Consider actions on the following table, users
select max(id) from users;

+-----+ | max | +-----+ | 896 | +-----+ 1 rows in set (0.03 sec)

Then, some delete action against most recent rows: 
delete from users where created_at > timestamp 'yesterday';
96 rows in set (0.15 sec) 

The next auto-increment value for id would be 897 on the next insert. Use the following command to reduce the sequence value for the users table by 96.
select setval('users_id_seq',(select max(id) from users));

| setval |
| 800    |
1 rows in set (0.04 sec)

Kill unresponsive SSH session

Occasionally an SSH session may become unresponsive. If I put my laptop to sleep this happens fairly often. Rather than killing the tab you can use SSH Escape Characters to kill the session

$ ~.

To view all escape characters check man ssh and search for ESCAPE CHARACTERS $ /ESCAPE CHARACTERS


PostgreSQL UPDATE Join

This query shows how to use the PostgreSQL UPDATE join syntax to update data in a table based on values in another table. for example;

updating subscription_missions.giftbox_type with rewards.gift_box_type through mission_rewards table (tested on 300k records, crazy level fast)

        subscription_missions sm
        giftbox_type = r.gift_box_type
        mission_rewards mr,
        rewards r
        sm.giftbox_type IS NULL
        AND = mr.subscription_mission_id
        AND mr.reward_id =;


Kill rails that got stuck

I'd like to kill rails running w/specific port. (My laptop running several rails servers. )

kill -9 $(lsof -i :3000|grep ruby|head -1|awk '{print $2}')


Automagical Intermediate-level Hashtables in Ruby

All intermediate-level hashtables will be created automagically

def autovivifying_hash {|ht,k| ht[k] = autovivifying_hash}


response_builder = autovivifying_hash
response_builder['payment']['status'] = 'OK'
response_builder # => {"payment"=>{"status"=>"OK"}


Using Git's aliases feature

Git allows you to create aliases internally through the config. Your global config should be located here ~/.gitconfig

  aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /' | sort
  count = shortlog -sn
  l = log
  co = checkout
  ca = commit -C HEAD --amend
  c  = commit
  cm = commit -m
  a  = add
  s  = status
  b  = branch
  p  = push
  mt = mergetool
  dt = difftool
  o  = origin
  rc = rebase --continue
  rs = rebase --skip
  pd  = !git checkout develop && git pull origin develop && git branch --merged | grep -v '*' | grep -v 'master' | xargs git branch -d && git fetch --prune origin
  pm  = !git checkout master && git pull origin master && git branch --merged | grep -v '*' | grep -v 'develop' | xargs git branch -d && git fetch --prune origin
  pr = pull-request -c -m

Most are self explanatory...

git pd (prune develop) - checks out to develop, fetches latest changes, deletes all stale branches except develop (will delete master too)

git pm (prune master) - does the same as prune develop for master


See deleted files in previous commits [git]

$ git log --diff-filter=D --summary --oneline


Change VS Code Go to definition to Cmd+Click

Just open your user settings JSON file and use this option:

    "editor.multiCursorModifier": "cmd",