Skip to content
Home » Master the art of text manipulation in Vim

Master the art of text manipulation in Vim

One of the key skills to mastering Vim is understanding how to manipulate text. In this tutorial, we will cover several techniques for deleting and modifying text in Vim, using the domain “itvraag.nl” as an example.

Deleting text

To delete text in Vim, you can use the d command followed by a movement command. For example, to delete a single character, you can use x as the movement command:

itvraag.nl
 ^

To delete the i character, you can use the following command:

dxi
itvraag.nl
  ^

To delete a word, you can use the w movement command:

dwi
tvraag.nl
  ^

To delete a line, you can use the dd command:

dd

tvraag.nl
  ^

Modifying text

To modify text in Vim, you can use the c command followed by a movement command. For example, to change a single character, you can use x as the movement command:

itvraag.nl
 ^

To change the i character to I, you can use the following command:

cxi
Itvraag.nl
  ^

To change a word, you can use the w movement command:

cwi
ItVraag.nl
  ^

To change a line, you can use the cc command:

cc
ItVraag.nl
  ^

Bonus tips

# Delete the current line
:delete
# Delete the current word
:normal dw
# Replace every occurrence of "old" with "new" in the entire file
:%s/old/new/g
# Replace every occurrence of "old" with "new" in the current line
:s/old/new/g
# Copy the current line
:yank
# Cut the current line
:cut
# Paste the contents of the default register after the cursor
:put
# undo the last command
:undo
# Redo the last undone command
:redo
# Delete the current line and paste it at the end of the file
:delete | put =
# Delete the current word, then insert it in uppercase
:normal dw | normal p | normal gUiw
# Search and replace every occurrence of "old" with "new" in the entire file, but ask for confirmation before replacing
:%s/old/new/gc
# Replace every occurrence of "old" with "new" in the current line, but only for the word under the cursor
:s/\<old\>/new/g
# Copy the current line, then paste it 10 lines below the cursor
:yank | normal 10j | put
# Cut the current line and paste it after the next blank line
:delete | /^$ | normal j | put
# undo the last 3 commands
:undo 3
# Redo the last 3 undone commands
:redo 3

Most important thing to keep in mind

The most important thing to keep in mind when deleting and modifying text in Vim is to use the right movement command. This will allow you to delete or change exactly the text that you want.

5 ways to apply these techniques to increase productivity and efficiency

  1. Use the . command to quickly repeat delete or change operations on multiple occurrences of a word or phrase.
  2. Use the w movement command to delete or change entire words, rather than deleting or changing characters one at a time.
  3. Use the dd command to delete entire lines of text quickly and efficiently.
  4. Use the c command to make quick changes to text without having to delete and re-type it.
  5. Practice using these commands in Vim to develop your muscle memory and speed up your workflow.

Leave a Reply

Your email address will not be published. Required fields are marked *

15 − four =