We’re rewarding the question askers & reputations are being recalculated! Read more.

Hot answers tagged

41

Note: I'm assuming you're using the a register for the entirety of this answer, but you can use any register Note2: <1b> is Esc; you can insert this with Ctrl+v and then Esc. It may also show up as ^[ (depending on the display setting). It's not a mapping; so it makes sense that :map doesn't work. It's recorded in a register, and you can see & ...


35

In pure Vim fashion: :%s/^/\=line('.').". " Explanation: :%s/^/ " the substitution will be applied to the beginning of every line \= " the rest of the replacement part is an expression line('.').". " " the expression returns the current line number concatenated with a dot and a space See :help \= and :help line(). Using an ...


30

q is the command to begin the recording of a macro. By pressing qq you have told Vim to record a macro and store it in the register q. To stop Vim to record the macro, and thus to make the message recording disappear, you have to stop the recording by pressing q again. See :help recording for more details.


22

Without plugins | indicates cursor position func(foo[0], bar(), |baz.spam) Steps: dt) will delete baz.spam Ff will move the cursor to the character f in foo[0] vt,p will delete foo[0] and replace it with baz.spam $P will paste the foo[0] before the parens Repeat steps 1-4 for any others. With plugins vim-argumentative by Peter Rincker : Provides text ...


18

If you are already in Vim, you can use the :argdo or :bufdo commands to execute a command on every item in the argument list or buffer list, respectively. e.g. to run a : command on every file in the argument list: :argdo StripWhitespace Or to invoke a function from every file in the buffer list: :bufdo call StripWhitespace() Or to run macro q on every ...


17

One way to do it: start your macro as usual (e.g. qq ... other chars) when you reach the cmdline-mode, write all the characters you want you want to end the macro here. Hit ESC to get out of cmdline-mode, q to end your macro. remove the ESC from your q register (*): you can paste it in a buffer, remove the ^[ character, select the whole stuff again (...


17

You can use the :global and :normal commands for this. The :global command has the following syntax: :global/{pattern}/{command} This will run "command" on every occurrence of "pattern". You can then use the :normal command to run a series of keystrokes on each occurrence of a word using :global. Like so: :g/WORD/normal {keystrokes} If those ...


16

Similar to the answer at https://vi.stackexchange.com/a/818/227, you can use the global command. With it you can instruct vim to search for lines matching a pattern, and then perform commands on it. In your case, you wish to prepend text to lines starting with "Level N:", so our global command could be :g/^Level \d:/{COMMANDS} Using the substitute ...


15

Yes, you can! There are a couple ways to do this. By default, all registers will be saved into your viminfo file, and loaded once you start vim. This is the easiest way. However, it's not foolproof. Each register will be lost if you accidentally record/yank over it. The better way to save a specific macro is to put it in your .vimrc. For example, let's say ...


14

Since Vim 8.1-0020, there is a reg_recording() function that'll return the name of the current register being recorded. An empty string is returned if we are not recording.


13

I think the following command should work : :%s/^\(.*\)\(\n\1\)\+$/\1/ Explanation : We use the substitution command on the whole file to change pattern into string : :%s/pattern/string/ Here pattern is ^\(.*\)\(\n\1\)\+$ and string is \1. pattern can be broken down like this : ^\(subpattern1\)\(subpattern2\)\+$ ^ and $ match respectively a ...


11

Another solution is when in command line mode, use Ctrl-f, and press q to stop recording. Ctrl-f allows you to open the command line window, in which you can edit the command in normal mode. To validate the command, simply use enter. Therefore, after having added something like: cnoremap <c-q> <c-f>i<c-o>q in your .vimrc, you can use &...


11

There is probably a simpler method but maybe you could try the following. Let's say you will use register q to record your recursive macro. At the very beginning of the recording, type: :let a = line('.') Then, at the very end of the recording, instead of hitting @q to make the macro recursive, type the following command: :if line('.') == a | exe 'norm @...


11

While it can be done with scripting, if you have a very recent version of Vim (e.g. the one in Arch Linux), you can use g Ctrl-a to increment a visual block. In your case: Write the original line: A = mapping[0] Esc Yank line and put 25 copies of it below: yy25p Visually select the column containing the 0 char (block selection, using Ctrl-v), excluding the ...


10

Setting the lazyredraw with :set lazyredraw will greatly improve macro execution speed by not redrawing the screen while a macro is executing or a command is run other than those you type, see :help 'lazyredraw' for more information. Another way to reduce make sure you don't have any time wasting autocmds or mappings. If your macros are still slow after ...


10

AFAIK, you cannot record two macros by qx...q in one shot. For your requirement, you have to create macro b by qb....q, then qa...@bq. If you don't want to "record" b you can do let @b='whatever' then @b will replay the macro. E.g. let @b='ggdG' define a macro b to remove all content of your current buffer without "recording". There is a macro definition ...


10

Try the following: :%s;\v^(.*)(\n\1)+$;\1; As with saginaw's answer, this uses Vim's :substitute command. However, it takes advantage of a couple of extra features to improve readability: Vim lets us use any non-alphanumeric ASCII character except backslash (\), double-quote ("), or pipe (|) to divide our match/replace/flags text. Here, I selected ...


10

How about writing a 2nd macro that moves down n lines and then executes @a? Using a mark you can easily jump back to your original position. E.g., having defined your macro in register a, press qb to record in register b, press 5j to go to line 6, press @a to execute a and then q to quit recording. Using 9@b will then execute macro a on every 5th line, i.e. ...


10

If you have a sequence of keystrokes that you want to execute in normal mode from the command line, you can use the :normal command. However, by default the :normal command can't be followed by another command because as the help says: This command cannot be followed by another command, since any '|' is considered part of the command. So, if you ...


9

One nice thing about Vim macros is that they can recurse (they can invoke themselves): Clear out register q: qqq Add the number to the first line: ggI1. (don't forget the space!) Move back to start of line and start recording a macro: 0qq Copy the number: yW Move down a line and paste the number: +P Move back to the start of the line and increment the ...


9

To get something to work with... qajjjq Will start record a macro into the a register. You can see many of your current registers (used for macros, yanking, deleting, etc.) with the :reg command, or you can specify a register to display by providing its name. For example, to show register a: :reg a yields --- Registers --- "a jjj You can append to ...


9

A recursive macro will stop as soon as it encounters a command that fails. Therefore, to stop at the end of a line, you need a command that will fail at the end of the line. By default*, the l command is such a command, so you can use it to stop a recursive macro. If the cursor is not at the end of the line, then you just need to move it back afterwards ...


9

Use the /e flag. From :help s_flag [e] When the search pattern fails, do not issue an error message and, in particular, continue in maps as if no error occurred. This is most useful to prevent the "No match" error from breaking a mapping. Vim does not suppress the following error messages, however: Regular expressions can't be ...


9

You've already (rightly) accepted an answer that provides a much simpler method of achieving your goal, but I thought I'd address a few of the other issues raised by your question. The problems with your :let command To debug your macro, a first step might be to try recording a working macro, and then comparing this with the macro created by your let ...


8

Indeed it is pretty safe to manipulate other programs while a macro is executed. A macro simulates keystrokes within Vim which has no interaction with external GUI applications. As macros don't depend on the movement of the mouse, no conflicts can appear (Or at least I've never experienced them or heard of someone who has). EDIT As Sato observed in the ...


8

You could include this command at the beginning of your macro to save the current value of register a: let old_a = getreg(a) yank the highlighted word to register a: "ay and reset register a once you are done: call setreg("a", old_a) Macros are great but there's always a point where they become too complex to maintain. You should probably consider a ...


8

You can use the -c argument to run a command on startup, from vim(1): -c {command} {command} will be executed after the first file has been read. {command} is interpreted as an Ex command. If the {command} contains spaces it must be enclosed in double quotes (this depends on the ...


8

If you want to remove ALL adjacent identical lines, not just Hold, you can do it extremely easily with an external filter from within vim: :%!uniq (in a Unix environment). If you want to do it directly in vim, it's actually very tricky. I think there is a way, but for the general case it is very tricky to make it 100% functional and I haven't worked out ...


8

Well, if you want turn them into a macro specifically, then this is pretty easy to do. The thing you need to know about macro registers is that they are exactly the same as text registers that you cut/copy/paste from. So if you had the following text on a line: iHello<esc> And you wanted to turn this into a macro, you could just go to the beginning ...


8

The :r command can read a file containing a code snippet and insert it to your active buffer. Unless I'm missing something, I believe this would address your request.


Only top voted, non community-wiki answers of a minimum length are eligible