Skip to content

Instantly share code, notes, and snippets.

@williscool
Last active October 30, 2023 11:31
Show Gist options
  • Save williscool/3a05a771d2fc00523d79 to your computer and use it in GitHub Desktop.
Save williscool/3a05a771d2fc00523d79 to your computer and use it in GitHub Desktop.
Commands that I've found to be useful. Mostly general command line, bash, and rails stuff

Useful commands:

TODO: rearrange this to bottom = oldest and top = newst (2-27-2020) below dashed line

fix tmux resurrect

ln -f -s $(/bin/ls -t tmux_resurrect_*.txt | head -n 1) last

tmux useful comands

https://tmuxcheatsheet.com/


for when your git author info is wrong

https://help.github.com/articles/changing-author-info/

actually useful version of find

find . -type f -name '*yml*' -not -path "*node_modules*" -not -path "*bower*" | grep aws

using sed

http://stackoverflow.com/questions/1583219/awk-sed-how-to-do-a-recursive-find-replace-of-a-string http://superuser.com/questions/428493/how-can-i-do-a-recursive-find-and-replace-from-the-command-line http://askubuntu.com/questions/266179/how-to-exclude-ignore-hidden-files-and-directories-in-a-wildcard-embedded-find

find . -not -path '*/\.*' -type f -print0 | xargs -0 sed -i 's/thing/another_thing/g'

even better

ag thingy-to-replace -l0 | xargs -0 sed -i '' 's/thingy-to-replace/replacement/g'

also very useful

git diff --name-only HEAD~1 | grep <filename-filter> | xargs sed -i -e 's/thingy-to-replace/replacement/g'

kill a hung ssh session

<Return>~.

http://superuser.com/questions/467398/how-do-i-exit-an-ssh-connection/620787#620787

pass vim a command at startup

vim -c ":NERDTree"

use mouse in vim

google vim mouse support. also http://superuser.com/questions/549930/cant-resize-vim-splits-inside-tmux http://stackoverflow.com/questions/3249275/vim-multiple-commands-on-same-line

set mouse=a | set ttymouse=xterm2

Actually useful silver searcher for node.js projects

ag --ignore '*node_mod*' --ignore '*bower*' "thing you actually want"

unfreeze terminal when you froze it by pressing Ctrl + s

Ctrl + q

make terminal stop freezing like a dumb ass

stty -ixon

http://unix.stackexchange.com/questions/12107/how-to-unfreeze-after-accidentally-pressing-ctrl-s-in-a-terminal

Make a small tmux window big

Ctrl + Z

press it again to make it small again

For when aptget stupidly forces ipv6

sudo aptitude -o Acquire::ForceIPv4=true update

the -o Acquire::ForceIPv4=true works with all of the other commands too

manage multple ssh keys. and not have to put your password in all the time

eval `ssh-agent -s`
ssh-add -D
ssh-add ~/.ssh/your_account_key

http://stackoverflow.com/questions/13752908/managing-multiple-ssh-keys-on-heroku http://stackoverflow.com/questions/17846529/could-not-open-a-connection-to-your-authentication-agent

flush chrome dns caches

chrome://net-internals/#dns

delete all space until the next character

Ctrl + Delete

pgrep is your friend when killall is being dumb

kill $(pgrep unity-panel-ser)

upgrade postgres with pg_upgrade

sudo -H -u postgres /usr/lib/postgresql/9.3/bin/pg_upgrade \
  -b /usr/lib/postgresql/9.2/bin \
   -B /usr/lib/postgresql/9.3/bin \
   -d /etc/postgresql/9.2/main/ \
   -D /etc/postgresql/9.3/main/ \
   -p 5432 \
   -P 5433
   
#### then dont forget to go into the new install's postgres.conf and change the port from 5433 to 5432
### also setup trust auth for local dev connections

sshfs

### this part unmounts the dir for you if it was unproperly done last time. like say by a disconnect
sudo umount -l /path/you/want/stuff/at

sshfs [email protected]:/remote/path/stuff/is/from /path/you/want/stuff/at

print out the postgres commands to change all the table ownerships

SELECT 'ALTER TABLE ' || table_name || ' OWNER TO <new user name>;' 
  FROM information_schema.tables
 WHERE table_schema='public'
   AND table_type='BASE TABLE';

rsync ftw

http://www.thegeekstuff.com/2010/09/rsync-command-examples/

rsync -avz /root/temp/ [email protected]:/home/thegeekstuff/temp/

open vim at certain line num

vi +124 app/assets/javascripts/application.js.erb

hash except idiosyncrasies

hash.except(*default_hash.keys) != hash.except(default_hash.keys)

Arel predicates (sql operator functions)

http://rubydoc.info/github/rails/arel/master/Arel/Predications

ctags

regenerate rails ctags

ctags -R app/ test/ vendor/ lib/ or ctags -R --exclude=vendor --exclude=public --exclude=db .

ctags and ruby

http://blog.bojica.com/2010/06/27/ctags-and-vim-for-ruby-on-rails-development

year ago postgres

select now() - interval '1 year';

copy file name to vim paste buffer

http://stackoverflow.com/questions/916875/yank-file-name-path-of-current-buffer-in-vim let @" = expand("%:p")

also copy relative name to system clipboard

let @+ = expand("%")

for that awkward moment after you hard reset because you commited a bunch of stuff you didnt want to

git clean -d app/ db/ test/ config/ share/ vendor/ lib/ public/ script/

log out all patches to a file

git log -p

scp

scp ~/path/to/file [email protected]:~/path/on/remote/server

##find sucks use locate, also you can pipe it to grep with the current dir for only that its a seperate db that trails behind the real filesystem but its a shitton faster than find and easier to use

locate file_name_.ext | grep `pwd` | less
locate builder | grep $(pwd) 

update ur submodules

git submodule init && git submodule foreach git pull origin master

port fowarding for torquebox ports

sudo ipfw add fwd 127.0.0.1,8080 tcp from any to me dst-port 80 in sudo ipfw add fwd 127.0.0.1,8443 tcp from any to me dst-port 443 in

#tmuxinator session rvm use ruby-1.9.3-p0 && mux project_name

#clear the screen with out typing "clear" ^L (ctrl + l)

#find a gem location gem which gem_name

#list ur postgres databases \list

#postgre server log if you install with brew on osx less /usr/local/var/postgres/server.log

#add params / parameters to a rails url url_for(params.merge({:param_name => model.name})

#get a list of vim key mapings :verbose map

#open file in nerdtree with only the context of its parent directory NERDTree %

#find a program path directory location whatnot which ruby

#screw up javascript Array.prototype.push = '(╯°□°)╯︵ ┻━┻';

#git stash only unstaged git stash -k

#git checkout file from branch git co <branch_name> -- <path_to_file>

#catch window.alert errors var oldAlert = window.alert; // reference to the original window.alert

window.alert = function(message) { if (window.console && console.log) { debugger; console.log(message); } else { oldAlert(message); // if console.log doesn't exist call window alert } }

#have git log trace the history of some string in a file git log -S "hi ima string maybe a function_name" app/controllers/some_controller.rb

#history of one file gitx -- path/to/file

#git merge one specific branch with a certain strategy git pull --strategy=ours origin feature/marketer_alerts_ticket_31305

not very useful but fun to run on a git code base

git log | grep Author | sort | uniq | less

searching the mac mail old school

Peached Tortilla OR Empanda OR food OR tacos OR truck

WUT!!! a date time from a string with a date in it!

"May 17 2011".to_datetime

ruby idiosyncrasies

focus == :foo || focus == :bar AND focus == :foo ||:bar

ARE DIFFERENT THINGS !!!!!!!!!!!!!!!

making good rails error message stack traces

logger.error "Error: could not do a thing I wanted to do" logger.error $!.message logger.error $!.backtrace.join("\n")

check an object's errors

object.errors

reset you head to the branches upstream

git reset origin/name_of_branch

see what changes are on your branch that aren't on the upstream

git log origin/branch..HEAD git log origin/branch..

#pry for the win https://github.com/pry/pry/wiki/Setting-up-Rails-or-Heroku-to-use-Pry binding.pry #this goes in your codes pry -r config/environment

#tell git to ignore changes in tracked files http://pivotallabs.com/users/rolson/blog/articles/1278-ignoring-tracked-files-in-git git update-index --assume-unchanged config/database.yml git update-index --assume-unchanged Gemfile Gemfile.lock

how to do a migration

./script/generate migration add_to_or_change_database_somehow

class TheMigrationNameAndWhatItDoes < ActiveRecord::Migration def self.up add_column :table_name, :column_name, :type end

def self.down remove_column :table_name, :column_name end end

#git checkout a new branch based off of the current branch git co -b branch_name

#DELETE an address / from the CHROME search BAR suggestion shift + delete

remove file from git stage

git reset HEAD

how to use a difftool that doesn't suck on a mac

http://gitguru.com/2009/02/22/integrating-git-with-a-visual-merge-tool/

#tab between windows of one application superkey + ~

OR bring process back to foreground with: fg

kill rails console when it gets into that weird state

crtl - Z ps aux | grep ruby killall ruby

!WARNING

always use ls -all to/dir

  1. always use a full path for both source and destination
  2. in the destination directory to make sure the link was created correctly

ln -s from/dir to/dir

!Important note. if you want it to be directory make sure TO use the slash in from of the source directory

also to double check its correctness

tell rvm to use a certain gemset

rvm use 1.9.2-p290@gemset_name

reverse a merge

http://lostechies.com/derickbailey/2010/04/01/git-how-to-revert-a-branch-merge/ git reset --hard HEAD^

thin with bundle exec

bundle exec thin -e development start

quit both sides of git difftool

:qa

reenable right click

document.oncontextmenu = null; if (typeof document.body.style.MozUserSelect!="none"){ document.body.style.MozUserSelect=null; } document.body.onmousedown = null;document.body.onselectstart = null;

inject le jquery

var element1 = document.createElement("script");element1.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js";element1.type="text/javascript";document.getElementsByTagName("head")[0].appendChild(element1);

start console with test environment

rails c test

#Git track a remote branch for an existing branch git branch --set-upstream branch_name origin/branch_name

#Git track a remote branch git branch --track feature/branch_name origin/feature/branch_name

Screen Shot on mac

http://guides.macrumors.com/Taking_Screenshots_in_Mac_OS_X

  • Command-Shift-4, then select an area: Take a screenshot of an area and save it as a file on the desktop
  • Command-Control-Shift-4, then select an area: Take a screenshot of an area and save it to the clipboard

#why you have missing attribute errors http://www.cowboycoded.com/tag/missingattributeerror/

  • basically because you used :select

#update test database

migrate dev db && then copy schema to test db

bundle exec rake db:migrate && bundle exec rake db:test:prepare

#run tests ruby -I"test" test/functional/controller_name_test.rb --name /account/

#restart windows in safe mode start -> "msconfig" -> boot options

#kill part of the rails cache Rails.cache.clear Rails.cache.delete "key/value/name"

running tests

ruby -I test {path/to/test}

how to to resize a linux vmware disk partition

http://creechy.wordpress.com/2011/03/16/resizing-an-lvm-in-a-centos-vmware-guest/

mac share dir on vm

/mnt/hgfs

How to fucking use a centos vmware image

http://lists.centos.org/pipermail/centos-virt/2008-February/000203.html

how to reset a local branch to a remote forcibly

git fetch --all
git reset --hard origin/<remote-branch-name>

Manual migrate

require ".db/migrate/#{timestamp}_migration_name.rb"
WhateverClassName.down
WhateverClassName.up

#pull updates for git submodules git submodule foreach git pull

#Make git not do the thing noted here http://kartzontech.blogspot.com/2011/04/how-to-disable-gnome-ssh-askpass.html unset SSH_ASKPASS

#Make Git shutup completely about ssl certs export GIT_SSL_NO_VERIFY=true

#Make git ignore ssl certs only once env GIT_SSL_NO_VERIFY=true git pull ...

  • tell grep to skip/ignore directory for one command env GREP_OPTIONS="--exclude=*log*" grep -Iirn "pgi=" * | less

  • tell grep to ignore svn directories export GREP_OPTIONS="--exclude=*.svn*"

#switch user super sudo su - postgres

vim line numbers :set number

  • how to search for a file named something in the codebase find . -name 'event.' -type f | less

  • how to search for an instance of something in text of a file in the codebase (-r means recursive, -i is case insensitive, and -l means say the filename instead of printing out the files contents to the standard out)

  • n = include line number

  • I = ignore binary files

grep -Iirn "pgi=" * | less

also^ http://lifehacker.com/219058/geek-to-live--visualize-your-hard-drive-usage du -k | sort -nr

current directory size du -sh (add * for sub dirs)

filesystem size df -h

  • Reload ruby console in ruby console reload!

  • Stop a process then restart when you get ready to use it again in same state

Ctrl + z fg

Exclude file from git diff or log

git diff . ":^yarn.lock"

fix chrome window switch on mac

install hyperswitch

use Option + Tab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment