Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

After selecting a range of lines with the V command, I would like to delete every line of the file that is NOT selected.

Edit: What I would really like to know, is if there is a way to invert the selection -- select every line that is not selected. Like how

:g!/FOO/d`

deletes every line which does NOT contain FOO. The inverse of

'a,'b

would be the two ranges:

1,'a-1 and 'b+1,$

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

4 Answers 4

up vote 2 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
1  
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. vimdoc.sourceforge.net/htmldoc/cmdline.html#cmdline-ranges ,,, vim.wikia.com/wiki/Ranges –  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
6  
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

Just for posterity:

:v/\%V/d

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 http://www.vim.org/scripts/script.php?script_id=953. 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.