Snippets

Snippets are super brief notes for my own use. I'd hoped that Twitter would be a convenient place to store tiny notes, but its not searchable enough. This page should allow quick browsing and searching for things I know I knew or saw in the past.

May

Create a custom command and function to create a new file in vim.

question on SO

:h expand()

:h %:h
command! -nargs=1 Ms call s:NewFile(<q-args>)`

function! s:NewFile(fp)
  echom a:fp
  execute "e " . "~/foo/bar/" . a:fp . ".ext"
endfunction
05 May 2021
vim

More better faster vim text objects

article about a plugin

05 May 2021
vim

Vim - delete stuff without leaving insert mode:

  • <C-h> - same as backspace
  • <C-w> - delete previous word
  • <C-u> - delete everything before cursor (on same row)
  • <C-d> or <C-t> - (un)indent a row

inoremap <C-e> <C-o>de then:

  • <C-e> - delete next word
05 May 2021
vim

If you want to alias a bunch of arguments for a command, use global aliases

alias -g foo="some complicated options"

then grep some complicated options

becomes: grep foo

https://www.thorsten-hans.com/5-types-of-zsh-aliases/

05 May 2021
alias
linux

Vim - it’s annoying when you delete something and overwrite your yanked text.

Use numbered registers! "0 to "9

"0 contains the most recent yank. "1 contains the most recent deleted text

"0p - paste the most recent yank, even if you deleted something after yanking it

05 May 2021
vim
linux
text

Chezmoi is a great tool for managing dotfiles. This is a shortcut to update the source state based on local changes.

chezmoi status | cut -c 4- | xargs -I % -p sh -c ‘chezmoi add ~/%’

project

05 May 2021
dotfiles
alias

“It [sleep] enhances your memory and makes you more creative. It makes you look more attractive. It keeps you slim and lowers food cravings. It protects you from cancer and dementia. It wards off colds and the flu. It lowers your risk of heart attacks and stroke, not to mention diabetes. You’ll even feel happier, less depressed, and less anxious.” Dr. Walker - why we need to sleep

04 May 2021
sleep
lifestyle

How to write robust bash scripts:

Bash Strict Mode

04 May 2021
bash
linux

An often recommended blog post by Kaleigh Moore about writing a good about page.

04 May 2021
writing

Domain name registries:

03 May 2021
web

linux directories: /etc (etsy) → “editable text config” → a place to put config files.

Originally, the root directory had /boot for booting, /dev for devices… One dir for each type of thing. But this put config in many places. so etc/ became convention

https://www.pathname.com/fhs/pub/fhs-2.3.html

02 May 2021
linux
filesystem

April

Blog post about browser/Web-app security, including:

  • CSRF
  • CORS
  • HTTP Security Headers
30 April 2021
xss
cors
http
30 April 2021
html
jam

Interview with Linus Torvalds:

I don’t want to claim that programming is an art, because it really is mostly just about ‘good engineering’. I’m a big believer in Thomas Edison’s ‘one percent inspiration and ninety-nine percent perspiration’ mantra. It’s almost all about the little details and the everyday grunt-work.

But there is that occasional ‘inspiration’ part, that ‘good taste’ thing that is about more than just solving some problem - solving it cleanly and nicely and yes, even beautifully.

29 April 2021
linux
interview
linus

A RPC is when an executable causes a procedure (subroutine) to execute on another computer, It’s coded as if it were a normal (local) subroutine. You don’t explicitly code the details for the remote interaction. You write the same code whether the subroutine is local or remote.

29 April 2021
rpc
linux

Just found a really useful blog

Interesting discussion about the difference between builtins and executables,

Nice examples of using fzf to improve workflows.

29 April 2021
linux
shell
fzf
workflow
zsh
bash
builtin

lsblk is a command to get info about connected devices.

Used when attaching drives.

28 April 2021
unix
cli

Ranger is a console based file manager with vi key bindings.

Install it with brew install ranger

Launch it with ranger

28 April 2021
ranger
unix
vim
tools

This is a great article about using regular expressions in Vim:

marcelfischer.eu/blog/2019/checkbox-regex/

28 April 2021
vim
regex

Show all known locations of an executable where <executable>

See a list of all builtin commands man where

25 April 2021
unix
macos
cli
bash

Make aliased files the real file

for f in $(find . -type l -maxdepth 1);
    do cp --remove-destination $(readlink $f) $f;
done;
20 April 2021
macos
linux
cli
bash

Reorganize tmux windows

swap-window -s 3 -t 1

19 April 2021
tmux
macos
linux
cli

Save a Vim file with sudo when you opened it without sudo-ing:

:w !sudo tee %

17 April 2021
vim
macos
linux

The trouble with climbing higher is that eventually you lose sight of the ground.

16 April 2021
advice
thoughts
meta

Broot is a tool that shows the contents of a directory on one screen, even its got lots of files or sub-directories.

13 April 2021
broot
macos
cli

I added keyboard navigation to my blog and it works really well. Now I find myself trying to use the same shortcuts on other sites.

12 April 2021
blog
jam
jquery

I should see if I can use GitHub actions to generate html from markdown and run some shell and python scripts.

10 April 2021
github
blog

Its more socially acceptable to make mistakes and errors of omission than commission.

10 April 2021
meta
thinking
advice

du is a tool for showing disk usage.

There is a similar tool, with a list of other similar tools here

07 April 2021
cli
unix
macos

An example of a safe, good, robust bash file skeleton.

06 April 2021
bash

I was running a binary in Debian that was complaining about an environment variable not existing. I moved the binary into a &dollarPATH directory and logged in as a sudo user. Why did this solve the problem?

05 April 2021
debian
binary
path

As a hacker, or creator, or whatever the best label is, I always want to create something (usually code) and have it finished.

But a strange creativity and productivity boost comes from dabbling, dipping in and out.

I think that if the technical challenges aren’t too hard, then the main criteria for success is creativity.

Creativity needs time away from the project, and sleep, to bubble up and let ideas grow.

Ultimately, the most successful path is usually the most interesting, because success has more consequences than failure. “Interesting” requires elements of novelty and surprise, and without creativity these elements can’t flourish. 3/n

Dabbling results in more creativity than 6+ hours of strenuous work, and is more likely to give you satisfying results.

01 April 2021
meta
thinking
advice

bar << foo bar will stop reading input when it reached “foo”.

bar <<< "foo" foo is all the input. bar wont run interactively.

bar < <(foo:list) process subscription. Kind of like piping in the output of multiple commands.

Stack Overflow

01 April 2021
bash
syntax

March

Pretty print JSON

console.info(`${JSON.stringify(foo, null, 2)}`);
31 March 2021
typescript
json
syntax

analystsnow.com looks like a really useful business.

30 March 2021
entrepreneur
saas

Frantic distraction via productive procrastination is exhausting, and useful.

30 March 2021
meta
thinking

Rearrange splits in Vim

<C-w>x - swap buffers, but keep arrangement the same

<C-w>H - make the current split cover the left of the screen.

J, K, L covers the bottom, top, right of the screen

29 March 2021
vim

A guide about writing plugins for Pelican.

Thanks @geographeur

29 March 2021
pelican
plugin
guide

Tried to read and write the same file in a pipeline, and got caught out by a race condition (why is the file empty?!). Do this instead:

some_script < file > smscrpt.$$ \
&& mv smscrpt.$$ file || rm smscrpt.$$

|| removes the temporary file if it errors.

$$ is the process ID and ensures that you always have a unique temporary file name.

24 March 2021
shell
pipe
syntax
bash

JSON tools:

I like jq for pretty printing JSON output, jj for making JSON pretty or condensed.

This was really useful when optimizing the search index for this blog.

24 March 2021
json

Commands for working with Docker containers.

docker run -d ...
docker logs -f <ID>
docker run -it ...
docker run -itd
docker container attach <ID>
<C-p><C-q> -> detach from container interactively

stack overflow

17 March 2021
docker

Pipe a script file into bash, probably one you just curl-ed

curl -sSfL https://example.com/install.sh | sh -s
11 March 2021
bash
syntax
shell

Split long output onto multiple lines (find and replace on output)

... | tr ':' '\n'

... | sed 's/:/\n/g'
10 March 2021
bash
linux
  1. Without noticing, create a .gitignore file with a single * in it.

  2. Spend a day trying to understand why ripgrep has stopped working for only 1 project.

  3. sad face.

10 March 2021
ripgrep
fail

Teaching my kids about money and work is having an effect.

Yesterday, my daughter made a painting for me and asked my to buy it using pretend money.

05 March 2021
parenting
kids
money
teaching

Stop prepending docker commands with sudo:

sudo groupadd docker -> make the group 
sudo gpasswd -a $USER docker -> add $USER to the docker group
newgrp docker -> activate the changes
05 March 2021
sudo
docker
linux

If you’re cat-ing a file and the bash prompt doesn’t start on a new line (cos the file you displayed using cat doesn’t end with a new line char) the following will fix it:

cat <filename> ; echo

05 March 2021
cat
linux
bash
shell

Creating users with sudo permissions:

adduser -m <username>
usermod -aG sudo <username>

CentOS:

adduser -m <username>
passwd <username>
usermod -aG wheel <username>

(wheel is a usergroup with sudo permissions)

04 March 2021
sudo
linux
user
admin

df -h

Show disk space

du -hs .

See how big the current dir is

03 March 2021
linux
du
df
shell
cli

Cloning large repos, or repos with large files in them, doesn’t work with git clone ... you need to use git lfs clone ...

So why is git lfs clone deprecated? What’s the replacement?

03 March 2021
git
git-lfs

Use nohup to keep a curl process running even when the terminal (tty?) session autocloses at 3am.

02 March 2021
linux
cli

You can group shells into groups:

  1. ksh - korn shell and zshell
  2. sh - bourne shell and bash (the bourne again shell)

Because

  • zsh isn’t a superset of bash.
  • bash is a superset of the bourne shell.
02 March 2021
zsh
bash
bsh
linux
cli

November

Two nights ago, I dreamed I was experimenting with Emacs..

27 November 2020
emacs
dream