Snippets are one of my favorite parts of programming. The shear endless depth of command line tricks and customizations is so exciting! Here is a collection I maintain for myself to comeback to.

Also navigable by list of snippet types here.


Prevent Vim Auto New Lines -- 13.08.2020 %

Sometimes when typing vim will automatically start a newline. This is an expected behavior but at times can be really annoying. Ex working with macros, you can recored one on a short line that breaks on longer lines 😟. The amount of text before vim will break to new line while typing is controlled via the textwidth setting. So a fix is pretty simple. If don’t want the behavior just set textwidth to a big number. ex:

: set tw=500

Here is a asciicast of the problem and solution in action:

asciicast

demo of setting tw

source – https://stackoverflow.com/questions/1272173/in-vim-how-do-i-break-one-really-long-line-into-multiple-lines

\- [ vim ]

Auto Find SSH Keys -- 12.08.2020 %

I use to always pass a key when sshing ex:

$ ssh -i ~/.ssh/de2 travis@vxxxxxxxxxxxxxxxxxxx.megasrv.de

That can be a bit annoying. I know two fixes:

add private key to ssh-agent

$ ssh-add ~/.ssh/de2

Now when you ssh you need not include the -i ~/.ssh/de2 because the ssh-agent will find it automatically.

configure individual host

You can configure individual hosts to use a spefic private key my editing your ~/.ssh/config:

Host de2
    HostName vxxxxxxxxxxxxxxxxxxxx.megasrv.de
    User travis
    IdentityFile ~/.ssh/de2

Now you only need to

$ ssh de2

source – https://www.techrepublic.com/article/how-to-use-per-host-ssh-configuration/

\- [ ssh ]

Using short server names -- 12.08.2020 %

It use to be when I was trying to ssh into one of my servers I would just: in the shell to get my fzz backwards command search, type ssh, then scroll up and down until I found the correct server. As my number of servers has grown this is no longer manageable because I don’t remember which IPs align with with server. Time for some human readable names.

By adding something like this to your ~/.ssh/config:

Host de1
    HostName vxxxxxxxxxxxxxxxxxxxx.megasrv.de
    User travis

Host de2
    HostName vyyyyyyyyyyyyyyyyyyyymegasrv.de
    User travis

Host de3
    HostName vzzzzzzzzzzzzzzzzzzz.megasrv.de
    User travis

Host nyc1
    HostName 123.123.123.1233
    User travis

sshing becomes as easy as:

$ ssh de2

source – https://www.techrepublic.com/article/how-to-use-per-host-ssh-configuration/

\- [ ssh ]

Disable User -- 11.08.2020 %

In this case disabling the user named ubuntu from logging in. This includes logging in via ssh.

$ sudo usermod --expiredate 1 ubuntu
\- [ sysadmin ]

Automatic tmux session names -- 13.07.2020 %

This month I ditched XQuartz and am back to using Tmux. One part I found tedious was manually naming sessions. I wrote this little alias help. When run from outside a tux session to creates a new session named after the current directory. Ex when in /users/t.shears/dev/cool-app a session named cool-app is created.

Excerpt from my .zshrc

alias tmux_new="tmux new -s \$(pwd | awk -F "/" '{print \$NF}' | sed 's/\./_/g')"
\- [ tmux ]

File Search Plus Size -- 02.07.2020 %

How big are the fonts in this project? Not sure where they are even. This command finds the files using fd then prints file size.

$ fd woff | xargs du -h
 20K    src/assets/fonts/brandon_text_medium.woff2
 20K    src/assets/fonts/brandon_text_regular.woff2
 20K    src/assets/fonts/chronicle_light.woff2
 20K    src/assets/fonts/chronicle_roman.woff2
4.0K    types/woff
4.0K    types/woff2
\- [ fd, du ]

Convert .mkv to .mp4 -- 30.06.2020 %

Before I updated my OBS settings to record to .mp4 files I manually converted .mkv files to .mp4. This shell command does that for every recording in a directory deleting the original.

$ for x in $(ls *.mkv | awk -F "." '{print $1}') ; do ffmpeg -i $x.mkv -c copy $x.mp4 && rm $x.mkv ; sleep 3; done
\- [ ffmpeg ]

Wipe a Mongo Collection -- 29.06.2020 %

Currently I run a shared mongo instance in my k8s cluster used by a bunch of my side projects. Every once in a while I have to manually execute mongo commands this is how I do so.

Manually ssh into the pod:

$ kubectl exec -it shared-mongo-xxxxxxxxxx-xxxxx -- /bin/bash

Then launch the mongo shell and hack away:

$ mongo
> db2 = db.getSiblingDB('coolDB')
coolDB
> db2.getCollectionNames()
[ "coolThings" ]
> db2.coolThings.count()
666
> db2.coolThings.remove({})
WriteResult({ "nRemoved" : 666 })
> db2.coolThings.count()
0

source:

\- [ mongo ]

Extending GPG Keys -- 22.06.2020 %

Don’t let those keys expire. 🚨

Time to edit some keys:

gpg --edit-key t@travisshears.com
Secret key is available.
sec  rsa2048/D4C2E4DFAB8BABF8
     created: 2018-07-18  expires: 2020-07-17  usage: SC
ssb  rsa2048/25C629D0FECC25B9
     created: 2018-07-18  expires: 2020-07-17  usage: E
ssb  rsa4096/97F7C2B46E6C5D11
     created: 2019-09-28  expires: 2023-09-28  usage: E
  1. select key to change key 1
  2. expire
  3. duration: 1y
  4. repeat until every key is updated

resulting output should be something like:

sec  rsa2048/D4C2E4DFAB8BABF8
     created: 2018-07-18  expires: 2021-06-22  usage: SC
ssb  rsa2048/25C629D0FECC25B9
     created: 2018-07-18  expires: 2021-06-22  usage: E
ssb  rsa4096/97F7C2B46E6C5D11
     created: 2019-09-28  expires: 2021-06-22  usage: E

lastly save and done

source:

\- [ gpg ]

Moving GPG Keys -- 20.06.2020 %

New laptop. Got to move over those GPG keys.

$ cd /tmp && mkdir gpg_export
$ gpg --output gpg_export/main_pub.gpg --armor --export t@travisshears.com
$ gpg --output gpg_export/main_sec.gpg --armor --export-secret-key t@travisshears.com
$ tar cvfz gpg_export.tar.gz ./gpg_export
$ gpg --symmetric ./gpg_export.tar.gz

Then I trasferend the encrypted tar via airdrop.

To import the keys.

$ gpg --decrypt gpg_export.tar.gz.gpg > gpg_export.tar.gz
$ tar xvfz gpg_export.tar
$ gpg --import gpg_export/main_sec.gpg
$ gpg --import gpg_export/main_pub.gpg

Source:

\- [ gpg ]