Snippets
Collection of my code snippets
Tags:
i need a file off my server but i don't want to set up ftp
11-01-20
%$ scp -i ~/.ssh/privkey travis@199.199.19.199:/home/travis/example.txt ./
Yeah sometimes you just need to move files around. Any server you have ssh access to you can use that same key to send files over ssh.
source:
- [ scp]
find that lost folder
11-01-20
%$ find . -name php-e\*
Where the hell did that /php-extras folder go that I donloaded for my emacs?? On there you are! also ag -g 'php-e' would work if you want to stick with silver searcher
source:
better git add
11-01-20
%$ git add -p
I tend to use magit in emacs to stange and unstage files/hunks but in a pinch -p or git add -i + selecting patch works great. You can choose exactly which hunks you want to stage leading to cleaner incrimental commits. The bonus to using magit is you can easily edit the file during the process.
source:
- [ git]
move branch
11-01-20
%$ git rebase source-commit --onto target-branch
Sometimes you need to start a new feature branch but the place you need to base the branch is not ready yet, say coworker has merged his changes to a stage branch but not master. With this work flow you simply start your branch off stage then move to master later. git branch -m new-name also comes in handy to rename the branch after you've moved it if need be.
source:
- [ git]
soft merge
11-01-20
%$ git merge feature/tickets/NUTS-1231 --no-commit --no-ff
Sometimes you need to merge but only parcial files and you want fine control over everything and and possibly want to manually merge parts of files. This is when I use --no-commit --no-ff, ff for fast forward, it basically stages the entire merge making it easy to go trough and make changes. Using magit in spacemacs I go through hitting 'u' on files I don't want unstaging them, then maybe 'e' on a file to edit it via ediff, combining the new changes with orginal file seamlessy. Overall a fun and impowering workflow!
source:
- [ git]
mutliplex all the shells
11-01-20
%$ tmux new -s 'example_session_name'
Creates a new tmux session with the name 'example_session_name' which is very powerful and will exist even if you close out of the terminal.
Once you launch use <Ctrl + "> to split the window horizontally or <Ctrl + %> for vertically split and moving between windows with <Ctrl - Vim_Direction> You can detach and go back to shell at any time with <Ctrl + d> then reattach later with tmux new-s 'example_session_name'. If you forget the name a simple tmux ls show running sessions.
- [ tmux]
rewrite history git history
11-01-20
%$ git rebase -i HEAD~3
Using this command you can rewrite a series of commits via dropping, fixing, squshing, and picking. It's most helpful before pushing to a remote repo if you did a bunch of small commits you want to roll into one.
source:
- [ git]
oops i take that back
11-01-20
%$ git revert sfjes_examplehash_f32f32h
Some times you only need to undo a spific commit, often when you have already pushed to orgin and can't rebase past a certin point that's where git revert comes in. Simply supply it with a commit hash and it will basiclly undo that commits changes. This can be combined with git rebase if you need to git revert servral commits then rebase all the reverts into a single revert.
source:
- [ git]
silver searcher, it's like grep but faster and easier
11-01-20
%$ ag -G .php 'the meaning of the universe'
Life on the command line means grepping for things on a daily basis. After doing this for a while I memorized so may flag like grep -n for line numbers -i for case-insensitive and even the . to specify file directory. Then I discovered silver searcher, not only was the name a call back to one of my favorite comic book heroes but, it basically did work just as well as grep without all the flags and seemingly faster. It is now my go-to command for searching files whether it be inside vim via :r !ag -G .php 'something' or from the cli. It even has different outputs depending on the situation, in the cli it opens a temp window to browse results like less when piping or in vim it outputs a more machine-readable chunk of text with file paths, line numbers, and a single line of code.
source:
search pass from password
11-01-20
%$ pass list | ag aws
Being a CLI interface the UX of Pass fits amazingly into the rest of the shell ecosystem. Need can't remember if you have a password for AWS saved, run this.
source:
- [ pass]
bulk import into pass
11-01-20
%$ passimport list.csv
Switching to Pass was not exactly a straightforward process. It lacks a built-in mass import feature and I was dealing with a few hundred passwords and as a programmer entering them manually was unthinkable. After looking around at several plugins for Pass nothing seemed simple enough so I wrote my open python script to handle the task. I later turned that script into an executable, run by this command, and pushed it to GitHub.
source:
- [ pass]
copy password from pass to the keyboard
11-01-20
%$ pass -c github
Switching to Pass, a CLI based password manager, was a big time saver for me. I was using Padlock, a minimalist open source electron based manager, but was wasting so much time waiting for the GUI to load up and entering my master password scrolling to the desired entry and clicking. I'm not a time-saving purist but it was downright annoying UX pattern. Now I don't even have to leave the comfort of my keyboard.
source:
- [ pass]
see previous commit changes
11-01-20
%git log -p -2
or git lg -p -2
Viewing previous changes was something I relied on a GUI's for, like GitLab/Source-Tree, until I found this command! The -p stands for --patch and the -2 stands for last two commits.
source:
- [ git]
aws s3 sync
11-01-20
%$ aws s3 sync --acl public-read --sse AES256 build/ s3://travisshears.com
Having the ability to send a site live with a single command is heaven and not a BASH script with a bunch of moving parts liable to break. This command takes me back to the old days when putting a site live meant an FTP upload command to a server letting Apache take care of the rest. This site, for example, is hosted in an AWS S3 bucket that is connected to AWS CloudFront.
inspiration:
lustforge - great blog post
aws cloudfront invalidation sync
11-01-20
%$ aws cloudfront create-invalidation --distribution-id E29OAXKYAP0NP8 --paths /work/
Pairing well with the sync is invalidating the CDN so that the live site is updated immediately with the S3 bucket. Like everything these days, there is a cost involved with this operation so if I'm not in a rush I often avoid it. Also you can reduce cost by using --paths only invalidating needed routes.
inspiration:
lustforge - great blog post
- [ aws]
pretty print json
11-01-20
%$ curl -X GET https://some.json.endpoint.com | python -m json.tool
Need to check a API request but stuck reading a garbled mess. Try pipeing the result to python json.tool
source: