7

I am trying to do some small html web stuff, and would find it incredibly useful to be able to see live updates of the html file viewed through a browser whenever I save my file. I know there are probably IDEs out there that do this for you, and if you recommend any, please do, but I am looking to make a script that will just open the file in the browser, while closing the version of the file that was already open, so that I can continue to do all my programming in vim.

I know that I can use the following in Mac OSX, to open the file into a new tab of my current browser:

open foo.html

but, if I have this occurring on a timed loop, or every time I write (:w) in vim, my browser will fill up with new tabs. Is there a way to close the old tab when opening the new tab? Or is there an even better approach to this that I have not considered? It would be highly preferred if I could continue to use vim in terminal.

Thanks in advance.

4

4 回答 4

12

您可以使用 AppleScript 重新加载选项卡。请参阅 Benjie对此问题
的回答 Use osascriptto call the AppleScript from shell script。你会得到这样的东西:

osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'

或者,您可以使用类似 next 关闭所有以前的选项卡:

tell application "Google Chrome"
    set windowList to every tab of every window whose URL starts with "http://stackoverflow.com"
    repeat with tabList in windowList
        repeat with thisTab in tabList
            close thisTab
        end repeat
    end repeat
end tell
于 2013-06-15T05:38:42.773 回答
3

如果已经有一个选项卡foo.htmlopen foo.html则应该在 Safari 中聚焦该选项卡。对于 Chrome,您可能会使用如下内容:

set u to "http://t.co/"
tell application "Google Chrome"
    repeat with w in windows
        set i to 0
        repeat with t in tabs of w
            set i to i + 1
            if URL of t is u then
                set active tab index of w to i
                set index of w to 1
                tell t to reload
                activate
                return
            end if
        end repeat
    end repeat
    open location u
    activate
end tell

我刚刚在 TextMateopen "$TM_FILEPATH" -a Safaritext.html范围内分配了 ⌘R。我还启用了在切换到另一个应用程序时保存文档,所以它基本上完成了编辑-保存-切换应用程序-刷新周期的最后三个步骤。

其他选项:

于 2013-06-17T02:44:45.247 回答
0

这对您的应用程序来说可能有点过头了,但我对所有 HTML 项目都使用了 make 文件。我只为自己制作静态网站,但我使用 Less 和 Jade(有时 php 在本地编译成静态页面,你不能动态包含在 jam 中很愚蠢)并且 make 与 vim 集成得非常好。您可以为编译、重新加载、推送到服务器等制定规则。我对浏览器进行实时更新的方式是启动一个 Node 或 python 服务器来使用额外的 javascript 'watcher' 来提供 html,这有很多种类,具体取决于您使用的其他技术。

于 2013-06-15T07:14:35.817 回答
0

你可以给自己一个可以从套接字接收命令的浏览器,比如 uzbl、luakit 或 dwb。

您可以使用扩展名(插件、插件,无论您喜欢的浏览器调用它)添加此功能(侦听套接字,甚至是文件)。

您可以在您正在编辑的页面中插入一些 javascript,这将使它在您可以从 CLI 控制的某些信号时重新加载。

您可以编写一个脚本来杀死浏览器并重新打开它。一些浏览器会尝试加载页面的缓存版本。对于这些,您最好每次使用查询字符串并创建一个唯一的 URI(例如open "foo.html?$(date +%s)")。

可能性是无止境!

于 2013-06-15T19:33:28.457 回答