2

I'm writing a debugger and when hitting a breakpoint or stepping through code I'm making a system call that instructs a gVim server to jump to the current line of the file being debugged, like this:

system_call("gvim --remote-send +\"%dG" --servername DEBUG --remote-tab-silent %s",
             line_number, file_name);

The problem is each time gVim receives the command the gVim window steals focus and I have to alt-tab or mouse-click back to the debugger window.

How can I prevent the gVim server window stealing focus when it receives at command?

(I've tagged the question with microsoft-windows and linux as I alternate between both operating systems and as such an answer would ideally work on both.)

4
  • Are you looking for :help -f?
    – romainl
    Commented May 21, 2016 at 6:14
  • @romainl I added -f to the call to gVim and it made no difference; it sill steals focus on every call. What were you thinking it would do?
    – x-x
    Commented May 21, 2016 at 8:26
  • 2
    @x-x did you ever solve this? interestingly i have the opposite problem: i cannot make gvim get focus with remote_foreground()
    – RJ-
    Commented Oct 3, 2017 at 15:59
  • @JackeJR No, I never solved it.
    – x-x
    Commented Oct 6, 2017 at 6:01

2 Answers 2

1

A solution to this problem is mentioned in Vim's documentation (:h --remote). The following command should open filename to line_number:

gvim --servername DEBUG --remote-send "<C-\><C-N>:tab drop filename<CR>line_numberG"

Note that this seems to neither raise nor focus Vim. And I believe that opening the file using :tab drop filename should work similar to vim --remote-tab-silent filename. Also, note that I tested this using the i3 and awesome window managers.

:h --remote 
--remote [+{cmd}] {file} ... *--remote* Open the file list in a remote Vim. When there is no Vim server, execute locally. There is one optional init command: +{cmd}. This must be an Ex command that can be followed by "|". The rest of the command line is taken as the file list. Thus any non-file arguments must come before this. You cannot edit stdin this way |--|. The remote Vim is raised. If you don't want this use vim --remote-send "<C-\><C-N>:n filename<CR>"
0

If you are using Linux, here is a bit of a hacky solution. You can augment your gvim command with a shell command that interacts with windows, like xdotool or wmctrl. Here's an example using xdotool:

xdotool getactivewindow > /tmp/gvim_wid.txt && \
gvim --servername DEBUG --remote-tab-silent +line_number file_name && \
gvim --servername DEBUG --remote-expr "system('cat /tmp/gvim_wid.txt | xargs xdotool windowactivate')"

Explanation:

  • line 1: use xdotool to get the window ID of the debugger window, and save it to a file (/tmp/gvim_wid.txt)
  • line 2: run your gvim command
  • line 3: run another gvim command which executes an xdotool command to focus the debugger window (using the window ID from the file)

A similar approach would be to save the window ID to a shell variable instead of a file, and run the final xdotool command directly from the shell. However, I was only able to make that method work by adding a sleep command before the final xdotool command.

Also, I don't know if there is an xdotool alternative in Windows, but I imagine there must be something similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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