usr_04.txt ForVim version 9.2. Last change: 2026 Feb 14 VIM USER MANUALbyBramMoolenaar Making small changesThis chapter shows you several ways of making corrections and moving textaround. It teaches you the three basic ways to change text: operator-motion,Visual mode and text objects.04.1 Operators and motions04.2 Changing text04.3 Repeatinga change04.4Visual mode04.5 Moving text04.6 Copying text04.7 Using theclipboard04.8 Textobjects04.9Replace mode04.10 Conclusion Next chapter:usr_05.txt Set your settings Previous chapter:usr_03.txt Moving aroundTable of contents:usr_toc.txt==============================================================================04.1 Operators and motionsIn chapter 2 you learned the "x" command to deletea single character. Andusinga count: "4x" deletes four characters. The "dw" command deletesa word. You may recognize the "w" commandas themoveword command. In fact, the "d" command may be followed by any motioncommand, andit deletes from the current location to the place where thecursor winds up. The "4w" command, for example, moves the cursor over four words. The "d4w"command deletes four words.To err is human. To really foul up you need a computer. ------------------> d4wTo err is human. you need a computer.Vim only deletes up to the position where the motion takes the cursor. That'sbecause Vim knows that you probably don't want to delete the first characterofa word. If you use the "e" command to move to theend ofa word, Vimguesses that youdo want to include that last character:To err is human. you need a computer.--------> d2eTo err is human. a computer.Whether the character under the cursoris included depends on the command youused to move to that character. Thereference manual calls this "exclusive"when the character isn't included and "inclusive" whenit is.The "$" command moves to theend ofa line. The "d$" command deletes from thecursor to theend of the line. Thisis aninclusive motion, thus the lastcharacter of the lineis included in the delete operation:To err is human. a computer. ------------> d$To err is humanThereisapattern here: operator-motion. You first type anoperator command.For example, "d"is the delete operator. Then you typea motion command like"4l" or "w". This way you can operate on any text you can move over.==============================================================================04.2 Changing textAnotheroperatoris "c", change. It acts just like the "d" operator, exceptit leaves you inInsert mode. For example, "cw" changesa word. Or morespecifically,it deletesaword and then puts you inInsert mode.To err is human -------> c2wbe<Esc>To be humanThis "c2wbe<Esc>" contains these bits:cthe changeoperator2wmove two words (they are deleted andInsert mode started)beinsert this text<Esc>back toNormal modeYou will have noticed something strange: Thespace before "human" isn'tdeleted. Thereisa saying that for every problem thereis an answer thatissimple, clear, and wrong. Thatis thecase with the example used here for the"cw" command. Thecoperator works just like thed operator, with oneexception: "cw". It actually works like "ce", change toend of word. Thusthespace after theword isn't included. Thisis an exception that dates backto the old Vi. Since many people are used toit now, the inconsistency hasremained in Vim.MORE CHANGESLike "dd" deletesa whole line, "cc" changesa whole line. It keeps theexisting indent (leading white space) though.Just like "d$" deletes until theend of the line, "c$" changes until theendof the line. It's like doing "d$" to delete the text and then "a" to startInsert mode and append new text.SHORTCUTSSome operator-motion commands are used so often that they have been givenasingle-letter command:x stands fordl (delete character under the cursor)X stands fordh (delete character left of the cursor)D stands for d$ (delete toend of the line)C stands for c$ (change toend of the line)s stands for cl (change one character)S stands forcc (changea whole line)WHERE TO PUT THE COUNTThe commands "3dw" and "d3w" delete three words. If you want to get reallypicky about things, the first command, "3dw", deletes oneword three times;the command "d3w" deletes three words once. Thisisa difference withoutadistinction. You can actuallyput in two counts, however. For example,"3d2w" deletes two words, repeated three times, fora total of six words.REPLACING WITH ONE CHARACTERThe "r" commandis not an operator. It waits for you to typea character, andwill replace the character under the cursor with it. You coulddo the samewith "cl" or with the "s" command, but with "r" you don't have to press<Esc>to get back out ofinsert mode.there is somerhing grong hererT rt rwThere is something wrong hereUsingacount with "r" causes that many characters to be replaced with thesame character. Example:There is something wrong here 5rxThere is something xxxxx hereTo replacea character witha line break use "r<Enter>". This deletes onecharacter and insertsa line break. Usingacount here only applies to thenumber of characters deleted: "4r<Enter>" replaces four characters with oneline break.==============================================================================04.3 Repeatinga changeThe "." commandis one of the simplest yet powerful commands in Vim. Itrepeats the last change. For instance, suppose you are editing an HTML fileand want to delete all the<B> tags. You position the cursor on the first<and delete the<B> with the command "df>". You thengo to the< of the next</B> and deleteit using the "." command. The "." command executes the lastchange command (in this case, "df>"). To delete another tag, position thecursor on the< and use the "." command. To <B>generate</B> a table of <B>contentsf< find first< --->df> delete to> -->f< find next< --------->. repeat df> --->f< find next< ------------->. repeat df> -->The "." command works for all changes you make, except for "u" (undo),CTRL-R(redo) and commands that start witha colon (:).Another example: You want to change theword "four" to "five". It appearsseveral times in your text. You cando this quickly with this sequence ofcommands:/four<Enter>find the firststring "four"cwfive<Esc>change theword to "five"nfind the next "four".repeat the change to "five"nfind the next "four".repeat the changeetc.==============================================================================04.4Visual modeTo delete simple items the operator-motion changes work quite well. But oftenit's not soeasy to decide which command will move over the text you want tochange. Then you can useVisual mode.You startVisual mode by pressing "v". You move the cursor over the text youwant to work on. While youdo this, the textis highlighted. Finally typetheoperator command. For example, to delete from the middle of oneword to the middle of anotherword:This is an examination sample of visual mode ----------> velllldThis is an example of visual modeWhen doing this you don't really have tocount how many times you have topress "l" toend up in the right position. You can immediately see what textwill be deleted when you press "d".Ifat any time you decide you don't want todo anything with the highlightedtext, just press<Esc> andVisual mode will stop without doing anything.SELECTING LINESIf you want to work on whole lines, use "V" to startVisual mode. You willsee right away that the whole lineis highlighted, without moving around.When you move left or right nothing changes. When you move up or down theselectionis extended whole linesata time. For example, select three lines with "Vjj": +------------------------+ | text more text |>> | more text more text | |selected lines>> | text text text | | Vjj>> | text more |V | more text more | +------------------------+SELECTING BLOCKSIf you want to work ona rectangular block of characters, useCTRL-V to startVisual mode. Thisis very useful when working on tables.nameQ1Q2Q3pierre123455234john09039steve39263334To delete the middle "Q2" column, move the cursor to the "Q" of "Q2". PressCTRL-V to start blockwiseVisual mode. Now move the cursor three lines downwith "3j" and to the nextword with "w". You can see the first character ofthe last columnis included. To exclude it, use "h". Now press "d" and themiddle columnis gone.GOING TO THE OTHER SIDEIf you have selected some text inVisual mode, and discover that you need tochange the otherend of the selection, use the "o" command (Hint:o for otherend). The cursor willgo to the other end, and you can move the cursor tochange where the selection starts. Pressing "o" again brings you back to theother end.When using blockwise selection, you have four corners. "o" only takes you toone of the other corners, diagonally. Use "O" to move to the other corner inthe same line.Note that "o" and "O" inVisual mode work very differently fromNormal mode,where they opena new line below or above the cursor.==============================================================================04.5 Moving textWhen you delete something with "d", "x", or another command, the textissaved. You can pasteit back by using the "p" command. (The Vim name forthisis put). Takea lookat how this works. First you will delete an entire line, byputting the cursor on the line you want to delete and typing "dd". Now youmove the cursor to where you want toput the line and use the "p" (put)command. The lineis inserted on the line below the cursor.a linea linea lineline 2ddline 3p line 3line 3 line 2Because you deleted an entire line, the "p" command placed the text line belowthe cursor. If you delete part ofa line (a word, for instance), the "p"command putsit just after the cursor.Some more boring try text to out commands. ----> dwSome more boring text to out commands. -------> welpSome more boring text to try out commands.MORE ON PUTTINGThe "P" command puts text like "p", but before the cursor. When you deletedawhole line with "dd", "P" willputit back above the cursor. When you deletedaword with "dw", "P" willputit back just before the cursor.You can repeat puttingas many timesas you like. The same text will be used.You can useacount with "p" and "P". The text will be repeatedas many timesas specified with the count. Thus "dd" and then "3p" puts three copies of thesame deleted line.SWAPPING TWO CHARACTERSFrequently when you are typing, your fingers get ahead of your brain (or theother way around?). The resultisa typo suchas "teh" for "the". Vimmakesiteasy to correct such problems. Justput the cursor on thee of "teh"and execute the command "xp". This worksas follows: "x" deletes thecharactere and placesit ina register. "p" puts the text after the cursor,whichis after the h.teh th thexp==============================================================================04.6 Copying textTo copy text from one place to another, you could delete it, use "u" toundothe deletion and then "p" toputit somewhere else. Thereis an easier way:yanking. The "y"operator copies text intoa register. Thena "p" commandcan be used toput it. Yankingis justa Vim name for copying. The "c"letter was already usedfor the change operator, and "y" was still available. Calling thisoperator "yank" madeit easier to remember to use the "y" key.Since "y"is an operator, you use "yw" toyanka word.Acountis possibleasusual. Toyank two words use "y2w". Example:let sqr = LongVariable * --------------> y2wlet sqr = LongVariable *plet sqr = LongVariable * LongVariableNotice that "yw" includes the whitespace aftera word. If you don't wantthis, use "ye".The "yy" command yanksa whole line, just like "dd" deletesa whole line.Unexpectedly, while "D" deletes from the cursor to theend of the line, "Y"works like "yy",it yanks the whole line. Watch out for this inconsistency!Use "y$" toyank to theend of the line.a text lineyya text linea text lineline 2line 2p line 2last linelast linea text line last line==============================================================================04.7 Using theclipboardIf you are using theGUI version of Vim (gvim), you can find the "Copy" itemin the "Edit" menu. First select some text withVisual mode, then use theEdit/Copy menu item. The selected textis now copied to the clipboard. Youcan paste the text in other programs. In Vim itself too.If you have copied text to theclipboard in another application, you can pasteit in Vim with the Edit/Paste menu item. This works inNormal mode andInsertmode. InVisual mode the selected textis replaced with the pasted text.The "Cut" menu item deletes the text before it'sput on the clipboard. The"Copy", "Cut" and "Paste" items are also available in thepopup menu (onlywhen thereisapopup menu, of course). If your Vim hasa toolbar, you canalso find these items there.If you are not using the GUI, or if you don't like usinga menu, you have touse another way. You use the normal "y" (yank) and "p" (put) commands, butprepend "* (double-quote star) before it. To copya line to the clipboard:"*yyToput text from theclipboard back into the text:"*pThis only works on versions of Vim that includeclipboard support. More abouttheclipboard can be found insection09.3 and here:clipboard.==============================================================================04.8 TextobjectsIf the cursoris in the middle ofaword and you want to delete that word, youneed to move back to its start before you cando "dw". Thereisa simpler waytodo this: "daw".this is some example text.dawthis is some text.The "d" of "daw"is the delete operator. "aw"isa text object. Hint: "aw"stands for "A Word". Thus "daw"is "DeleteA Word". To be precise, the whitespace after thewordis also deleted (or the whitespace before theword ifattheend of the line).Using textobjectsis the third way to make changes in Vim. We already hadoperator-motion andVisual mode. Now we add operator-text object. Itis very similar to operator-motion, but instead of operating on the textbetween the cursor position before and afteramovement command, the textobjectis usedasa whole. It doesn't matter where in theobject the cursorwas.To changea wholesentence use "cis". Take this text:Hello there. Thisis an example. Justsome text.Move to the start of the second line, on "is an". Now use "cis":Hello there. Justsome text.The cursoris in between the blanks in the first line. Now you type the newsentence "Another line.":Hello there. Another line. Justsome text."cis" consists of the "c" (change)operator and the "is" text object. Thisstands for "Inner Sentence". Thereis also the "as"("A Sentence") object.The differenceis that "as" includes the whitespace after thesentence and"is" doesn't. If you would deletea sentence, you want to delete the whitespaceat the same time, thus use "das". If you want to type new text thewhitespace can remain, thus you use "cis".You can also use textobjects inVisual mode. It will include the textobjectin theVisual selection.Visual mode continues, thus you cando this severaltimes. For example, startVisual mode with "v" and selectasentence with"as". Now you can repeat "as" to include more sentences. Finally you use anoperator todo something with the selected sentences.You can finda longlist of textobjects here:text-objects.==============================================================================04.9Replace modeThe "R" command causes Vim to enter replace mode. In this mode, eachcharacter you type replaces the one under the cursor. This continues untilyou type<Esc>. In this example you startReplace mode on the first "t" of "text":This is text.Rinteresting.<Esc>This is interesting.You may have noticed that this command replaced 5 characters in the line withtwelve others. The "R" command automaticallyextends the line ifit runs outof characters to replace. It will not continue on the next line.You can switch betweenInsert mode andReplace mode with the<Insert> key.When you use<BS> (backspace) to makea correction, you will notice that theold textisput back. Thusit works like anundo command for the previouslytyped character.==============================================================================04.10 ConclusionThe operators,movement commands and textobjects give you the possibility tomake lots of combinations. Now that you know how they work, you can useNoperators withMmovement commands to makeN *M commands!You can findalist of operators here:operator.For example, there are many other ways to delete pieces of text. Here areafew common ones:xdelete character under the cursor (short for "dl")Xdelete character before the cursor (short for "dh")Ddelete from cursor toend of line (short for "d$")dwdelete from cursor to next start ofworddbdelete from cursor to previous start ofworddiwdeleteword under the cursor (excluding white space)dawdeleteword under the cursor (including white space)dGdelete until theend of the filedggdelete until the start of the fileIf you use "c" instead of "d" they become change commands. And with "y" youyank the text. And so forth.There area few common commands to make changes that didn't fit somewhereelse:~Changecase of the character under the cursor, and move thecursor to the next character. Thisis not anoperator (unless'tildeop'is set), thus you can't useit witha motioncommand. It does work inVisual mode, whereit changescasefor all the selected text.IStartInsert mode after moving the cursor to the firstnon-blank in the line.AStartInsert mode after moving the cursor to theend of theline.==============================================================================Next chapter:usr_05.txt Set your settingsCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: