The root of the problem is the
gv at the end. It is unnecessary since the visual selection persists from the previous part of the command. Worse, it's not harmless because when you do
gv when you're already in Visual mode the current and previous Visual area are exchanged. Meaning the previous selection is reselected!
Even though the previous selection was deleted the selection is applied to the same area...or at least what remains of it. In this case that would be the first of the remaining
line 2. sentences.
To demonstrate, watch what happens when you enter this twice:
2G)hvgggvd. This is exactly what's happening on the command line, minus the write to disk, but with 100% Normal mode commands.
So anyways we just need to lose the
gv and this will work:
:2 | exec 'normal! )hvgg' | exec "'<,'>w! /tmp/part1.txt" | exec 'normal! d'
Beyond that there is a lot of noise in this command that can be cleaned up. Two of the
execs and accompanying quotes aren't necessary:
:2 | exec 'norm! )hvgg' | '<,'>w! /tmp/part1.txt | norm! d
Update: In response to one of your comments this...
is a linewise operation that selects from the first line of the last selected visual area to the last line of the same area. To get character wise ranges you need to use back-tics like this:
However, this won't work with
:w...it's a linewise operation only.
I'd point out that this is outside the scope of your question which asks why particular text is being deleted....so you might want to ask a separate question for characterwise write to file.
Update 2: Here's a totally different approach to your problem...
Create a macro with the following command
:let @q = ")hs\<CR>\<ESC>k:let @a=@a+1\<CR>:1,.w /tmp/part\<C-R>a.txt\<CR>dgg"
- Run this command:
:let @a = 0.
- Put the cursor on a sentence and, in Normal mode, run the macro with
This will behave the way you describe: the sentence and everything above it will be written to a file named
/tmp/part1.txt and the saved text will be deleted. Now move to any other sentence and do
@q again and this time the same thing will happen but it will write to
/tmp/part2.txt. Each time you run the macro the file name will auto-increment. You can reset the index again with
:let @a = 0.
Like the previous update this is not addressing the actual question asked but because I'm nice I'm providing some extra help. (And OP still won't accept my answer.)