Deep vi hacking when you have 400 files to edit with registers, macros, mksession and wv

Ok this is a truly deep post only for those who are living in vi/vim/neovim and have 400 files to manually edit (because they were idiots and didn’t use shellcheck early enough for their many random bash scripts).

So if you have to do this, it rapidly becomes pretty old. You could try to write a really fancy awk script to fix things, but stuff like this does need a little bit of a manual touch still. So here are the tools that you need:

  1. Registers. There are 26 named registers where you can store things like say a # shellcheck source=./include.sh command that you have to write a million times. You specify the buffers with a double quote so "iyy means store into the register i, the entire line. and "eY means put into register e everything from the cursor to the end of the line. To copy that register contents to wherever your cursor is, type "ip to get register i. Note that the internet often confuses buffers for registers. In vi speak, buffers are where you stick files, where registers are where you stick little snippets.
  2. Macros. These are hard to find but kind of neat. This actually uses the same storage space as registers and really just stores, so to say start recording a macro into register q, you would start with qq so q starts recording and then you enter the register number. Then type in whatever commands you like and hit q again. To execute the macro, use the ampersand like @q and if you want to see what you’ve recorded, "q will put it at the cursor.
  3. Viewing macros and registers. One big problem is that there is no easy way to see what you have in your macros and registers, but there is a :registers command to see it all, or just :register abx if you want to see the contents of say the a, b and x registers.

Saving all this stuff

  1. Mksession. If you have a long running set of edits like this, then :mksession will generate a Session.vim file in the current directory. So you can edit and edit and when you quit, do a :mks! and when you restart run vim -S. This saves all the buffers and open files.
  2. Viminfo. This is in ~/.viminfo and stores registers and macros but not what is in sessions. It is across multiple sessions, so serves as your overall session. If you want multiple of these, then you can run wv Viminfo to stick it in the current directory and then you get restart both with vi -S -i .Viminfo

I’m Rich & Co.

Welcome to Tongfamily, our cozy corner of the internet dedicated to all things technology and interesting. Here, we invite you to join us on a journey of tips, tricks, and traps. Let’s get geeky!

Let’s connect

Recent posts

Loading Mastodon feed…