0

For setting breakpoints, I was wondering if there is any way to copy the location of a line in a file opened in vim, to gdb rather than typing the whole file path and linenumber in the gdb shell?

  • 1
    If you're using tmux I can give you an easy answer. – B Layer May 5 at 23:09
  • Yes I am using tmux @B Layer. – In78 May 5 at 23:34
  • Cool. Writing an answer now. – B Layer May 5 at 23:37
1

Good, since you're using tmux then you can use send-keys (alias: send), a tmux command for inputting text into a different pane than the current one. You can call it from Vim using whatever data you need from there. For current (full) path and line number that would be expand('%:p') and line('.')

The following command will send those two strings (separated by :) to the terminal in pane 3. The combined string will not be submitted (i.e. carriage return is not sent).

:exe "!tmux send -t 3 '" . expand("%:p") . ":" . line(".") . "'"

So with, for example, a file /tmp/foo.txt and line number 33 the tmux command that will be executed from the shell is:

tmux send -t 3 '/tmp/foo.txt:33'

If you want to submit the sent string in the target pane (i.e. by sending a carriage return) you just need to replace the last "'" with "' Enter".

Regarding the -t param to send-keys:

  • In its simplest form, takes the target pane index as argument.
  • If you are sending your command to another pane in the same window that's the only form we need to know. (Otherwise read the man page and/or ask me.)
  • You can display pane indexes in the current window with tmux-prefix + q.

Obviously, if you're going to use this a lot you'll want to put it in a mapping and/or function. A mapping-only solution:

nnoremap XX :<C-U>exe "!tmux send -t " . v:count . " '" . expand("%:p") . " " . line(".") . "'"<CR>

Replace XX with whatever unused key or key combo you want. Determine the target pane index (used by -t) and, from Normal mode, type that number and then your XX replacement.

Though a little hard to read, the RHS of this isn't much more than the original exe command plus:

  • Insertion of the variable v:count where the -t value goes. v:count is a built-in variable that contains any number entered right before entering command-line mode (here that means the number entered before the the mapped key(s) are pressed).
  • <C-U> triggers Ctrl-U in order to clear the command line before we populate it. (Because when a number is entered right before entering command-line mode (:) the command-line is prepopulated with a line range and we don't want that here.)

Alternatively, you could prompt the user for a pane index with a function+mapping. Something like:

func! BufferInfoToPane()
    let l:pidx = ...prompt code here...
    exe "!tmux send -t ".l:pidx." '".expand("%:p")." ".line(".")."'"<CR>
endfunc

With the mapping:

nnoremap XX :call BufferInfoToPane()<CR>
  • How to create a mapping for this? Also I have one more question, while making a mapping how can I make sure that I am not overwriting any existing mapping? – In78 May 6 at 0:05
  • 1
    Added a mapping. As for overwriting mappings you need to check manually whether something is mapped. :nmap <leader>X for normal mode mapping using "\X", for example (assuming default <leader> key). – B Layer May 6 at 0:13
  • 1
    @ln78 Does it work for you? – B Layer May 7 at 19:59
  • 1
    I'll take that as a yes. :) – B Layer May 8 at 2:59
0

I have found one way which is to use the : terminal feature of vim to start a new shell, run gdb in it and paste the filename from the alternate register #.

Your Answer

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

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