In vim, I often find myself deleting (or copying) large blocks of text. One can count the lines of text and say (for example) 50dd
to delete 50 lines.
But how would one delete this large block of text without having to know how many lines to delete?
51dd
. Alternatively, use a motion command -- 50d<down arrow>
:set rnu
and Vim will do this instantly! The line you're on will still be numbered absolutely, but lines above and below the cursor will symmetrically be numbered 1, 2, ... (and update with cursor movement, file-level events, etc.)
sed
in linux CLI… see stackoverflow.com/a/6287940/6614155
Go to the starting line and type ma (mark "a"). Then go to the last line and enter d'a (delete to mark "a").
That will delete all lines from the current to the marked one (inclusive). It's also compatible with vi
as well as vim
, on the off chance that your environment is not blessed with the latter.
I'm no vim guru, but what I use in this circumstance is "visual mode". In command mode, type V
(capital). Then move up/down to highlight the block you want deleted (all the usual movement commands work). Then remove it with x
or d
.
.vimrc
is making it act up?
Shift
-v
... I was trying to use the CTRL
key as well. So this works and is probably the most convenient way.
CTRL
-v
) and Visual Block (Shift
-v
).
You can use the visual mode also (some commands are usable with the delete option also) vip vap to select paragraph, v2ap to select two paragraphs dap works, d2ap also. You can delete within blocks of [
]
like da[
For reference: the types of objects. From vim documentation : section 4. http://vimdoc.sourceforge.net/htmldoc/visual.html
4. Operating on the Visual area *visual-operators*
...
The objects that can be used are:
aw a word (with white space)
iw inner word
aW a WORD (with white space)
iW inner WORD
as a sentence (with white space)
is inner sentence
ap a paragraph (with white space)
ip inner paragraph
ab a () block (with parenthesis)
ib inner () block
aB a {} block (with braces)
iB inner {} block
a< a <> block (with <>)
i< inner <> block
a[ a [] block (with [])
i[ inner [] block
dap
is very easy to remember as "delete a paragraph"
There are many better answers here, but for completeness I will mention the method I used to use before reading some of the great answers mentioned above.
Suppose you want to delete from lines 24-39. You can use the ex command
:24,39d
You can also yank lines using
:24,39y
And find and replace just over lines 24-39 using
:24,39s/find/replace/g
It sort of depends on what that large block is. Maybe you just mean to delete a paragraph in which case a dip would do.
If you turn on line numbers via set number
you can simply dNNG
which will delete to line NN from the current position. So you can navigate to the start of the line you wish to delete and simply d50G
assuming that is the last line you wish to delete.
There are several possibilities, what's best depends on the text you work on.
Two possibilities come to mind:
switch to visual mode (V, S-V, ...), select the text with cursor movement and press d
delete a whole paragraph with: dap
If the entire block is visible on the screen, you can use relativenumber setting. See :help relativenumber. Available in 7.3
Counting lines is too tedious for me, but counting 'paragraphs' isn't so bad. '{' and '}' move the cursor to the first empty line before and after the cursor, respectively. Cursor moving operations can be combined with deletion, and several other answers used a similar approach (dd for a line, dG for the end of the document, etc.) For example:
/* Lorem ipsum dolor sit amet, consectetur adipiscing elit. */
Lorem *ipsum(void) {
return dolor(sit, amet);
}
If your cursor starts above the comment block, 'd}' deletes the comment block, and 'd2}' deletes both the comment block and the code block. If your cursor starts below the code block, 'd{' deletes the code, and 'd2{' deletes both. Of course, you can skip over one block by moving the cursor first: '{d{' or '}d}'. If you're consistent with your whitespace, or you can count the paragraphs at a glance, this should work. The Vim help file has more cursor tricks if you're interested.
You could place your cursor at the beginning or end of the block and enter visual mode (shift-v). Then simply move up or down until the desired block is highlighted. Finally, copy the text by pressing y or cut the text by pressing d.
Alongside with other motions that are already mentioned here, there is also /{pattern}<CR>
motion, so if you know that you want to delete to line that contains foo, you could do dV/foo<CR>
. V
is here to force motion be line-wise because by default /
is characterwise.
You can also enter a very large number, and then press dd
if you wish to delete all the lines below the cursor.
dG
, which deletes to the end of the document, as G
defaults to going to the last line.
Deleting a block of text
Assuming your cursor sits at the beginning of the block:
V/^$<CR>d (where <CR> is the enter/return key)
Explanation
Enter "linewise-visual" mode: V
Highlight until the next empty line: /^$
Delete: d
Key binding
A more robust solution:
:set nowrapscan
:nnoremap D V/^\s*$\\|\%$<CR>d
Explanation
Disable search wrap: :set nowrapscan
Remap the D key (to the following commands): :nnoremap D
Enter "linewise-visual" mode: V
Highlight until the next empty/whitespace line or EOF: /^\s*$\\|\%$
Delete: d
Success story sharing
/var/mail/username
in bulk, and leaving the last mails intact. I do a mark, then I enter for example50000
followed bygg
to go to line 50.000, then I delete all lines in-between. works like a charm!vim
as a tool for editing gigabyte files - I'd tend to opt for text processing tools such asawk
and its brethren :-)