I started debugging a c program inside vim with:

:packadd termdebug

with vim 8 and and gdb > 8.3 installed.

In order to set a breakpoint, I would like to send the following string while the cursor is in file main.c (by typing <leader>b):

break fileName:lineNumber

so that the gdb terminal receives:

break main.c:100

if the file name is main.c and the current line for this example is 100 and represented with let lineNumber = line('.')

Here is what I've come up with until now:

nnoremap <leader>b : call SetBreakPoint()<CR>
function! SetBreakPoint() abort
    let lineNumber = line('.')
    let fileName = %f
    exe "normal! ma"
    exe "normal! \<C-w>w\<C-w>w" | call term_sendkeys('', "break fileName\:lineNumber\<CR>")
    exe "normal! \<C-w>w`a"

The line exe "normal! \<C-w>w\<C-w>w | call term_sendkeys()..." takes into account that the cursor is in a file named main.c and has to traverse Nerdtree to arrive to the Terminal window opened by vim with gdb. I have in total 3 vim windows (main.c, Nerdtree and Terminal (gdb)).

The problem here is that I cannot make term_sendkeys() load the values of fileName and lineNumber.

However, changing the longest line of code to

exe "normal! \<C-w>w\<C-w>w" | call term_sendkeys('', "break main.c:100\<CR>")

works but defeats the whole purpose of defining break points inside the vim main.c window without having to manually change window and type in the break command for gdb.


  • 1
    call term_sendkeys('', "break " . fileName . ':' . lineNumber . "\<CR>") or something like that.
    – Matt
    Oct 23, 2019 at 7:21
  • Why not use :h :Break ? If you really need to do some customization, consider TermDebugSendCommand.
    – dedowsdi
    Oct 23, 2019 at 7:26
  • @Matt I cannot dereference the value of fileName inside your command, how do you output the value of fileName and lineNumber inside term_sendkeys?
    – tham mes
    Oct 23, 2019 at 22:07


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.