*change.txt* For Vim version 5.6. Last change: 1999 Dec 21 VIM REFERENCE MANUAL by Bram Moolenaar This file describes commands that delete or change text. In this context, changing text means deleting the text and replacing it with other text using one command. You can undo all of these commands. You can repeat the non-Ex commands with the "." command. 1. Deleting text |deleting| 2. Delete and insert |delete-insert| 3. Simple changes |simple-change| *changing* 4. Complex changes |complex-change| 4.1 Filter commands |filter| 4.2 Substitute |:substitute| 5. Copying and moving text |copy-move| For inserting text see |insert.txt|. ============================================================================== 1. Deleting text *deleting* ["x] or ** *x* *dl* ["x]x Delete [count] characters under and after the cursor [into register x] (not linewise). Does the same as "dl". See |:fixdel| if the key does not do what you want. See |'whichwrap'| for deleting the (join lines). {Vi does not support } *X* *dh* ["x]X Delete [count] characters before the cursor [into register x] (not linewise). Does the same as "dh". Also see |'whichwrap'|. *d* ["x]d{motion} Delete text that {motion} moves over [into register x]. See below for exceptions. *dd* ["x]dd Delete [count] lines [into register x] (linewise). *D* ["x]D Delete the characters under the cursor until the end of the line and [count]-1 more lines [into register x]; synonym for d$ (not linewise). These commands delete text. You can repeat them with the "." command (except ":d") and undo them. Use Visual mode to delete blocks of text. See |registers| for an explanation of registers. An exception for the d{motion} command: If the motion is not linewise, the start and end of the motion are not in the same line, and there are only blanks before the start and after the end of the motion, the delete becomes linewise. This means that the delete also removes the line of blanks that you might expect to remain. Trying to delete an empty region of text (e.g., "d0" in the first column) is an error when 'cpoptions' includes the 'E' flag. *J* J Join [count] lines, with a minimum of two lines. Remove the indent and insert up to two spaces (see below). These commands delete the between lines. This has the effect of joining multiple lines into one line. You can repeat these commands (except ":j") and undo them. These commands, except "gJ", insert one space in place of the unless there is trailing white space or the next line starts with a ')'. These commands, except "gJ", delete any leading white space on the next line. If the 'joinspaces' option is on, these commands insert two spaces after a '.', '!' or '?' (but if 'cpoptions' includes the 'j' flag, they insert two spaces only after a '.'). ============================================================================== 2. Delete and insert *delete-insert* *replacing* *R* R Enter Replace mode: Each character you type replaces an existing character, starting with the character under the cursor. Repeat the entered text [count]-1 times. See |Replace-mode| for more details. *c* ["x]c{motion} Delete {motion} text [into register x] and start insert. When 'cpoptions' includes the 'E' flag and there is no text to delete (e.g., with "cTx" when the cursor is just after an 'x'), an error occurs and insert mode does not start (this is Vi compatible). When 'cpoptions' does not include the 'E' flag, the "c" command always starts insert mode, even if there is no text to delete. *cc* ["x]cc Delete [count] lines [into register x] and start insert (linewise). If 'autoindent' is on, preserve the indent of the first line. *C* ["x]C Delete from the cursor position to the end of the line and [count]-1 more lines [into register x], and start insert. Synonym for c$ (not linewise). *s* ["x]s Delete [count] characters [into register x] and start insert (s stands for Substitute). Synonym for "cl" (not linewise). *S* ["x]S Delete [count] lines [into register x] and start insert. Synonym for "cc" (linewise). Notes: - You can end Insert and Replace mode with . - See the section "Insert and Replace mode" |mode-ins-repl| for the other special characters in these modes. - The effect of [count] takes place after Vim exits Insert or Replace mode. - When the 'cpoptions' option contains '$' and the change is within one line, Vim continues to show the text to be deleted and puts a '$' at the last deleted character. See |registers| for an explanation of registers. Replace mode is just like Insert mode, except that every character you enter deletes one character. If you reach the end of a line, Vim appends any further characters (just like Insert mode). In Replace mode, the backspace key restores the original text (if there was any). (See section "Insert and Replace mode" |mode-ins-repl|). *cw* *cW* Special case: "cw" and "cW" work the same as "ce" and "cE" if the cursor is on a non-blank. This is because Vim interprets "cw" as change-word, and a word does not include the following white space. {Vi: "cw" when on a blank followed by other blanks changes only the first blank; this is probably a bug, because "dw" deletes all the blanks} ============================================================================== 3. Simple changes *simple-change* *r* r{char} Replace the character under the cursor with {char}. If {char} is a or , a line break replaces the character. To replace with a real , use CTRL-V . CTRL-V replaces with a . {Vi: CTRL-V still replaces with a line break, cannot replace something with a } If you give a [count], Vim replaces [count] characters with [count] {char}s. When {char} is a or , however, Vim inserts only one : "5r" replaces five characters with a single line break. When {char} is a or , Vim performs autoindenting. This works just like deleting the characters that are replaced and then doing "i". *case* *locale* *$LANG* The following commands change the case of letters. The currently active locale is used. This depends on your system On Unix, setting the $LANG environment variable can make a difference. For example, to enable changing the case of letters with umlauts: > setenv LANG de *~* ~ 'notildeop' option: Switch case of the character under the cursor and move the cursor to the right. If a [count] is given, do that many characters. {Vi: no count} ~{motion} 'tildeop' option: switch case of {motion} text. {Vi: tilde cannot be used as an operator} SHIFTING LINES LEFT OR RIGHT *shift-left-right* *<* <{motion} Shift {motion} lines one 'shiftwidth' leftwards. *<<* << Shift [count] lines one 'shiftwidth' leftwards. *>* >{motion} Shift {motion} lines one 'shiftwidth' rightwards. *>>* >> Shift [count] lines one 'shiftwidth' rightwards. The ">" and "<" commands are handy for changing the indentation within programs. Use the 'shiftwidth' option to set the size of the white space which these commands insert or delete. Normally the 'shiftwidth' option is 8, but you can set it to, say, 3 to make smaller indents. The shift leftwards stops when there is no indent. The shift right does not affect empty lines. If the 'shiftround' option is on, the indent is rounded to a multiple of 'shiftwidth'. When the 'expandtab' option is off (this is the default) Vim uses s as much as possible to make the indent. You can use ">><<" to replace an indent made out of spaces with the same indent made out of s (and a few spaces if necessary). If the 'expandtab' option is on, Vim uses only spaces. Then you can use ">><<" to replace s in the indent by spaces (or use ":retab!"). ============================================================================== 4. Complex changes *complex-change* 4.1 Filter commands *filter* A filter is a program that accepts text at standard input, changes it in some way, and sends it to standard output. You can use the commands below to send some text through a filter, so that it is replace by the filter output. Examples of filters are "sort", which sorts lines alphabetically, and "indent", which formats C program files (you need a version of indent that works like a filter; not all versions do). The 'shell' option specifies the shell Vim uses to execute the filter command (See also the 'shelltype' option). You can repeat filter commands with ".". Vim does not recognize a comment (starting with '"') after the ":!" command. See the end of the Colon Commands reference doc for a list of available filename-modifiers. *!* *filter* !{motion}{filter} Filter {motion} text through the external program {filter}. *!!* !!{filter} Filter [count] lines through the external program {filter}. *v_!* {Visual}!{filter} Filter the highlighted lines through the external program {filter} (for {Visual} see |Visual-mode|). {not in Vi} :{range}![!]{filter} [!][arg] *:range!* Filter {range} lines through the external program {filter}. Vim replaces the optional bangs with the latest given command and appends the optional [arg]. Vim saves the output of the filter command in a temporary file and then reads the file into the buffer. *=* ={motion} Filter {motion} lines through the external program given with the 'equalprg' option. When the 'equalprg' option is empty (this is the default), use the internal formatting function to set the indent of each line |C-indenting|. *==* == Filter [count] lines through the external program given with the 'equalprg' option. When the 'equalprg' option is empty (this is the default), use the internal formatting function |C-indenting|. *v_=* {Visual}= Filter the highlighted lines through the external program given with the 'equalprg' option. When the 'equalprg' option is empty (this is the default), use the internal formatting function |C-indenting|. (for {Visual} see |Visual-mode|). {not in Vi} 4.2 Substitute *:substitute* *:s* *:su* *:substitute* :[range]s[ubstitute]/{pattern}/{string}/[c][e][g][p][r][i][I] [count] For each line in [range] replace a match of {pattern} with {string}. See |:s_flags| for the flags. *:s_flags* The arguments that you can use for the substitute commands: [c] Confirm each substitution. Vim positions the cursor on the matching string. You can type: *:s_c* 'y' to substitute this match 'l' to substitute this match and then quit ("last") 'n' to skip this match to skip this match 'a' to substitute this and all remaining matches {not in Vi} 'q' to quit substituting {not in Vi} CTRL-E to scroll the screen up {not in Vi} CTRL-Y to scroll the screen down {not in Vi}. [g] Replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line. If the 'edcompatible' option is on, Vim remembers this flag and toggles it each time you use it, but resets it when you give a new search pattern. If the 'gdefault' option is on, this flag is on by default and the [g] argument switches it off. [p] Print the line containing the last substitute. If the {pattern} for the substitute command is empty, the command uses the pattern from the last substitute or ":global" command. For compatibility with Vi these two exceptions are allowed: "\/{string}/" and "\?{string}?" do the same as "//{string}/r". "\&{string}&" does the same as "//{string}/". Instead of the '/' which surrounds the pattern and replacement string, you can use any other character, but not an alphanumeric character, '\', '"' or '|'. This is useful if you want to include a '/' in the search pattern or replacement string. Example: > :s+/+//+ *sub-replace-special* Some characters in {string} have a special meaning: magic action ~ & replaced with the whole matched pattern \& replaced with & \0 replaced with the whole matched pattern \1 replaced with the matched pattern in the first pair of () \2 replaced with the matched pattern in the second pair of () .. .. \9 replaced with the matched pattern in the ninth pair of () ============================================================================== 5. Copying and moving text *copy-move* *quote* "{a-zA-Z0-9.%#:-"} Use register {a-zA-Z0-9.%#:-"} for next delete, yank or put (use uppercase character to append with delete and yank) ({.%#:} only work with put). *y* *yank* ["x]y{motion} Yank {motion} text [into register x]. When no characters are to be yanked (e.g., "y0" in column 1), this is an error when 'cpoptions' includes the 'E' flag. *yy* ["x]yy Yank [count] lines [into register x] (linewise). *Y* ["x]Y yank [count] lines [into register x] (synonym for yy, linewise). If you like "Y" to work from the cursor to the end of line (which is more logical, but not Vi-compatible) use ":map Y y$". *p* *put* ["x]p Put the text [from register x] after the cursor [count] times. {Vi: no count} ["x]P or *P* ** ["x] Put the text [from register x] before the cursor [count] times. Using the mouse only works when 'mouse' contains 'n' or 'a'. {Vi: no count} You can use these commands to copy text from one place to another. Do this by first getting the text into a register with a yank, delete or change command, then inserting the register contents with a put command. You can also use these commands to move text from one file to another, because Vim preserves all registers when changing buffers (the CTRL-^ command is a quick way to toggle between two files). *linewise-register* *characterwise-register* You can repeat the put commands with "." (except for :put) and undo them. If the command that was used to get the text into the register was linewise, Vim inserts the text below ("p") or above ("P") the line where the cursor is. Otherwise Vim inserts the text after ("p") or before ("P") the cursor. With the ":put" command, Vim always inserts the text in the next line. You can exchange two characters with the command sequence "xp". You can exchange two lines with the command sequence "ddp". You can exchange two words with the command sequence "deep" (start with the cursor in the blank space before the first word). You can use the "']" or "`]" command after the put command to move the cursor to the end of the inserted text, or use "'[" or "`[" to move the cursor to the start. Note that after a yank command, Vim leaves the cursor on the first yanked character that is closest to the start of the buffer. This means that "yl" doesn't move the cursor, but "yh" moves the cursor one character left. Rationale: In Vi the "y" command followed by a backwards motion would sometimes not move the cursor to the first yanked character, because redisplaying was skipped. In Vim it always moves to the first character, as specified by Posix. There are nine types of registers: *registers* 1. The unnamed register "" 2. 10 numbered registers "0 to "9 4. 26 named registers "a to "z or "A to "Z 7. The selection register "* 8. The black hole register "_ 1. Unnamed register "" *quote_quote* *quotequote* Vim fills this register with text deleted with the "d", "c", "s", "x" commands or copied with the yank "y" command, regardless of whether or not a specific register was used (e.g. "xdd). An exception is the '_' register: "_dd does not store the deleted text in any register. Vim uses the contents of this register for any put command (p or P) which does not specify a register. Additionally you can access it with the name '"'. This means you have to type two double quotes. {Vi: register contents are lost when changing files, no '"'} 2. Numbered registers "0 to "9 *quote_number* *quote0* *quote1* *quote2* *quote3* *quote4* *quote9* Vim fills these registers with text from yank and delete commands. Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with ["x]. Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (Vim puts text deleted with "x" or "dw" in the small delete register). With each successive deletion or change, Vim shifts the previous contents of register 1 into register 2, 2 into 3, and so forth, losing the previous contents of register 9. {Vi: numbered register contents are lost when changing files; register 0 does not exist} 4. Named registers "a to "z or "A to "Z *quote_alpha* *quotea* Vim fills these registers only when you say so. Specify them as lowercase letters to replace their previous contents or as uppercase letters to append to their previous contents. 7. Selection register "* Use this register for storing and retrieving the selected text for the GUI. See |quotestar|. When the clipboard is not available or not working, the unnamed register is used instead. {not in Vi} 8. Black hole register "_ *quote_* When writing to this register, nothing happens. This can be used to delete text without affecting the normal registers. When reading from this register, nothing is returned. {not in Vi} If you use a put command without specifying a register, Vim uses the register that was last filled (this is also the contents of the unnamed register). If you are confused, use the ":dis" command to find out what Vim will put (this command displays all named and numbered registers; the unnamed register is labelled '"'). vim:tw=78:ts=8:sw=8: