0

I'm mostly developing with python. I'm using pdbpp as python debugger so I'm contantly adding

import pdb;pdb.set_trace()

or

import pdb
pdb.set_trace()

So I wrote couple of functions.

function! AddBreakPoint()
    let l:line = line('.')
    let l:indentChar = ' '
    call append(l:line - 1, repeat(l:indentChar, indent(l:line)) . "import pdb;pdb.set_trace")
endfunction


function! RemoveAllBreakPoints()
    write
    silent! execute 'bufdo g/\v^\s+(import pdb|pdb.set_trace)/de'
endfunction

The problem with that now is that RemoveAllBreakPoints() messes relative position of cursor on the screen. So for example if the cursor was in the middle of the screen after zz it would be on the same line, but this line suddenly appears at the bottom of the screen.

EDIT1 OK, I solved it with KeepView plugin, but could someone explain to me why vim doing that and is there a native way to keep view after executing command?

1 Answer 1

3

You don't need a plugin for that, the functionality is built into Vim:

function! RemoveAllBreakPoints()
    let view = winsaveview()
    write
    g/\v^\s+(import pdb|pdb.set_trace)/d
    call winrestview(view)
endfunction

Reference:

:help winsaveview()
:help winrestview()
4
  • Oh, that I know. KeepView is just a function that decorates any other function with winsaveview() and winrestview(). Commented Apr 25, 2016 at 7:34
  • So… why ask the question if you already know the answer?
    – romainl
    Commented Apr 25, 2016 at 7:57
  • 1
    I want to know exactly why this is happening, because I don't see any reasonable explanation. Commented Apr 25, 2016 at 8:42
  • 3
    That's the question you added, not the original question. Anyway, ex commands leave the cursor on the last "item" concerned by the command (line, buffer, window, tab page…). That means that you need to somehow record the current position if you want to come back afterward.
    – romainl
    Commented Apr 25, 2016 at 9:19

Your Answer

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

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