Dev: Vim magic substitutions, macros and yank buffers

I have been hacking away at Vim for a long time. There will be a long post on using Lazyvim for incredible power, but even the basic vim is crazy powerful. So as I’m writing lots of code right now

Yank buffers

I keep forgetting many of these commands, but if you want to to yank something and copy it repeatedly.

So the magic commands are to add something you need to run a yank noting a buffer.

“ayy this means yank the line in buffer a

“ap. Means but that register in.

With LazyVim when you type a quote it will show you the buffer contents

Mark to return to locations

Then you can go to the location you want and then

ma means mark that location in buffer a

‘a means go to the location in buffer a

Macros

This is always been confusing but today what buffer and then q ends the macro

qa starts the macro and stores in b register

q and the macro

To run the macro from with “a

Substitution patterns, substitutions and repeats

Ok substitute just to be pretty simple

:s/^.*$//. This mean at the beginning with ^ and .* matches anything and then $ for the end

But patterns can be complex and they have short cuts so this matches digits

:%s/\d*// This means % for the whole buffer and \d is shorthand for [0-9]

If you want to rerun the last substitute then && does that.

Substitute replacement patterns

This is where the syntax gets really ugly but if you put \( and \) in then you can refer to those patterns in the replacement so this goes through the buffer and leaves you with just non digits so if you have a url with a number in between and get rid of all the junk and then the \2 means the second pattern group which are the numbers got that.

%s/.*\(\d*\).*/\2/

The nice thing about Lazyvim is that it highlights what the selection is and shows you what the replacement would be.

LazyVim, : map and Which keys

One very nice neovim plugin is Which-keys which if you type the leader key. This is usually \ although pros change it to a SPACE for convenience. In too used to using space to move forward. But if you press backslash, quote, double quote you can see what’s in the buffers.

This only works for things that are uncommon like ‘g’, but if you want to know about Ctrl keys then you should search ‘:map’ or for LazyVim look at the key map page.

Leave a Reply

Only people in my network can comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

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…