How to open pdf files under the cursor (using "gf") using external PDF readers in vim

The current g f command will open * .pdf files as ascii text. I want the pdf file to be opened using external tools (e.g. okular, foxitreader, etc.). I tried using autocmd to achieve it as follows:

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 startup did not work properly. I could not find a way to execute the <Ctrl-o> autocmd in autocmd .

Can someone give me a hint about how to <Ctrl-o> in autocmd , or just suggest a better way to open pdf files with g f ?

Thanks.

+4
source share
1 answer

What follows autocmd is the ex command (those that start with the colon). To simulate the execution of a normal mode command, use :normal . The problem is that you cannot pass <CO> (and not <Ctrl-O> ) directly to :normal , it will be interpreted as literal characters (<, then C, then r), which is not a very significant normal command. You have two options:

1.Paste the literal character ^ O

Use control v control o 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 a shielded sequence:

 au BufEnter *.pdf exe "normal! \<co>" 

In any case, this is not the most suitable team. <CO> just jumps to the previous location in the jump list, so your buffer stays open. I would do something like:

 au BufEnter *.pdf bdelete 

Instead. However, I have another solution for you.


Create another team with a map, say gO . Then use your PDF reader directly or a utility like open if you are on MacOS X or Darwin (not sure if other Unix systems have this and what it's called). It's just like double-clicking the file icon is passed as an argument, so it will open your default PDF reader or any other application configured to open any default file, such as an image or so.

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

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

If you do not have this command, or if you prefer only a PDF solution, create a map your preferred command:

 :nnoremap gO :!FoxitReader <cfile> &<CR> 
+6
source

All Articles