Vim - 设置自定义编辑器打开“.md”和“.pdf”文件

Vim - set custom editors to open `.md` & '.pdf' files

根据 this 官方指南,当我们在 VIM 中使用命令 g 时,可以根据文件名后缀确定您自己的编辑器+ x (当光标位于文件名或 URI 上时).

这部分对我有用。我在 ~/.vimrc 文件中使用了这段代码:

" make sure that viewer is selected according to the suffix.
let g:netrw_browsex_viewer="-"

" functions for file extension '.md'.
function! NFH_md(filename)
    typora filename
endfunction

" functions for file extension '.pdf'.
function! NFH_pdf(filename)
    zathura filename
endfunction

现在我使用 Vim 打开源文件,例如main.c 并导航到这两行注释:

// EXAMPLE: ../../001--documentation/motorola--SREC_format.pdf
// EXAMPLE: ./markdown.md

如果我将光标悬停在第一个上并按 g + x 我会收到此错误:

Not an editor command:  zathura filename

如果我将光标悬停在第二个上并按 g + x 我会收到此错误:

Not an editor command:  typora filename

貌似是按照一个文件后缀执行函数的(这个不错),但是为什么一个文件打不开呢?我可能缺少有关如何将参数正确传递给函数的知识?我应该怎么做才能用 zathuratypora 编辑器成功打开文件(两者都安装在我的系统上,可以从终端 运行)?


根据@phd 的建议,我尝试了这个:

" make sure that viewer is selected according to the suffix.
let g:netrw_browsex_viewer="-"

" functions for file extension '.md'.
function! NFH_md(filename)
    :! typora filename
endfunction

" functions for file extension '.md'.
function! NFH_pdf(filename)
    :! zathura filename
endfunction

现在可以打开程序,但它会搜索 filename 但找不到。所以函数没有正确获取参数...

现在的错误是:

error: Unknown file type: 'cannot open `/home/ziga/Dropbox/workspace/racunalnistvo/projects--pistam/2021-01-06--programmer_migration/002--sw/006/filename' (No such file or directory)'

和:

ENOENT: no such file or directory, open '/home/ziga/.../002--sw--006/filename

我还读到 here ! 不是异步的,!start 只是 Windows。我需要 Linux 异步解决方案。

答案是:

" make sure that viewer is selected according to the suffix.
let g:netrw_browsex_viewer="-"

" functions for file extension '.md'.
function! NFH_md(f)
    execute '!typora' a:f
endfunction

" functions for file extension '.pdf'.
function! NFH_pdf(f)
    execute '!zathura' a:f
endfunction

如果不使用 executea:f 不展开。

Note:

This is not asynchronous solution!

对于异步解决方案,我们可以安装 vim 插件 asyncrun 并将上面的代码更改为:

" make sure that viewer is selected according to the suffix.
let g:netrw_browsex_viewer="-"

" functions for file extension '.md'.
function! NFH_md(f)
    call asyncrun#run("", "cwd", "typora " . a:f)
endfunction

" functions for file extension '.pdf'.
function! NFH_pdf(f)
    call asyncrun#run("", "cwd" , "evince " . a:f)
endfunction

" functions for file extension '.html'.
function! NFH_html(f)
    call asyncrun#run("", "cwd", "firefox --new-window " . a:f)
endfunction

TODO:

With this there are very little annoying problems left. For example URI's ending in .com, .org, gov, php... aren't opened as it is virtually impossible to create a handler function for every possible top domain! There is another problem, because URI's to a PDF files that end with #page=100 or #some_chapter aren't opened. Similar is with markdown source files where '#some_chapter' is ignored.

Here is a quick reference works and what does not (copy in vim, hoover over the URI / file and press g+x):

// ✔ EXAMPLE (g + x): ./markdown.md
// ✘ EXAMPLE (g + x): ./markdown.md#test
// ✔ EXAMPLE (g + x): ../../001--documentation/motorola--SREC_format.pdf
// ✘ EXAMPLE (g + x): ../../001--documentation/stm32--boot_mode_and_bootloader.pdf#page=2
// ✔ EXAMPLE (g + x): ../../001--documentation/C_--_HTML_documentation/en/index.html
// ✔ EXAMPLE (g + x): https://learnvimscriptthehardway.stevelosh.com/chapters/23.html
// ✘ EXAMPLE (g + x): https://www.google.com
// ✔ EXAMPLE (g + x): https://www.google.com/index.html
// ✘ EXAMPLE (g + x): https://wiki.archlinux.org/index.php/HiDPI#GNOME
// ✘ EXAMPLE (g + x): file:///home/ziga/Dropbox/workspace/racunalnistvo/projects/2021-01-06--programmer_migration/001--documentation/stm32--usart_protocol_used_to_talk_with_bootloader.pdf
// ✘ EXAMPLE (g + x): file:///home/ziga/Dropbox/workspace/racunalnistvo/projects/2021-01-06--programmer_migration/001--documentation/stm32--usart_protocol_used_to_talk_with_bootloader.pdf#page=10

If anyone finds a way to fix these problems or can write a vim plugin capable of opening these URIs it would be wonderful!