[mozlab] Lightweight MozRepl + Vim integration: Refresh Firefox, and restore scroll position on Vim save !

544 views
Skip to first unread message

jcox

unread,
May 13, 2010, 5:33:55 PM5/13/10
to MozLab
I wanted to make Firefox refresh automagically & preserve its scroll
position when I save html files (and CSS) in Vim. That led me to
MozRepl today... and so I created this joyful little hack to reduce
repetitive keystrokes.

Clearly, this integration is no where near as fancy as the Emacs
MozRepl binding, but this one feature alone makes it super useful
to me -- and to you too, I hope. Feel free to improve it & post what
you've done.

Here's the relevant excerpt from my .vimrc

"----------------------------------------------------------------------------------------------------
" Make *.html (and friends) to refresh firefox using MozRepl. on
save
"----------------------------------------------------------------------------------------------------
autocmd BufWriteCmd *.html,*.css,*.gtpl : call Refresh_firefox()

"----------------------------------------------------------------------------------------------------
"
" When editing an .html, .css, or .gtpl template file, make Firefox
" refresh once the buffer is saved, preserving the current scroll
offset.
"
" Requires:
" [2] MozRepl must be installed & running in Firefox
"
" [1] Netcat (nc) must be on your path.
"
" [3] Firefox & vim are both on the same machine (localhost).
" Actually, you can fix that if you want by adjusting
" this script + your MosRepl settings but it's probably
" a bit of a security risk.
"
" Ok, here's how it works:
"
" I use echo to send a dopey little MosRepl script (see below)
" to Firefox. Netcat (nc) has a nice option to disconnect
" after 1 second (-q 1), so I'm using that. All the output
" is tossed away (2>&1 > /dev/null) because MozRepl is chatty.
"
" Global variables are used (vimXo, vimYo) to capture the X and Y
" offset of the web page for vim. Maybe there's a way to not use
" a global, but I don't know that that might be. Anyway, after
saving
" the buffer and reloading the browser, scroll to the proper X,Y
offset.
"
" See also:
" http://wiki.github.com/bard/mozrepl/tutorial
" http://forums.whirlpool.net.au/forum-replies-archive.cfm/495300.html
"
" Aside: Line continuation in vimscript is a bit weird;
" it comes at start of the line you want merged,
" rather than at the end of the line being broken.
"----------------------------------------------------------------------------------------------------
function! Refresh_firefox()
if &modified
write
silent !echo 'vimYo = content.window.pageYOffset;
\ vimXo = content.window.pageXOffset;
\ BrowserReload();
\ content.window.scrollTo(vimXo,vimYo);
\ repl.quit();' |
\ nc -q 1 localhost 4242 2>&1 > /dev/null
endif
endfunction


--
You received this message because you are subscribed to the Google Groups "MozLab" group.
To post to this group, send email to moz...@googlegroups.com.
To unsubscribe from this group, send email to mozlab+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mozlab?hl=en.

esquifit

unread,
May 13, 2010, 6:11:40 PM5/13/10
to moz...@googlegroups.com
I found similar scripts in a couple of Japanese sites dating almost
four years back. [1],[2],[3].
A couple of days ago I slightly adapted this approach to work with
gvim and nc in Windows under cygwin [4].
Preserving the scroll position is a nice addition, thanks!

[1]
最速インターフェース研究会 :: Firefoxの拡張MozLabの中に含まれるMozReplがヤバすぎる件について
http://la.ma.la/blog/diary_200609280045.htm
[2]
Vimでファイル保存時にFirefoxをリロードする(with MozLab) : Serendip - Webデザイン・プログラミング
http://www.serendip.ws/archives/87
[3]
MozreplをWindowsで使う - C0FFEE の日記
http://slashdot.jp/~C0FFEE/journal/474698
[4]
betrachtungen: Commanding Firefox from Vim
http://esquifit.blogspot.com/2010/04/commanding-firefox-from-vim.html

jcox

unread,
Jun 14, 2010, 9:29:38 PM6/14/10
to MozLab

mahima mendu

unread,
Jun 15, 2010, 5:58:20 AM6/15/10
to moz...@googlegroups.com
HI,
 
How are you running MOZREPL code is it in html code or any tool.
 
Thanks,
Mahima

Reply all
Reply to author
Forward
0 new messages