有没有办法用 vim 动态检查 html 输出?

发布于 2024-12-04 02:05:51 字数 370 浏览 5 评论 0 原文

当我在 MacVim 中编写 html 时,我会在 Web 浏览器中保持同一文件打开,以便可以检查 html 输出。我不喜欢这种方法的一件事是我经常需要离开 vim,转到浏览器,然后重新加载 html 才能看到更新。 vim 是否有更优雅的解决方案,可以在 vim 中输入时动态更新 html 输出?我记得Textmate好像可以做到这一点?谢谢。

更新: 我找到了我试图记住的视频。您可以在这里找到它:

http://files.fletcherpenney.net/TextMate-Preview.mov

When I write html in MacVim I keep the same file open in a web browser so that I can examine the html output. One of the things I dislike about this method is that I constantly have to leave vim, go to the browser, and reload the html to see the updates. Is there a more elegant solution with vim that will dynamically update html ouput as it is typed in vim? I seem to recall that Textmate could do this? Thanks.

UPDATE:
I found the video I was trying to remember. You can find it here:

http://files.fletcherpenney.net/TextMate-Preview.mov

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花伊自在美 2024-12-11 02:05:51

将其添加到您的 .vimrc 中:

" Refresh WebKit
nnoremap \r :Refresh<CR>

command! Refresh call RefreshWebKit() 

function! RefreshWebKit()
    silent !osascript -e 'tell application "WebKit"'
                    \ -e '  set p_url to URL of current tab of front window'
                    \ -e '  set URL of current tab of front window to p_url'
                    \ -e 'end tell'
endfunction

这将创建一个映射、一个命令和一个函数。我本可以加入他们,但是
这样就更清晰了,我可以在其他地方重用该功能。

该函数调用一点 AppleScript 来再次设置最前面选项卡的 URL,
从而令人耳目一新。这将在 Safari 和 WebKit 下工作,但我不能
保证它可以与其他浏览器一起使用。如果没有,谷歌“刷新{浏览器}
applescript”并使用不同的脚本。

地图只调用命令,命令调用函数。所以你可以编写你的
文件并使用它来刷新浏览器而无需离开 Vim。同样,使用
:Refresh 执行相同的操作。

请注意,您可能想要更改一些内容:

  • 地图本身,我使用 \r,使用您觉得舒服的任何内容。
  • 函数的名称,如果你想避免与其他现有的冲突
    功能
  • 浏览器。这里我使用的是WebKit,而不是Safari。这是每晚构建
    WebKit 本身。使用 Chrome、Firefox 或任何其他名称(带有各自的
    更改)

快速说明:传递给程序的额外 -e 选项只是为了方便
的可读性。按照你想要的方式去做。


还可以查看自动命令(检查 :h autocmd.txt)。这会让你
以更自动化的方式执行此操作,例如:

:autocmd BufWrite *.html Refresh

每次为 .html 文件编写缓冲区时,都会调用 :Refresh 。那里
还有插入文本等事件。

Add this to your .vimrc:

" Refresh WebKit
nnoremap \r :Refresh<CR>

command! Refresh call RefreshWebKit() 

function! RefreshWebKit()
    silent !osascript -e 'tell application "WebKit"'
                    \ -e '  set p_url to URL of current tab of front window'
                    \ -e '  set URL of current tab of front window to p_url'
                    \ -e 'end tell'
endfunction

This will create a map, a command and a function. I could have joined them, but
this way it it's more clear and I can reuse the function in other places.

The function calls a little AppleScript to set the URL of frontmost tab again,
thus refreshing it. This will work under Safari and WebKit, but I can't
guarantee that it works with other browsers. If not, google "refresh {browser}
applescript" and use a different script.

The map just calls the command, which calls the function. So you can write your
file and use it to refresh the browser without leaving Vim. Equally, use
:Refresh to do the same.

Note that you may want to change some things:

  • The map itself, I used \r, use whatever you feel comfortable with.
  • The name of the function, if you want to avoid collisions with other existing
    functions
  • The browser. Here I used WebKit, which is not Safari. It's the nightly build
    of WebKit itself. Use Chrome, Firefox, or any other name (with the respective
    changes)

A quick note: the extra -e options passed to the program are just for the sake
of readability. Do it in the fashion you want.


Take a look also in auto-commands (check :h autocmd.txt). This will let you
do it in a more automatized way like:

:autocmd BufWrite *.html Refresh

This will call :Refresh every time you write a buffer for .html files. There
are also events for inserted text and so on.

泪痕残 2024-12-11 02:05:51

我已经看到了很多方法来做到这一点:

或简单的元标记:

<meta http-equiv="refresh" content="15" />

我喜欢用低技术解决高科技问题,所以我实际上使用元标记,但 sidyll 的解决方案似乎很棒。

I've seen a bunch of ways to do that:

or a simple meta tag:

<meta http-equiv="refresh" content="15" />

I like lo-tech solutions to hi-tech problems so I actually use the meta tag but sidyll's solution seems excellent.

一笔一画续写前缘 2024-12-11 02:05:51

我使用 (osx) kicker ruby​​ gem 它会在文件更改时自动运行:

kicker -e "osascript -e 'tell application \"WebKit\" to do JavaScript \"window.location.reload()\" in first document'"

或者将其粘贴在 .kick 中文件以更好地控制哪些文件应该触发它和/或更多操作:

process do |files|
  unless files.grep(/\.(html|css|js)$/).empty?
    execute("osascript -e 'tell application \"WebKit\"
      do JavaScript \"window.location.reload()\" in first document
    end tell'")
  end
end

I use the (osx) kicker ruby gem which automatically runs on file changes:

kicker -e "osascript -e 'tell application \"WebKit\" to do JavaScript \"window.location.reload()\" in first document'"

Or stick it in a .kick file for more control over which files should trigger it and/or more actions:

process do |files|
  unless files.grep(/\.(html|css|js)$/).empty?
    execute("osascript -e 'tell application \"WebKit\"
      do JavaScript \"window.location.reload()\" in first document
    end tell'")
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文