Vim - 设置自定义编辑器以打开 `.md` &“.pdf"文件 [英] Vim - set custom editors to open `.md` & '.pdf' files

查看:60
本文介绍了Vim - 设置自定义编辑器以打开 `.md` &“.pdf"文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

<代码>"确保根据后缀选择查看器.让 g:netrw_browsex_viewer="-";"文件扩展名为.md"的函数.功能!NFH_md(文件名)字体文件名端功能"文件扩展名为.pdf"的函数.功能!NFH_pdf(文件名)zathura 文件名端功能

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

//示例:../../001--documentation/motorola--SREC_format.pdf//示例:./markdown.md

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

不是编辑器命令:zathura 文件名

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

不是编辑器命令:typora 文件名

所以看起来函数是根据文件后缀执行的(这个很好),但是为什么没有打开文件呢?我可能缺少有关如何正确将参数传递给函数的知识?我应该怎么做才能使用编辑器 zathuratypora (两者都安装在我的系统上并且可以从终端运行)成功打开文件?


在@phd 建议之后,我尝试了这个:

<代码>"确保根据后缀选择查看器.让 g:netrw_browsex_viewer="-";"文件扩展名为.md"的函数.功能!NFH_md(文件名):!字体文件名端功能"文件扩展名为.md"的函数.功能!NFH_pdf(文件名):!zathura 文件名端功能

现在打开程序,但它搜索 filename 并且找不到它.所以函数不能正确获取参数...

错误现在是:

错误:未知文件类型:'无法打开`/home/ziga/Dropbox/workspace/racunalnistvo/projects--pistam/2021-01-06--programmer_migration/002--sw/006/filename'(无此文件或目录)'

和:

ENOENT: 没有那个文件或目录,打开'/home/ziga/.../002--sw--006/filename

另外我读到这里 ! 不是异步的,并且!start 仅适用于 Windows.我需要异步的 Linux 解决方案.

解决方案

答案是:

<代码>"确保根据后缀选择查看器.让 g:netrw_browsex_viewer="-";"文件扩展名为.md"的函数.功能!NFH_md(f)执行 '!typora' a:f端功能"文件扩展名为.pdf"的函数.功能!NFH_pdf(f)执行 '!zathura' a:f端功能

如果 execute 没有被使用,那么 a:f 不会被扩展.

<块引用>

注意:

这不是异步解决方案!

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

<代码>"确保根据后缀选择查看器.让 g:netrw_browsex_viewer="-";"文件扩展名为.md"的函数.功能!NFH_md(f)调用 asyncrun#run("", "cwd", "typora" .a:f)端功能"文件扩展名为.pdf"的函数.功能!NFH_pdf(f)调用 asyncrun#run("", "cwd", "evince" .a:f)端功能"文件扩展名为.html"的函数.功能!NFH_html(f)调用 asyncrun#run("", "cwd", "firefox --new-window" .a:f)端功能

<块引用>

待办事项:

有了这个,剩下的烦人的问题就很少了.例如以 .com.orggovphp 结尾的 URI 未按原样打开几乎不可能为每一种可能的情况创建一个处理函数顶级域名!还有另一个问题,因为 URI 指向 PDF 文件不会打开以 #page=100#some_chapter 结尾的内容.类似的是带有忽略#some_chapter"的降价源文件.

这里有一个快速参考有效,什么无效(在 vim 中复制,将鼠标悬停在 URI/文件上,然后按 g+x):

//✔ 示例 (g + x): ./markdown.md//✘ 示例 (g + x): ./markdown.md#test//✔ 示例(g + x):../../001--documentation/motorola--SREC_format.pdf//✘ 示例 (g + x): ../../001--documentation/stm32--boot_mode_and_bootloader.pdf#page=2//✔ 示例(g + x):../../001--documentation/C_--_HTML_documentation/en/index.html//✔ 示例(g + x):https://learnvimscriptthehardway.stevelosh.com/chapters/23.html//✘ 示例(g + x):https://www.google.com//✔ 示例 (g + x):https://www.google.com/index.html//✘ 示例(g + x):https://wiki.archlinux.org/index.php/HiDPI#GNOME//✘ 示例(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//✘ 示例(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

如果有人找到解决这些问题的方法或可以编写vim插件能够打开这些 URI 就太好了!

According to this official guide it is possible to determine your own editors that are called per the filename suffix when in VIM we use command g + x (while cursor is over a file name or URI).

This partially works for me. I used this block of code in the ~/.vimrc file:

" 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

Now I use Vim to open a source file e.g. main.c and navigate to these two line comments:

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

If I hover with the cursor over the 1st one and press g + x I get this error:

Not an editor command:  zathura filename

If I hover with the cursor over the 2nd one and press g + x I get this error:

Not an editor command:  typora filename

So it looks like functions are executed according to a file suffix (this is good), but why isnt a file opened? I am probably missing the knowledge on how to properly pass an argument to the funmctions? What should I do to sucessfuly open the files with editors zathura and typora (both are installed on my system and can be run from the terminal)?


After @phd suggestions I tried this:

" 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

Which now opens the programs but it searches for the filename and can't find it. So function doesn't get the argument properly...

Errors are now:

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)'

and:

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

Also I read here that ! is not asynchronous and !start is a Windows only. I need Linux solution which is asynchronous.

解决方案

The answer is:

" 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

If execute is not used then a:f is not expanded.

Note:

This is not asynchronous solution!

For an asynchronous solution we can install vim plugin asyncrun and change the above code to this:

" 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!

这篇关于Vim - 设置自定义编辑器以打开 `.md` &amp;“.pdf"文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆