The Computer Oracle

How to invert the selection in vim?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Darkness Approaches Looping

--

Chapters
00:00 How To Invert The Selection In Vim?
00:40 Accepted Answer Score 7
01:24 Answer 2 Score 16
01:38 Answer 3 Score 1
01:59 Answer 4 Score 11
02:16 Thank you

--

Full question
https://superuser.com/questions/72688/ho...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#vim

#avk47



ANSWER 1

Score 16


  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 
    



ANSWER 2

Score 11


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.




ACCEPTED ANSWER

Score 7


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.



ANSWER 4

Score 1


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."