Vim Tips Wiki

Goj/Using Vim with Loreto

1,610pages on
this wiki

< User:Goj

Revision as of 08:37, August 21, 2011 by JohnBeckett (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

We often use regexps to manipulate text. Consider changing
a.LeftBorder = True
a.TopBorder = True
a.RightBorder = True
a.BottomBorder = True


a.BorderLeft = True
a.BorderTop = True
a.BorderRight = True
a.BorderBottom = True

It's quite obvious what regular expression would do the job, but still, typing it is a bit painful. That's why Loreto was created.

Loreto is a Python application that uses the longest common subsequence algorithm to generate regular expressions describing few similar lines and displays GUI that allows one to edit generated regex and preview the results.

This tip explains how to get similar functionality in Vim. The basic idea is that we'll use Loreto's regular expression creation library and do some programming to call it from Vim instead of using Loreto's GUI.

You will need Vim compiled with Python support.

  1. Get Loreto's file:
    svn export
  2. Put it somewhere it can be found by Python. I placed mine in ~/.vim/python folder and I added the following to my ~/.vimrc:
    " basic imports
    python << EOPYTHON
    import vim
    import sys, os
    sys.path.append(os.environ['HOME'] + '/.vim/python')
  3. Next, some configuration. We have to teach Loreto Vim's regex syntax, which has different excaping rules to Python's (~/.vimrc):
    python << EOPYTHON
    def vim_re_escape(text):
        for bit in ['\\', '$', '.', '[', ']', '^', '*', '~']:
            text = text.replace(bit, '\\' + bit)
        return text
    import REgen
    REgen.GLOB_REPR = '\(.*\)'
    REgen.escape = vim_re_escape
  4. Following lines are Vim-Loreto integration:
    python << EOPYTHON
    def dq_escape(string):
        return string.replace('\\', r'\\').replace('"', r'\"')
    def loreto(prefix):
        lines = vim.eval('getreg("r")').rstrip().split('\n')
        globs = REgen.generate(*lines)
        before = globs.as_re()
        after = globs.as_backref().replace(r'\ ', ' ').replace(r'\,', ',')
        subst = prefix + "s/%s/%s/g" % (before.replace('/', r'\/'), after.replace('/', r'\/'))
        vim.eval('setreg("r", "%s")' % dq_escape(subst))
    nnoremap <Leader>r "ryj:python loreto('%')<CR>q:"rp02f/l
    vnoremap <Leader>r "ry:python loreto("'<,'>")<CR>q:"rp02f/l


  • Type some repetitive text and select it


  • Press <Leader>r and see the magic happen:


  • Now, you can edit guessed regex as if you have typed it yourself:


  • Press Enter to apply regex (double Ctrl+c to cancel)



History This page was created on May 28, 2009 and was discussed at 200905 proposed new tips. Due to some unresolved issues, the page has been moved here (a subpage of the author) pending further consideration. The original title was Using Vim with Loreto and that is now a redirect to this page. There is no commitment to keep the redirect, or this page, or the associated images, for more than another three months.

While we are grateful for new contributions, we have too much maintenance to perform to attend to the issues with this tip: We do not want embedded html, and the title "Loreto" needs to be explained at the top of the tip. More importantly, the tip starts by saying "It's quite obvious what regular expression would do the job" but that is a just wrong – it is not obvious to anyone who would bother reading a regex tip. Furthermore, the actual example in the tip does nothing useful: It's a very complex way of entering what is an obvious substitute command. JohnBeckett 05:05, September 4, 2009 (UTC)

Around Wikia's network

Random Wiki