Quick links: help overview · quick reference · user manual toc · reference manual toc · faq
Go to keyword (shortcut: k)
Site search (shortcut: s)
usr_10.txt  	For Vim version 9.1.  Last change: 2025 Nov 09


		     VIM USER MANUAL	by Bram Moolenaar


			     Making big changes


In chapter 4 several ways to make small changes were explained.  This chapter
goes into making changes that are repeated or can affect a large amount of
text.  The Visual mode allows doing various things with blocks of text.  Use
an external program to do really complicated things.

10.1  	Record and playback commands
10.2  	Substitution
10.3  	Command ranges
10.4  	The global command
10.5  	Visual block mode
10.6  	Reading and writing part of a file
10.7  	Formatting text
10.8  	Changing case
10.9  	Using an external program

     Next chapter: usr_11.txt  Recovering from a crash
 Previous chapter: usr_09.txt  Using the GUI
Table of contents: usr_toc.txt

==============================================================================
10.1  	Record and playback commands

The "." command repeats the preceding change.  But what if you want to do
something more complex than a single change?  That's where command recording
comes in.  There are three steps:

1. The "q{register}" command starts recording keystrokes into the register
   named {register}.  The register name must be between a and z.
2. Type your commands.
3. To finish recording, press q (without any extra character).

You can now execute the macro by typing the command "@{register}".

Take a look at how to use these commands in practice.  You have a list of
filenames that look like this:

	stdio.h 
	fcntl.h 
	unistd.h 
	stdlib.h 

And what you want is the following:

	#include "stdio.h" 
	#include "fcntl.h" 
	#include "unistd.h" 
	#include "stdlib.h" 

You start by moving to the first character of the first line.  Next you
execute the following commands:

	qa			Start recording a macro in register a.
	^			Move to the beginning of the line.
	i#include "<Esc>	Insert the string #include " at the beginning
				of the line.
	$			Move to the end of the line.
	a"<Esc>			Append the character double quotation mark (")
				to the end of the line.
	j			Go to the next line.
	q			Stop recording the macro.

Now that you have done the work once, you can repeat the change by typing the
command "@a" three times.
   The "@a" command can be preceded by a count, which will cause the macro to
be executed that number of times.  In this case you would type: 

	3@a


MOVE AND EXECUTE

You might have the lines you want to change in various places.  Just move the
cursor to each location and use the "@a" command.  If you have done that once,
you can do it again with "@@".  That's a bit easier to type.  If you now
execute register b with "@b", the next "@@" will use register b.
   If you compare the playback method with using ".", there are several
differences.  First of all, "." can only repeat one change.  As seen in the
example above, "@a" can do several changes, and move around as well.
Secondly, "." can only remember the last change.  Executing a register allows
you to make any changes and then still use "@a" to replay the recorded
commands.  Finally, you can use 26 different registers.  Thus you can remember
26 different command sequences to execute.


USING REGISTERS

The registers used for recording are the same ones you used for yank and
delete commands.  This allows you to mix recording with other commands to
manipulate the registers.
   Suppose you have recorded a few commands in register n.  When you execute
this with "@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".  (If your recorded
commands include line breaks, adjust the last two items in the example to
include all the lines.)


APPENDING TO A REGISTER

So far we have used a lowercase letter for the register name.  To append to a
register, use an uppercase letter.
   Suppose you have recorded a command to change a word to register c.  It
works properly, but you would like to add a search for the next word to
change.  This can be done with: 

	qC/word<Enter>q

You start with "qC", which records to the c register and appends.  Thus
writing to an uppercase register name means to append to the register with
the same letter, but lowercase.

This works both with recording and with yank and delete commands.  For
example, you want to collect a sequence of lines into the a register.  Yank
the first line with: 

	"aY

Now move to the second line, and type: 

	"AY

Repeat this command for all lines.  The a register now contains all those
lines, in the order you yanked them.

==============================================================================
10.2  	Substitution						find-replace

The ":substitute" command enables you to perform string replacements on a
whole range of lines.  The general form of this command is as follows: 

	:[range]substitute/from/to/[flags]

This command changes the "from" string to the "to" string in the lines
specified with [range].  For example, you can change "Professor" to "Teacher"
in all lines with the following command: 

	:%substitute/Professor/Teacher/

	Note:
	The "