Skip to content

Instantly share code, notes, and snippets.

@berinhard
Created August 13, 2010 19:30
Show Gist options
  • Save berinhard/523420 to your computer and use it in GitHub Desktop.
Save berinhard/523420 to your computer and use it in GitHub Desktop.
A script to enable easy ipdb usage for Vim users.
" Author: Bernardo Fontes <falecomigo@bernardofontes.net>
" Website: http://www.bernardofontes.net
" This code is based on this one: http://www.cmdln.org/wp-content/uploads/2008/10/python_ipdb.vim
" I worked with refactoring and it simplifies a lot the remove breakpoint feature.
" To use this feature, you just need to copy and paste the content of this file at your .vimrc file! Enjoy!
python << EOF
import vim
import re
ipdb_breakpoint = 'import ipdb; ipdb.set_trace()'
def set_breakpoint():
breakpoint_line = int(vim.eval('line(".")')) - 1
current_line = vim.current.line
white_spaces = re.search('^(\s*)', current_line).group(1)
vim.current.buffer.append(white_spaces + ipdb_breakpoint, breakpoint_line)
vim.command('map <C-I> :py set_breakpoint()<cr>')
def remove_breakpoints():
op = 'g/^.*%s.*/d' % ipdb_breakpoint
vim.command(op)
vim.command('map <C-P> :py remove_breakpoints()<cr>')
EOF
@meain
Copy link

meain commented May 19, 2016

" Toggling rather than two different keys + autosave
" Setting ipdb breakponts
python << EOF
import vim
import re

ipdb_breakpoint = 'import ipdb; ipdb.set_trace()'

def set_breakpoint():
    breakpoint_line = int(vim.eval('line(".")')) - 1

    current_line = vim.current.line
    white_spaces = re.search('^(\s*)', current_line).group(1)

    vim.current.buffer.append(white_spaces + ipdb_breakpoint, breakpoint_line)

def remove_breakpoints():
    op = 'g/^.*%s.*/d' % ipdb_breakpoint
    vim.command(op)

def toggle_breakpoint():
    breakpoint_line = int(vim.eval('line(".")')) - 1
    if 'import ipdb; ipdb.set_trace()' in vim.current.buffer[breakpoint_line]:
        remove_breakpoints()
    elif 'import ipdb; ipdb.set_trace()' in vim.current.buffer[breakpoint_line-1]:
        remove_breakpoints()
    else :
        set_breakpoint()
    vim.command(':w')

vim.command('map <f6> :py toggle_breakpoint()<cr>')
EOF

@D4D3VD4V3
Copy link

Here's a simpler VimL version:

func! s:SetBreakpoint()
    cal append('.', repeat(' ', strlen(matchstr(getline('.'), '^\s*'))) . 'import ipdb; ipdb.set_trace()')
endf

func! s:RemoveBreakpoint()
    exe 'silent! g/^\s*import\sipdb\;\?\n*\s*ipdb.set_trace()/d'
endf

func! s:ToggleBreakpoint()
    if getline('.')=~#'^\s*import\sipdb' | cal s:RemoveBreakpoint() | el | cal s:SetBreakpoint() | en
endf
nnoremap <F6> :call <SID>ToggleBreakpoint()<CR>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment