3

Doing l and l - all the time is inconvenient, and misses feature like syntax highlight and jumping to definitions.

How to I run an external command that opens an editor (possibly a CLI editor like Vim) on the current file, at the current line?

I already know about editor plugins integrations as mentioned at: https://vi.stackexchange.com/questions/2046/how-can-i-integrate-gdb-with-vim or Eclipse, but opening the editor from GDB has the advantage that it should work with any editor, and is more lightweight, and therefore less likely to break. The cost is worse integration, e.g. can't set breakpoints from editor, but I'm fine with that.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985

2 Answers2

6

The edit command, with no arguments, will do what you want.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
3

Method that automatically centers lines

This Python GDB script is like edit, but it also centers Vim on the current line:

class Vim(gdb.Command):
    """
Open current file in vim at a the current line.
"""
    def __init__(self):
        super().__init__('vim', gdb.COMMAND_FILES)
    def invoke(self, argument, from_tty):
        sal = gdb.selected_frame().find_sal()
        call(['vim', sal.symtab.fullname(), '+{}'.format(sal.line), '+normal! zz'])
Vim()

The Vim CLI arguments are explained at: How can I open vim with a particular line number at the top?

The sal GDB Python object is documented at: https://sourceware.org/gdb/onlinedocs/gdb/Symbol-Tables-In-Python.html#Symbol-Tables-In-Python

Unfortunately I could not use vim --remote properly because I could not pass the current line: How to send commands to gvim when using --remote?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 1
    Just wanted to note some extensions to this that can be done, you can use vim-remote instead of invoking a new vim session every time, see also gdb PR 13598, for a mechanism to make the editor follow the current line/file. – matt Apr 22 '17 at 18:52
  • @matt I had looked into `--remote`, but I couldn't add the line number with that option :-( http://stackoverflow.com/questions/10311254/how-to-send-commands-to-gvim-when-using-remote let me know if you find a way. Added that to the answer as well. – Ciro Santilli OurBigBook.com Apr 22 '17 at 19:59
  • I think you need 2 invocations of vim remote: vim --servername GDB --remote "foo.c" (to open the file) followed by: vim --servername GDB --remote-send ":5 " with 5 ~= sal.line – matt Apr 22 '17 at 20:47