Back up a file with same name + prefix

$ cp todo.txt !#:1.bak
cp todo.txt todo.txt.bak


NOTE: Actually you can use !#:1 in every command 'cause that's last argument passed to a command.

UPDATED: Now it works with zsh

Ali

optional relation + RSpec w/shoulda-matchers

User 1 - 0..1 Laptop

  • Model ```ruby a/m/user.rb has_one :laptop

a/m/laptop.rb belongs_to :user, optional: true ```

  • spec ruby s/m/laptop_spec.rb it { should belong_to(:user).optional }
827
Alyson

How to find god objects in your project (Rails)

Go to your project root and cd into app/models (using CLI obviously) then run wc -lw * | sort -u

$ wc -lw * | sort -u
103     186 deal_broker.rb
116     313 physical_sim_report.rb
126     229 coupon_code.rb
126     260 subscription_mission.rb
129     284 payment.rb
145     290 deal_value.rb
147     255 single_use_coupon.rb
166     344 sim_card.rb
166     406 coupon_campaign.rb
225     451 order_summary.rb
260     534 deal.rb
308     783 package.rb
443    1031 user.rb
728    1744 subscription.rb
7262   15841 total


So now you can see Rocky is all about User ,Subscription and Package.

Note: First column number of lines and the second column number of words.

827
Ali

Remove ruby 2.6 bundled bundler

Generating a rails 6 app with ruby 2.6+ when you have bundler v2 installed creates unusable binstubs. If you are seeing this error You must use Bundler 2 or greater with this lockfile. you can remove the bundler that comes bundled with ruby 2.6 and rely on the manually installed v2 bundler.

If you are using rbenv you can use this command (adjust for your ruby version e.g. 2.6.{0, 1} etc), other installation systems will use different locations

rm -rf ~/.rbenv/versions/2.6.1/lib/ruby/2.6.0/bundler*


828
Joe

Chrome tab manipulation

Sometimes I want to go back to the last page I viewed while still keeping the same one open. You can very easily achieve this with shortcuts

  1. ⌘-L to focus the omnibox
  2. ⌘-Enter to open the current URL in a new tab
  3. ⌘-⇧-[ to go back to the original tab
  4. ⌘-[ to go back to the previously viewed page in the original tab
830
Joe

Quickly move lines up and down in any Vim mode

I often move lines around in vim so I have created mappings to make this easier. These map to ⌘-j for move down and ⌘-k for move up. Works in normal, insert, and visual modes. vim nnoremap :m .+1== nnoremap :m .-2== inoremap :m .+1==gi inoremap :m .-2==gi vnoremap :m '>+1gv=gv vnoremap :m '<-2gv=gv I use MacVim so I have used D(⌘) for the modifier key. To use in terminal based vims, use a different modifier e.g.

829
Joe

Delete already-merged branchs in Git

$ git branch --merged | egrep -v 'master|develop' | xargs -n 1 git branch -d


You can safelist more branches in egrep -v 'master|develop|foo|bar'

828
Ali

Diff a local file with remote version in Git

Easily run the below command and get a diff version from the remote

$ git diff origin/master -- [local-path]


831
Ali

Remove unwanted commit from your branch

git rebase -p --onto SHA^ SHA


827
Tino

Suppress rspec warnings

$ RUBYOPT=W0 ./bin/rspec spec/
...

$ RUBYOPT=W0 bundle exec rspec spec/
...

$ RUBYOPT=W0 rspec
...

$ RUBYOPT=W0 ...
...


827
Ali