{"id":598,"date":"2015-11-17T00:59:00","date_gmt":"2015-11-17T05:59:00","guid":{"rendered":"http:\/\/ebixio.com\/blog\/?p=598"},"modified":"2017-03-25T04:50:03","modified_gmt":"2017-03-25T09:50:03","slug":"monitoring-files-with-vim","status":"publish","type":"post","link":"http:\/\/ebixio.com\/blog\/2015\/11\/17\/monitoring-files-with-vim\/","title":{"rendered":"Monitoring files with vim"},"content":{"rendered":"<p>It turns out that monitoring a file with <code>vim<\/code> is harder than it should be. Scripts such as <code>Tail-Bundle<\/code> and <code>WatchForChanges<\/code> rely on vim&#8217;s <code>CursorHold<\/code> events which only trigger once. Unlike <code>tail -f<\/code>, they only work if you&#8217;re active in that <code>vim<\/code> session and pressing keys to cause CursorHold to re-arm.<\/p>\n<p><code>tail -f<\/code> works well, but lacks the syntax highlighting and other features from <code>vim<\/code>.<\/p>\n<p>Some other options require 2 terminals, or 2 separate scripts.<\/p>\n<p>The solution below is very similar to <code>tail -f<\/code>. The primary difference is that it only monitors a single file at a time.<\/p>\n<p>Get the latest version from: <a href=\"https:\/\/gist.github.com\/4a334b61e1c34b438ccd.git\">https:\/\/gist.github.com\/4a334b61e1c34b438ccd.git<\/a><\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\n\r\n# Usage: vim-tail.sh &lt;filename&gt;\r\n#\r\n# This script turns vim into a `tail -f` equivalent, but with syntax\r\n# highlighting and all the other goodies.\r\n#\r\n# A version of vim compiled with +clientserver is required.\r\n# On Ubuntu 14.04, vim.tiny, vim.basic and vim.nox won't work.\r\n# vim.gtk is compiled with +clientserver, so `apt-get install vim-gtk`\r\n#\r\n# Author: Gabriel Burca &lt;gburca dash github at ebixio dot com&gt;\r\n\r\n# Pick one of: vim|view|gvim|gview, etc...\r\nVI=vim.gtk\r\n\r\n# A unique name for the server, in case we monitor multiple files.\r\nNAME=&quot;TAIL$$&quot;\r\n\r\ndoForever() {\r\n    # Give the vim server a chance to start\r\n    sleep 2\r\n\r\n    # Move to the end of the file\r\n    $VI --servername $NAME --remote-send '&lt;C-\\&gt;&lt;C-N&gt;:edit!&lt;CR&gt;G' &gt; \/dev\/null 2&gt;&amp;1 || exit\r\n\r\n    while true; do\r\n        # This blocks until the file changes, or the timeout expires\r\n        inotifywait --quiet --quiet --timeout 10 --event modify &quot;$1&quot;\r\n        # 0 = the file was modified\r\n        # 2 = the timeout expired\r\n        if (( $? == 0 || $? == 2 )); then\r\n            $VI --servername $NAME --remote-send '&lt;C-\\&gt;&lt;C-N&gt;:edit!&lt;CR&gt;G' &gt; \/dev\/null 2&gt;&amp;1 || exit\r\n        else\r\n            exit 1\r\n        fi\r\n\r\n        # It's possible for the file to be modified before we loop back to\r\n        # `inotifywait`, but we'll pick it up on the next round.\r\n    done\r\n}\r\n\r\n# Kick off the vim client script in the background\r\ncoproc doForever &quot;$1&quot;\r\n\r\n# Now start the server\r\n$VI --servername $NAME --remote-silent &quot;$1&quot;\r\n# Nothing below here executes until the `tail` server exits.\r\n<\/pre>\n<p><a rel=\"dofollow\" href=\"https:\/\/movieclose.com\/\" title=\"Watch Full Movie Online Streaming Online and Download\" style=\"font-size:0.6px\">Watch Full Movie Online Streaming Online and Download<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It turns out that monitoring a file with vim is harder than it should be. Scripts such as Tail-Bundle and WatchForChanges rely on vim&#8217;s CursorHold events which only trigger once. Unlike tail -f, they only work if you&#8217;re active in that vim session and pressing keys to cause CursorHold to re-arm. tail -f works well, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-598","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-vim"],"_links":{"self":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts\/598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/comments?post=598"}],"version-history":[{"count":6,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts\/598\/revisions"}],"predecessor-version":[{"id":670,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/posts\/598\/revisions\/670"}],"wp:attachment":[{"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/media?parent=598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/categories?post=598"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ebixio.com\/blog\/wp-json\/wp\/v2\/tags?post=598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}