I’ve been doing a little project and took a moment to get a bit better at using tmux.
Every time I go into this project I set up some splits. A main window where I’ll edit files using vim, then I split a pane off to run the code or test suite on every save. Another split where I pip install after any changes to my requirements.txt file.
Since I do the same thing repeatedly I was pretty sure tmux has a way to set this up so I don’t need to do it by hand. I tried using tmux session saving plugins, but they are too much for what I need right now.
Turns out tmux is incredibly easy to script. This gist is very long and very informative on how to split windows in tmux and covered everything I needed.
# split -h horizontally to take up 30% of the width to run my __main__.py file on every save of a python file
# this is -d detached so that focus remains on the main window
tmux splitw -d -h -p 30 'ls *.py | entr -c env/bin/python . ./goodreads_library_export.csv data.csv ~/books'
# split my second pane vertically with 20% for rerunning pip installs on save of requirements.txt
tmux splitw -d -t 2 -p 20 'ls requirements.txt | entr -c env/bin/pip install -r requirements.txt'
# create a little detached shell just in case I need to try something
tmux splitw -d -t 3
# open up the python files in tabs in my main pane
vim -p *.py
entr is a great little tool I like for monitoring for file changes and running a command in response.
I just learned something that gave me a EUREKA moment. The text you type in vim is also potentially instructions to vim and the instructions are just text. You can record a macro, a series of keystrokes that you play back, in Vim by typing:
a letter to record your keystrokes into – let’s use r
then doing your keystrokes
then type q again to stop recording.
record a macror
go down6 lines
move forward to a pipe character “|”
Changethe next 3 words to say |width=auto then escape edit mode
go down9 lines
move forward to a pipe character “|”
change the next 3 words to |width=80% then escape edit mode
stop recording the macro
To apply that macro or play it back: type @r. Vim will play all those keystrokes back from where you are in a document. That’s really useful when you want to automate something repetitive.
I changed my mind in how I wanted something to look in a wiki at work. 80 pages needed to be changed in a dumb way and I thought it might take longer to explain to people that it needed to be done than to just do it myself. I started by copying each page to vim to edit, running a macro of keystrokes to make the change, then pasting the result back.
Then I hit a couple of pages that were slightly off. They broke the macro, which was long and complicated. I read up on how to edit a macro so I didn’t have to reenter it.
That’s when I found out that the “r” that we stored that macro in is just a register, just like you might yank into. In vim you copy/paste by yanking into a register with a y and pasting with a p:
“fyt; – store in registerf the yanking of everything on this line til a semicolon
“fp – take what is in registerf and put it where the cursor is.
I typed “rp and saw my macro contents there on a line. I edited the macro to account for these new differences and then did “tD to delete the entire line and store it in registert. Then I went to the top of the document by typing gg and applied my macro t by typing @t and it just worked!
How cool is that? There is no new scripting language to learn – all the text you delete and paste is potentially also instructions. This must be what lispers feel like all the time.
Now I wonder about trying to apply documents as instructions to edit themselves….