You should be able to do a search and replace.
:%s/^\(\s*\)#\(\s*pdb.set_trace()\)/\1\2/ To un-comment and
:%s/^\(\s*\)\(pdb.set_trace()\)/\1#\2/ To re-comment.
This is done by using back-references. Anything matched in between memory parentheses \(\) can be recalled by using a back-reference (\1 and \2 in this case).
Edit: Oh, and I kind of forgot that you said you wanted this in a script. For using this in a script just omit the first colon. :)
Here's an example of using it in a function:
function! UncommentSetTrace() %s/^\(\s*\)#\(\s*pdb.set_trace()\)/\1\2/ endfunction function! CommentSetTrace() %s/^\(\s*\)\(pdb.set_trace()\)/\1#\2/ endfunction You should be able to just call this function and it will comment/uncomment all instances in your current file. If you want to use a single keystroke you can map it to the function:
noremap <A-c> :call CommentSetTrace()<CR> This maps Alt+c to comment all the instances.