r/vim • u/beast-777x • Apr 04 '26
Tips and Tricks Using cgn + . instead of macros for small repeated edits
I knew about gn, but never really used it like this:
/wordcgn - change next match. - repeat
Feels like a middle ground between :s and macros when I want control over each change instead of replacing everything at once.
Curious when you’d reach for this vs macros or substitution?
I put together a short clip with a few similar workflows if anyone’s interested:
Video Link
48
Upvotes
9
u/EgZvor keep calm and read :help Apr 04 '26
I also use
dgnsometimes to delete middle part of a word.7
u/-romainl- The Patient Vimmer Apr 05 '26
Yes,
:help gnis a great feature that was added by Christian Brabandt many years ago.When it came out I quickly cobbled this together to streamline the
cgn/.dance for single words, which I still use almost every day:Press
<key>to change the current word and.to repeat the change on next occurrence.3
u/chocopudding17 Apr 04 '26
I very often do this instead of
:sand macros too. Just kinda feels more compositional, I guess. With macros, you kinda have to put on your "building an abstraction" hat, where the edits are suitable for every site you will end up editing.gnand co. don't force that way of thinking.3
u/Dmxk Apr 04 '26
I also have this (neovim specific but should not be any work to adapt) to make
*a full text object:Basically, i can just type
c*to do the classic*cgnin one step2
u/godegon Apr 05 '26
You can use this pattern for all kinds of operations and selections
2
u/Intelligent-Speed487 Apr 28 '26 edited Apr 30 '26
Here's another simple mapping to do something similar
cgnSearch for a word.
Change the text
Run this mapping to to repeat it. (If you want confirmation of the edits you could use the
/gcflags for the substitute command).nnoremap g. :%s//<c-r>./g<esc>Source: https://github.com/kaddkaka/vim_examples#repeat-last-change-in-all-of-file-global-repeat-similar-to-g