# Vim Macros Not Working

You recorded a macro to automate a repetitive task, but when you play it back, it does something completely different or nothing at all. Macro issues usually stem from how they were recorded or how Vim handles the commands. Let me show you how to fix them.

Understanding Macro Basics

Macros are sequences of commands stored in registers:

vim
qa    " Start recording to register a
...   " Your commands
q     " Stop recording
@a    " Play the macro
10@a  " Play macro 10 times
@@    " Repeat last macro

Macro Not Recording

Check the Register

The macro might be recorded to a different register than expected:

vim
:registers

Look for your register (e.g., a) in the list.

Recording Indicator

Ensure recording started. You should see recording @a in the status line:

vim
:set showcmd

If you don't see the recording indicator, check your status line configuration.

Overwritten Register

The register might have been overwritten. Named registers (a-z) are also used for yanking:

vim
" If you do this while recording:
"ayy    " Yanks line to register a, overwriting your macro!

Avoid using the same register for both macros and yanking during recording.

Macro Plays Wrong Commands

Mode Changes During Recording

Macros record exact keystrokes, including mode changes. If you end in insert mode during recording, the macro will start in normal mode and try to enter insert mode:

```vim " Good macro recording: qa " Start recording 0 " Go to line start i# <Esc> " Insert # and return to normal mode q " Stop recording

" Bad (ends in insert mode): qa " Start recording 0 " Go to line start i# " Insert # (still in insert mode!) q " Stop recording - inconsistent state ```

Always end your macro in normal mode.

Positional Issues

Macros record absolute positions, which can cause problems:

```vim " Bad - depends on word length qa ww " Move two words forward (depends on text!) q

" Better - use search qa /pattern<CR> " Find pattern q ```

Use Relative Movements

```vim " Bad - absolute line number qa :10<CR> " Go to line 10 q

" Good - relative movement qa jjj " Move down 3 lines q ```

Or better:

vim
" Use search or text objects
qa
/search<CR>
q

Macro Execution Stops Midway

Error Halts Execution

Macros stop when a command fails:

vim
" If j (move down) fails on the last line, the macro stops
qa
j
dw
q

Suppress errors with :normal!:

vim
:let @a = 'j!dw'

Or use :silent!:

vim
:silent! normal @a

Unexpected Mode

If you're in the wrong mode when executing:

vim
" Ensure normal mode first
<Esc>@a

Edit a Macro

If your macro has an error, edit it rather than re-record:

```vim " Paste macro contents :put a

" Edit the line " (make your changes)

" Yank back to register "ayy ```

Or modify directly:

vim
" Append to macro A (uppercase appends)
qA
...additional commands
q

Macro with Special Characters

Special characters need to be represented differently when editing:

KeyRepresentation
Enter<CR> or <Enter>
Escape<Esc> or <Esc>
Tab<Tab> or <Tab>
Ctrl-x<C-x>
Alt-x<A-x> or <M-x>

When viewing registers, these appear as actual characters. To insert them when editing:

vim
" In insert mode, type:
Ctrl-V <Esc>    " Insert literal Escape
Ctrl-V <Enter>  " Insert literal Enter

Recursive Macros

Macros can call themselves:

```vim " Record to a qa j @a " Call macro a (recursively) q

" This will run until it errors (e.g., can't move down anymore) @a ```

Use with caution. Always ensure a stopping condition.

Macro in Command Mode

Use macros from command mode:

```vim " Run macro a on all lines :%normal @a

" Run on range :10,20normal @a ```

Save Macros Permanently

In .vimrc

vim
let @a = '0i# <Esc>'

In a Function

```vim function! CommentLine() normal! 0i# endfunction

nnoremap <leader>c :call CommentLine()<CR> ```

Debugging Macros

Step Through Macro

Use :normal to see what happens:

```vim " See each keystroke :set showcmd

" Execute slowly to watch :normal! @a ```

Print Macro Contents

vim
:echo @a

Verify Register

vim
:reg a

Common Macro Mistakes

Not Returning to Normal Mode

```vim " Bad - ends in insert mode qaiHelloq

" Good - returns to normal mode qaiHello<Esc>q ```

Hardcoded Counts

```vim " Bad - only works for specific number of words qadwwwq

" Good - works for any line qadw0q ```

Ignoring Edge Cases

```vim " This fails on the last line qajdwq

" Better - check for more lines qaj:if line('.') < line('$')<CR>dw<CR>endif<CR>q ```

Macro vs. Command-line

For complex operations, a command might be better:

```vim " Instead of a macro :%s/^/# /

" Instead of a macro :g/pattern/d ```

Repeat Last Change

If you don't need a full macro, . repeats the last change:

vim
dw    " Delete word
.     " Delete next word
.     " Delete next word

Macro Performance Tips

For large files, macros can be slow. Use :s or :g instead:

```vim " Slow with macro qa0dwjq 10000@a

" Fast with substitute :%s/^\w\+ // ```

Quick Reference

```vim " Record macro qa...q

" Play macro @a

" Play macro N times 10@a

" Repeat last macro @@

" View macro :reg a

" Edit macro :put a " Paste " (edit) "ayy " Yank back

" Append to macro qA...q " Uppercase appends

" Run on all lines :%normal @a

" Save permanently (in .vimrc) let @a = 'commands' ```

Macros are powerful when recorded correctly. Plan your keystrokes, always end in normal mode, and use relative movements for portable macros.