Creating and Using Keyboard Macros (Record Macro, Play Macro) to Record and Playback Keystrokes in Vim

  • In normal mode, press the key sequence q followed by a. This starts the recording mode (and you should see the word “recording” on the mode-line at the bottom).
  • Then enter or exit the insert-mode as many times as you wish, editing text using the keyboard. All the time, vim will keep recording all your keystrokes.
  • Finally exit insert-mode (get into normal mode) and then press q. This will stop recording your keystrokes.
  • Playback the recorded macro in normal mode, anytime, using the key-sequence @ followed by a. This will playback all the edits you made earlier. Once you’ve referred to a the first time, you can subsequently use @ followed by @ (a double @) to repeat the operation.

Instead of the key a following q when starting recording mode, you can really use any one of the keys 0 to 9 or a to z. The second key is basically a reference to a ‘register’ that sort of names your macro.  You can record multiple different macros and play them back by recording to each one of these ‘registers’. The key you use on playback after the @ refers to the appropriate macro recorded.

Recording and playback of keystrokes in Emacs is explained here.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *