Vim

De Mathux
Révision datée du 10 mars 2023 à 10:34 par Admin (discussion | contributions)
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)

Edit Macro

The registers are used both for macros and for yank and delete commands. This has some advantages.

Suppose you have recorded a few commands in register n. Then, when you run @n, you notice you did something wrong. You could try recording again, but perhaps you will make another mistake. Instead, use this trick:

  • G — Go to the end of the file.
  • o<Esc> — Create an empty line.
  • “np — Put the text from the n register. You now see the commands you typed as text in the file.
  • {edits} — Change the commands that were wrong. This is just like editing text.
  • 0 — Go to the start of the line.
  • “ny$ — Yank the corrected commands into the n register.
  • dd — Delete the scratch line.

Now you can execute the corrected commands with @n.

This trick is a great example of the Unix and Vim philosophies: If you know how to modify text, and if everything is made out of text, then you can do anything.

N.b. :From https://sebastiancarlos.com/use-vim-macros-in-your-ide-and-unleash-its-hidden-power-3a17eda8400e

Configuration

My configuration files are hosted here. A simplified version (more re-usable) is here

Copy

  • Copy into the clipboard

"*y

  • Past from the clipboard

"*p

  • Copy/Past into/from the register "a"

"ay / "ap

Other

  • Substitute every occurrence of '0' with incremental values:
:let @a=0 | %s/0/\=(@a+setreg('a',@a +1))/g
  • Delete all trailing whitespace (at the end of each line) with
:%s/\s\+$//