How to open pdf files under cursor (using 'gf') with external PDF readers in vim
Asked Answered
J

2

7

The current gf command will open *.pdf files as ascii text. I want the pdf file opened with external tools (like okular, foxitreader, etc.). I tried to use autocmd to achieve it like this:

au BufReadCmd *.pdf silent !FoxitReader % & "open file under cursor with FoxitReader
au BufEnter *.pdf <Ctrl-O> "since we do not really open the file, go back to the previous buffer

However, the second autocmd failed to work as expected. I could not figure out a way to execute <Ctrl-o> command in a autocmd way.

Could anyone give me a hint on how to <Ctrl-O> in autocmd, or just directly suggest a better way to open pdf files with gf?

Thanks.

Jessamyn answered 18/10, 2011 at 16:50 Comment(0)
H
8

That's because what follows an autocmd is an ex command (the ones beginning with a colon). To simulate the execution of a normal mode command, use the :normal command. The problem is that you can't pass a <C-O> (and not <Ctrl-O>) directly to :normal, it will be taken as literal characters (<, then C, then r) which is not a very meaningful normal command. You have two options:

1.Insert a literal ^O Character

Use controlvcontrolo to get one:

au BufEnter *.pdf normal! ^O

2.Use :execute to Build Your Command

This way you can get a more readable result with the escaped sequence:

au BufEnter *.pdf exe "normal! \<c-o>"

Anyway, this is not the most appropriate command. <C-O> just jumps to the previous location in the jump list, so your buffer remains opened. I would do something like:

au BufEnter *.pdf bdelete

Instead. Still I have another solution for you.


Create another command with a map, say gO. Then use your PDF reader directly, or a utility like open if you're in MacOS X or Darwin (not sure if other Unix systems have it, and how it's called). It's just like double clicking the icon of the file passed as argument, so it will open your default PDF reader or any other application configured to open any file by default, like images or so.

:nnoremap gO :!open <cfile><CR>

This <cfile> will be expanded to the file under the cursor. So if you want to open the file in Vim, use gf. If you want to open it with the default application, use gO.

If you don't have this command or prefer a PDF-only solution, create a map to your preferred command:

:nnoremap gO :!FoxitReader <cfile> &<CR>
Hak answered 18/10, 2011 at 17:38 Comment(5)
Thanks! Your suggestion with autocmd works. However, when I tried the 'gO' solution with FoxitReader (I did not find equivalent program of open in my Linux distro), the FoxitReader just reported "The file could not be found". It seems the <cfile> had not been expanded correctly. Any further suggestions?Jessamyn
Glad to help @Liang! In the situation where the gO isn't working, does gf work? With <cfile> it is supposed to be the same. It's possible that the file isn't in the current directory you're in, or something like that.Hak
It does related to path issue! However, gf does not suffer from the issue, while gO does. In my case, I had a bibtex file in ~/papers/library.bib, and all pdf files were located in the same directory of papers. When I opened the bib file from $HOME, gf worked and gO failed; When I opened it from papers directory, both solutions worked.Jessamyn
unfortunately the gO solution has issues when working with filenames that contain spaces.Ravo
The window-deleting behavior of :bdelete was unacceptable to me, and I switched to github.com/qpkorr/vim-bufkill (it deletes buffers without closing windows).Dkl
R
0

If the default app is acceptable, then simply using :!open % in command mode works. You can always map this to a suitable leader combination in your vim config file etc.

If you want something that works with normal mode, then you could try something like the following (i use this too for opening HTML files), and modify to your own needs:

if has('win32') || has ('win64') autocmd FileType html nmap <Leader>g :silent ! start firefox "%"<cr> elseif has('mac') autocmd FileType html nmap <Leader>g :!open "%"<cr><cr> endif

Ravo answered 3/7, 2018 at 1:28 Comment(1)
I used :nnoremap gO :!xdg-open <cfile><CR> and it's opening files with the default associated application, but not files with a @ in the path eg ~/[email protected]/paper.pdf It only captures the bit after the @. help <cfile> does not say much. Quotes around <cfile>, like this "<cfile>" don't help. Any ideas?Predikant

© 2022 - 2024 — McMap. All rights reserved.