Append to a Write-Protected File

Suppose you want to append to a file that’s write-protected and can only be edited via sudo. Is that possible? It is! A technique I discovered today is to use tee with sudo: $ cat file.txt | sudo tee -a /etc/hosts This lets me append the contents of file.txt to the write-protected /etc/hosts file by entering my password.

February 22, 2024

Create a File With `cat`

When you combine cat (concatenate) with > (output redirection), you can create and write to a file from the command line. $ cat > test.txt Hit enter after the file name, and your terminal will wait for input. End the file with CTRL + D. $ cat > test.txt one two three $ cat test.txt one two three >> works, too; appending to the file as expected.

December 12, 2023

Override Autojump's Directory Rankings

Autojump is my tool of choice for navigating directories. If I enter the following into the terminal: $ j til Autojump will try to guess where I want to go among directories I’ve previously visited. It uses a database of directories and their relative rankings to do so. But there’s a problem! These day I only want to visit ~/oss/til. In the past I’ve visited ~/oss/hr-til and ~/oss/tilex hundreds of times. Autojump always sends me there first. I don’t want that! ...

March 8, 2023

Force ASDF Reshim

When I upgrade ASDF, I sometimes get an error like this when I try to run a command: /Users/jakeworth/.asdf/shims/jq: line 3: /usr/local/Cellar/asdf/0.9.0/libexec/bin/asdf: No such file or directory The issue is that I’ve upgraded ADSF to v0.10.2, but the jq shim doesn’t know that and can’t find it in a directory called “0.9.0”. This issue persists even after running asdf reshim. My solution is to force ASDF to rebuild shims directory by renaming it. A rename lets me get back to where I am now, in case I need to. ...

June 21, 2022

Ctags Dotfiles

Something I took for granted at Hashrocket was the file that configures Exhuberant Ctags. Without configuration, recursive tag generation will tag many things and cause problems. overindex badly. Here’s how you configure Ctags to ignore things: /ctags.cnf (on MSDOS, MSWindows only) /etc/ctags.conf /usr/local/etc/ctags.conf $HOME/.ctags $HOME/ctags.cnf (on MSDOS, MSWindows only) .ctags ctags.cnf (on MSDOS, MSWindows only) If any of these configuration files exist, each will be expected to contain a set of default options which are read in the order listed when ctags starts, but before the CTAGS environment variable is read or any command line options are read. This makes it possible to set up site-wide, personal or project-level defaults. Here’s the head of my root .ctags, which even this far in is excluding a lot: ...

March 18, 2022

Save Size Change to iTerm Profile

The longer I program (ahem), the bigger I want my terminal fonts. I think most programmers should be using a bigger font than they are. Complicating matters, the standard font for iTerm is miniscule on a computer monitor. Today I discovered iTerm’s ‘View > Size Changes Update Profile’ feature. When enabled, changes to your font size via ⌘ + and ⌘ - will be applied to your iTerm profile. The next time you open iTerm, the new font size will persist.

November 13, 2021

Autojump and Open

I love autojump. I use it because I think that remembering my location in a directory structure relative to other directories, so I can navigate with cd, is mental energy I could be using for something else. Today I learned that autojump has a jo command, which jumps to a directory and opens it in your file explorer. jo Desktop This might be a throwaway lesson if I wasn’t regularly sidetracked by the MacOS file explorer. Finding a file using the file explorer is a routine operation I have yet to do fluidly on a Mac. This helps!

November 6, 2021

Command Line Subshell

When chaining commands in the terminal, you may need to do things that affect the shell’s environment, like change directories. Here’s an example: $ yarn install && cd ios && pod install && cd .. && yarn build These cd’s can clutter up a script, and they’re incidental. My solution is to use a subshell (here’s the explanation from man bash): (list) list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and builtin commands that affect the shell’s environment do not remain in effect after the command completes. The return status is the exit status of list. ...

August 2, 2021

Directional Commands

You can move the cursor without arrow keys. Here is the keyboard equivalent for each. Up (‘previous’): CTRL + P Down (’next’): CTRL + N Left (‘back’): CTRL + B Right (‘forward’): CTRL + F Mapping caps lock to CTRL makes these combinations very accessible.

April 21, 2021

Finding Getters

After writing the other day about why you might not want to use simple getters, I decided that I wanted to eliminate all such methods from a project. Here is the regex I wrote to isolate the pattern: ag 'def (\w+);?\s+@\1;?\s+end' The semicolon catches one-line getter methods as well as the more common three-line.

April 21, 2021