Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

After selecting a range of lines with the V command, I would like to delete every line of the file that is NOT selected, so is a way to invert the selection -- select every line that is not selected?

Like how


deletes every line which does NOT contain FOO.

If I had a range in mind and wanted to use its inverse:


I would need to use the two ranges:




I suspect that this cannot be done in one step -- but it would be nice!

share|improve this question
so you want a "crop"-style operation? – quack quixote Nov 19 '09 at 16:10
up vote 4 down vote accepted

First create the following mapping (for example bound to the <F4> key)

map <F4> :<C-U>1,'<-1:delete<CR>:'>+1,$:delete<CR>

then, after selecting a range in visual mode, just press <F4> to trigger the associated command. The command can be easily explained in parts:

  • ":" Enter command line mode.
  • "<C-U>" Remove all characters between the cursor position and the beginning of the line.
  • "1,'<-1" Specifiy the range from the first line of the file to the line before the start of current selection.
  • ":delete<CR>" Delete (the previously specified range of lines).
  • ":'>+1,$:delete<CR>" Delete the lines in the range "'>+1,$", i.e. from the line after the end of the selection to the end of the file.
share|improve this answer
just to explain it: 1,'< - 1 is the range from line 1 upto the start of the selection. '> + 1,$ is the range from 1 line after the selection upto the end... – akira Nov 19 '09 at 17:13
Is there some way to do that with only one "delete" command? – Kevin Panko Nov 19 '09 at 18:34
@Kevin Panko: you'd need to combine the ranges into a single expression. i've looked at the docs and i don't think the range syntax supports such a thing. ,,, – quack quixote Nov 19 '09 at 22:05
  1. select your text
  2. "*yggdG"*p which means ...

    "*y    " yank it to the selection register
    ggdG   " delete everything
    "*p    " and paste the selection again 
share|improve this answer
ygg"_dGP is the same but shorter. – Kevin Panko Nov 19 '09 at 17:56
in the end you get the same text, true :) – akira Nov 19 '09 at 18:20
I would like just refer to akira and Kevin Panko answers (I cannot add comments). " [o]pposite [d]elete in (v)isual mode vmap od ygg"_dGP – Marcin Rogacki Jan 16 '15 at 13:29
Plugins like make this approach easier. You don't have to worry as much about your yank register getting clobbered by the delete. – Jack O'Connor Oct 20 '15 at 15:10

Just for posterity:


That does an inverse global on lines that are not part of the selection. Note that :v is a line-based construct, so any line that contains any of the selection, be it line, stream, or block selection will be spared deletion.

share|improve this answer

since 'inverting' means basically to create 'multiselections' (the area before and after the original selection) i searched the vimscripts again try this.

 There are a number of operations to very
 easily manipulate selections such as modifying 
 the regions (add/delete/inverte/clear), hiding, 
 refreshing, saving and restoring etc."
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .