patriques
patriques

Reputation: 5211

how to record both command and input mode keystrokes

Im trying to learn how to use the record ability of vim, but it seems as if I can only record and play keystrokes from the normal mode session. When I switch over to input mode the keystrokes I make does not seem to be recorded or does not play up when I play the recording. To illustrate what Im talking about:

I have an file with these rows:

Dir['*.data']        # Files with the "data" extension
Dir['?']             # Any single-character filename
Dir['*.[ch]']        # Any file that ends with .c or .h
Dir['*.{java,rb}']   # Any file that ends with .java or .rb

And I want to move the comments at the end of each line 4 tabs further to the right. So I put the cursor on the beginning of the first row and I start recording to register a: qa, then I type f# to find the comment on line, and then switch over to insert mode i and type <Tab><Tab><Tab><Tab> switch over to normal mode again esc and move down to the beginning of the next line and stop the recording q. When I play up the recording @a only the cursor moves down but none of the input keystrokes plays up?

Upvotes: 0

Views: 298

Answers (2)

Kent
Kent

Reputation: 195219

I am pretty sure that you have superTab installed in your vim. I had the same problem sometime ago.

What you can do for your needs is, you record in this way:

qaf#4i<c-v><tab><esc>j0q

then x@a

so, press Ctrl-v<Tab> instead of <tab>

BTW, a little trick: if you want to save the x, (re-play how many times), you could just use a recursive/nested macro:

qaf#4i<c-v><tab><esc>j0@aq

after that, you just press @a it will do the same till the end of the line.

Note, this answer is just for your macro problem, not for the editing problem. If you just want to achieve your Editing goal, I would go with C-V block selection and I instead of recording macro.

Upvotes: 2

romainl
romainl

Reputation: 196789

You forgot the last q step in your question, did you skip it in real life too?

Anyway, your macro works, it's what you recorded in that macro that doesn't work: after you have pushed your comments to the right and exited insert mode, you move the cursor down but it's now in the middle of the comment. f# can't work because there's no # after the cursor on that line:

Dir['*.data']                    # Files with the "data" extension
Dir['?']             # Any single-character filename
                                ↑
                           the cursor

Starting your recording with 0 is a macro best practice: it places the cursor on the very first column and ensures that further moves and thus further executions will work as intended.

In your case, f# would work if you would do 0 first.

qa
0
f#
i<Tab><Tab><Tab><Tab>
<Esc>
j
q

Upvotes: 1

Related Questions