0

I would like a warning when a large chunk of data is to be deleted.

I tend to use the mouse along with vim to highlight text (to copy or delete).

I also use the mouse to scroll notes when I want to look around after searching a keyword.

Sometimes the mouse accidentally goes into visual mode—when I scroll up and down, this will causes all the text along the scrolling path to be selected or deselected.

I often don't realize this at the time and end up deleting (somehow) a large chunk of data without even realizing it.

Is there some kind of script I can install that will warn me when deleting in conjunction with visual-mode and/or scrolling?

I believe most of the issue is related to these:

  1. highlighting (be it v or V or mouse left button accidentally dragged when scrolling),
  2. scrolling (be it Ctrl-e, Ctrl-y, or mouse wheel)
  3. text manipulating (be it delete or yank, etc.)
4
  • 2
    What do you mean by "wiped out"? Just press u. And anyways, pressing u is a lot faster than reading a warning and dismissing it, isn't it?
    – B Layer
    Apr 5, 2021 at 10:26
  • no, the problem with me is that accidentally deleted a large chunk of data withtout knowing.. and even closed vim... not realizing the mistake.
    – HoldOn
    Apr 17, 2021 at 19:44
  • I realized lately , a lot of my data has gone missing into thin air.. I believe the main reason is because of mouse highlight. (pls don't deter me from using mouse in vim.. sometime mouse is faster than keyboard alone. ).
    – HoldOn
    Apr 17, 2021 at 19:45
  • I think my data was deleted because of highlighting with mouse. I used to highlight a line of data (for example), then use scroll wheel to scroll down or up to see if more needed to be deleted.. not realizing when i scroll over the screen, all those lines outside (being scrolled over) got highlighted..and will be deleted when "d" is pressed.
    – HoldOn
    Apr 17, 2021 at 19:47

2 Answers 2

1

Here's a small script that does what you want:

function CheckBeforeDelete(cmd)
    let delta = line("'>") - line("'<")
    if delta > 10
        echo "You're about to delete ".delta." lines of data. Continue? (y/n) > "

        let char = ''
        while char != 'n' && char != 'y' && char != "\<ESC>"
            let char = nr2char(getchar())
        endwhile

        norm! :

        if char != 'y'
            return
        endif
    endif

    exe 'norm!gv'.a:cmd
    if index(['c'], tolower(a:cmd)) >= 0
        startinsert
    endif
endfunction

vnoremap d :<C-u>call CheckBeforeDelete('d')<CR>
vnoremap D :<C-u>call CheckBeforeDelete('D')<CR>
vnoremap c :<C-u>call CheckBeforeDelete('c')<CR>
vnoremap C :<C-u>call CheckBeforeDelete('C')<CR>
vnoremap <DEL> :<C-u>call CheckBeforeDelete("\< <BS>DEL>")<CR>
vnoremap x :<C-u>call CheckBeforeDelete('x')<CR>

autocmd WinScrolled * if tolower(mode()[0]) == 'v' | echohl WarningMsg | echo "You are scrolling and selecting text at the same time!" | echohl None | endif

This will prompt you for confirmation before you try to delete more than 10 lines, using visual mode, and the keys d, D, c, C, <del> or x.

It will also display a warning message when you're scrolling and selecting text at the same time.

9
  • Your script is good, thx. P1: But can you make it warn only when the highlighted line SCROLLED over the screen ? P2: there is a tiny bug: the echo msg (you're about to delete 11 lines of datas. continued ? y/n... ) is there (even when i pressed "n" for no); causing the subsequence command key i pressed not show. (this echo msg only vanish when i do another highlight (using mouse).
    – HoldOn
    Apr 17, 2021 at 20:33
  • Your script is good, thx. P1: But can you make it warn only when the highlighted line SCROLLED over the screen ? P2: there is a tiny bug: the echo msg (you're about to delete 11 lines of datas. continued ? y/n... ) is there constantly (if i pressed "n" to say not to delete. Then the status of my subsequence action is no longer showed. for example, when i pressed "dd" to delete/ cut a line of data, it won't show "1 less line" anymore. P3: can you make it i can "abort" my delete action using "esc" button ? Thx
    – HoldOn
    Apr 17, 2021 at 20:41
  • P4: Can you make the "echoed msg" dissapear straight after i entered either "y" or "n" ? so that it won't block the status bar ?
    – HoldOn
    Apr 17, 2021 at 20:49
  • Can you make the "You're about to delete ".delta." lines of data. Continue? (y/n) " msg dissapear after user input Y/N , instead of keep staying there ? Thx.
    – HoldOn
    Apr 18, 2021 at 16:49
  • Is there another way of warning instead of by highlighted line number ? For example, it gives warning when user highlighted line and scroll over the screen with mouse roller wheel (which will auto highlight all lines that being scrolled over the screen, which usually is not the intend of the user to highlight all those lines).
    – HoldOn
    Apr 18, 2021 at 16:52
1

I would like a warning when a large chunk of data is to be deleted

By default Vim should echo the number of lines deleted once you get past 2 full lines. This should help you do you quick sanity check. Also 'showcmd' (on by default) will show you the size of your current selection. Add set showcmd to your vimrc if need be.

Example:

enter image description here

Sometimes the mouse accidentally goes into visual mode—when I scroll up and down, this will causes all the text along the scrolling path to be selected or deselected.

Selected text should look different than normal text. You may want to check your colorscheme and/or terminal to make sure the colors stand out better

I often don't realize this at the time and end up deleting (somehow) a large chunk of data without even realizing it.

If you are using visual mode to delete then you may want to get in the habit of using o to toggle between the start and end of the visual selection. This will help you verify the size of the selection

Ultimately, it sounds like you have developed some troublesome habits. It might be worth it to improve your habits. e.g. avoid relying on the mouse so much, exit visual mode when looking around, set your colorscheme to have more contrast, looking at 'showcmd' to see the size of the your selection

With all that said you can use TextYankPost autocommand event to give yourself a warning. e.g.

augroup DeleteMsg
  autocmd!
  autocmd TextYankPost *
        \ if v:event.operator is# 'd' && v:event.visual && len(v:event.regcontents) > 3 |
        \   echoerr "Big delete!" |
        \ endif
augroup END
1
  • [Quote]Selected text should look different than normal text. You may want to check your colorscheme and/or terminal to make sure the colors stand out better[/Quote] when the lines were scrolled over the screen, those line is no longer visible.. my color scheme for visual highlight is of no problem.. it is the lines that went outside of monitor (due to scrolling with scroll wheel) that is not noticable.
    – HoldOn
    Apr 17, 2021 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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