When working with Latex files, I use Vim to edit the tex file and Okular to view the corresponding pdf. I have pdf-to-tex search (which requires running Vim as a server, and compiling the tex file with Synctex) configured by making vim --remote +%l %f my custom editor command in Okular (Settings 🡢 Configure Okular 🡢 Editor 🡢 Command). This works, but it does not focus Vim, and I remain in Okular.

I would like to focus Vim when I do this search, so I can immediately start editing the tex file. How can I achieve this?

Note: This question is basically the same as this question, except I am using Vim, not gVim. And the only answer posted to that question does not work with Vim.

1 Answer 1


One option is to use xdotool, which is a command-line tool that can interact with windows.

step 1: save Vim's window ID to a file

Run this Vim command after Vim is started as a server:

:call system('xdotool getactivewindow > /tmp/vim_wid.txt')

Explanation: The shell command xdotool getactivewindow will output the active window ID, and the command xdotool getactivewindow > /tmp/vim_wid.txt will save that window ID to the file /tmp/vim_wid.txt (as an example).

step 2: use an xdotool command after your vim --remote-silent command to focus the Vim window

You can set your custom Okular editor command (Settings 🡢 Configure Okular 🡢 Editor 🡢 Command) to be this:

/bin/sh -c "vim --remote +%l %f && cat /tmp/vim_wid.txt | xargs xdotool windowactivate"

Explanation: The shell command xdotool windowactive window_ID will activate the window with ID window_ID. So, if you want to activate the Vim window, you can pipe the window ID from /tmp/vim_wid.txt into that command with cat /tmp/vim_wid.txt | xargs xdotool windowactivate. The /bin/sh -c "command" formatting is necessary for the command to be interpreted correctly.

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.