How to (safely) swap the contents of two files opened in (neo)vim buffers

Raise your hand if you’ve been here before: you have file1 open in a vim or neovim buffer and you want to “fork” its contents over to file2, but you need to reference file1 while you do so. So you do the obvious: you open a split buffer with :sp or :vsp, run a quick :saveas file2 then hack away at the file to make the changes you want followed by :w (or whatever shortcut you have mapped to the same) and call it a day… only to realize that you were in the wrong split and that you’ve accidentally switched file1 and file2 around?

Continue reading

Portable (Cartesian) brace expansion in your shell

Cartesian expansion, also known as brace expansion, is an incredibly powerful feature of most unixy shells, but despite being fundamentally simple and incredibly empowering, it’s been traditionally relegated to the dark and shadowy corners of command line hacking, employed only by greybeards looking to avoid repeating themselves at any cost. And, boy, does it really cut down on repetition.

Take for example this snippet from a Dockerfile that sets up permissions on certain directories:

mkdir -p /var/log/php /var/log/unitd /var/log/mysql
chown -R user:user /var/log/php /var/log/unitd /var/log/mysql
chmod ug+rw /var/log/php /var/log/unitd /var/log/mysql

Continue reading