The Accidental Rubyist

invalid byte sequence in UTF-8

Archive for the ‘vim’ Category

Auto-update timestamp in files upon Save

with one comment

If you use vim for editing files, you can stick this in your vimrc file, so whenever you save the file, "Last update" gets updated with the current time. If you enhance this script, pls let me know, so we can all benefit.

This requirement is already met by timestamp.vim, but that’s a biggish plugin, and does a lot of other updates, and I just wanted a few lines in my vimrc, and only timestamp updated.

" auto-update last update if there's a update tag
autocmd! BufWritePre * :call s:timestamp()
" to update timestamp when saving if its in the first few lines of a file
function! s:timestamp()
let pat = '\(Last update\s*:\s*\).*'
let rep = '\1' . strftime("%Y-%m-%d %H:%M")
call s:subst(1, 5, pat, rep)
" subst taken from timestamp.vim
" {{{1 subst( start, end, pat, rep): substitute on range start - end.
function! s:subst(start, end, pat, rep)
let lineno = a:start
while lineno <= a:end
let curline = getline(lineno)
if match(curline, a:pat) != -1
let newline = substitute( curline, a:pat, a:rep, '' )
if( newline != curline )
" Only substitute if we made a change
"silent! undojoin
keepjumps call setline(lineno, newline)
let lineno = lineno + 1

" }}}1

Written by totalrecall

November 29, 2012 at 10:30 am

Posted in vim

Vim function for markdown maruku users

leave a comment »

For vim users who use markdown or maruku and need a little help surrounding words.
This function goes into your vimrc.
I’ve mapped it to “,m” in visual mode.

Select a word or line or block, press ,m
You are prompted for a char or chars. These chars are placed on both sides of the visually
marked block.

I use HTML.vim, for most serious work
but for simple NOTES, TODO, CHANGELOG, etc files I use markdown, and this helps a lot.
It’s a copy of a function called VisualHTMLTagWrap.

Written by totalrecall

October 12, 2009 at 11:23 pm

Posted in vim

Vim and Textile

leave a comment »

Tim the Enchanter, has some great things to say about the vim editor, my childhood friend. He talks of Textile for Vim. Sounds great. However, I wanted to leave a note telling him about HTML.vim but Tim doesn’t take comments.

I used to like Textile but much prefer Maruku now.

Neverblock: asynch IO for ruby.
Neverblock mysql.

Written by totalrecall

August 31, 2008 at 6:07 pm

Posted in vim

Using HTML/XHTML Editing in Vim

leave a comment »

This is just SOOOOO mind-blowing. How could anyone edit HTML in anything other than VIM once they get their hands around this … HTML.vim.

One quick glance at the above document will convince you that Vim IS the text-editor to use for HTML’ing.

while (<>) {

// OOOOH ! see how they convert characters like \ to their entity equivalents by just typing ;& over the character. No having to hunt up HTML docs to figure out.

This is sooo coool kewl. And now to try out previewing this in Firefox!
Well, all i have to do is: “!open %” from within, Z’all.

One small tip for Vim newbies. In order to visually mark out a word prior to bold-facing it or running any other formatting command on it, one need not use the mouse or arrow keys to get the whole word selected. “viw” is enough. — “v” for visual mode, “iw” for internal word”. Cooler, any day, than double-clicking! The two words “any day” were bolded not through mouse or arrow key jugglery, but the very predictable and easy “v” followed by “e” meaning end of next word.

This is so @#$% easy, no more selecting words clumsily with a mouse and then searching through some menu to get formatting commands. How painful and “last century” that was!

Now let’s post this winner with a “#mtn” !

[Update: No more putting P
tags, boys, WP is adding BR and P’s all over and creating an ocean of whitespace in between paras.]

Now let’s update this champ with a “#mte” !

Written by totalrecall

March 2, 2007 at 7:43 pm

Posted in life, vim

testing vim macros with blogger

leave a comment »

gentlemen. Now is the time to rise, er raise your mineral water glasses. We can finally breathe !!!

We can finally edit our blogs in Vim, and publish them from within Vim at a keystroke. What is more we can write scripts in other languages and easily interface them with Vim. What is more, content written in Vim can then be processed by a processor such as Textile or Markdown, Maruku etc before sending off to the server!

The possibilities are endless.

Let us rejoice. We are free from the tyranny of things non-vim.

Written by totalrecall

March 2, 2007 at 5:42 pm

Posted in life, vim

Pretty Print Code script (hack)

The hack presented below has been developed on a MAC OS X. It uses the TOhtml function of the Vim Editor (i use Version 7.0). It runs in HTML Mode only.

f=$1vim -f +"syn on" +"run! syntax/2html.vim" +"wq" +"q" $f

sed  -n '/<pre>/,/<\\/pre>/p' $f.html | sed 's~<pre>~<pre style="margin: 1.5em 0px; padding: 10px 10px 10px 10px; color: #333; background: #000000;color:#ffffff; font-family: monaco,monospace; font-size: 120%; border-left: solid #ccc 1px; overflow: auto;">~p' > $f

rm $f.html

Line 1. saves temp file name to a variable f.

Line 2. Invokes a vim plugin on the temporary file. Vim creates a file with an html extension.

Line 3. Extracts lines from output file between PRE tags. Then replace opening PRE tag with out own styling. This is required for me, since this blog does not allow me to modify the CSS.

Line 4. Delete the temp file created by Vim.

Note: If most of the code you post is in one language then the input file given to Vim should have that extension. That way the Vim plugin will do a more comprehensive syntax highlighting.

The other option is to detect the programming language from the first line of the temp file (assuming it has the interpreter name in it) and rename the file accordingly.

Source (please note that this program does not treat the escape character \, and WP does not print the \)

Written by totalrecall

March 1, 2007 at 1:00 pm

Posted in ecto, Mac OS X, ruby, vim

Vim and Ruby

leave a comment »

Have been doing some work of late using my favorite editor VIM. A life-changing article or series of tips is this page. Using Vim with and a programming language like Ruby, makes a very powerful combination.

So I do part of my work in Vim itself, using its powerful regular expressions and other features. Once I am done in Vim, I run a ruby program over the file and my job is done.

Ruby has its glitches here and there, but still very powerful. And great fun. And very productive, especially for someone coming from the verbose world of Java. So it looks like my days (years) with Java are over! Phew !

Written by totalrecall

February 28, 2007 at 6:51 pm

Posted in ecto, Mac OS X, ruby, vim