Skip to content

Instantly share code, notes, and snippets.

@tolyod
Last active March 6, 2020 12:10
Show Gist options
  • Save tolyod/24725b5a07b5879c24905bab749daa99 to your computer and use it in GitHub Desktop.
Save tolyod/24725b5a07b5879c24905bab749daa99 to your computer and use it in GitHub Desktop.
vim-style edit

Deleting Words in Vim

As a recent vim convert, I learn new commands everyday from my fellow rocketeers. Here’s some cool ones for deleting words:

  • Use dw to delete word. Cursor placement is important! If your cursor is not on the first character, it will only delete from your cursor to the end of the word.
  • Use diw to delete inside word. Deletes the entire word that your cursor resides in.
  • Use dt<char> to delete to character. Deletes from your cursor to the specified character.

Quickly change word or line

To quickly change a word you can use cw, caw or ciw. Use c$ or just C to quickly change from the cursor to the end of a line, cc to change an entire line, or cis for a sentence. The standard change word command requires you to type cw, then a new word, then press Escape. Using the version below, you would type pw, then a new word, then press Space to exit back to normal mode. Or, you would type p$, then a new line, then press Enter.

nmap pw :inoremap <lt>Space> <lt>Space><lt>Esc>:iunmap <lt>lt>Space><lt>CR><CR> cw

nmap p$ :inoremap <lt>CR> <lt>CR><lt>Esc>:iunmap <lt>lt>CR><lt>CR><CR> c$

How to Delete Line in VIM on Linux

How to delete lines from Vim? How to delete ranges of lines? How to delete lines by a given pattern? Let's cover in this article different ways to delete lines in Vim editor.

1) Delete a single line

To delete a single line in Vim editor, follow the steps below

  • Place the cursor to the beginning of the line
  • Press the ESC key.
  • Next, press dd i.e quick press letter d twice in quick succession.

In the example below, pressing dd at the beginning of line 6 as shown below will delete the entire line.

2) Delete all lines

Below are the two ways to delete all lines.

:1,$d

or

:%d

3) Delete multiple lines

To delete multiple lines

  • place the cursor at the beginning of a line.
  • Prefix the dd command with the number of lines you want to delete below it. For example, if you want to delete 3 consecutive lines below line 3 press # 3dd

4) Delete a range of lines

If you want to delete a range of lines, say from line 3 to line 5, the syntax is as shown below

:[start_line_no],[end_line_no]d

In this case, Press ESC Then type the command below and hit Enter.

:3,5d

To delete the last line

:$d

To delete all lines before the current line

:1,.-1d

To delete all lines after the current line

:.+1,$d

5) Delete lines by a given pattern

Finally, you can delete lines following a given pattern.

For instance, to delete lines that contain a certain word, press ESC and run

:g /word/d

In our case, to delete lines that contain the word "lazy"

:g /lazy/d

To delete every line that doesn't contain the word "lazy"

:%g!/lazy/d or :v/lazy/d

To delete lines that begin with a certain letter, say 'A'

:g/^A/d

If you want to delete lines that begin with a special character like $ sign, prefix the character with a backslash as shown

:g/^\$/d

To get rid of all blank lines

:g/^$/d

Do you have any tips to delete vim lines? Hope this article helped you and please leave your comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment