Using vim Buffers and Marks for Efficient Code Editing

I love things that make my programs act like vi when I need to edit or jump to things quickly. Among the vi addons I use are: viPlugin for Eclipse, viEmu for Visual Studio and SQL Server Management Studio, and Vimperator for Firefox.

I was coding on a rather large project recently using Zend Studio for Eclipse with viPlugin. It adds vi functionality to Eclipse, which is really handy for coding. I was talking to Chris Weldon about it and how I wanted to have the ability to copy multiple lines of code into reusable buffers. I use snippets in Visual Studio for this same functionality, and I knew vi had a way to do it, but I had forgotten what it did.

Buffers

Introducing buffers… Buffers are vi’s answer to snippets. By default, any time you delete, yank, or change an open file, that line is saved in the default buffer. But you can add your own custom buffers using the ” character. Example: “ayy would yank the current line into a buffer name “a”. To reuse that buffer, simply execute in command mode: “ap to paste below or “aP to append to the current position. Buffers are a great way to increase your efficiency when you absolutely have to repeat lines multiple times (say across multiple files).

Marks

I also showed Chris how to use marks, which are basically bookmarks in a document. Marks are added to a document using ‘<mark_name> (single quote – mark name). Example: If you are on line 50 of a file, and you do ‘a and then jump to line 75 (75gg), you can easily jump back to line 50 using ma (go to mark “a”). Both IDEs that I use regularly (Visual Studio and Eclipse) have bookmark functionailty built in, but it is generally a much more involved process to add and jump to bookmarks.

There is one caveat to using viPlugin and viEmu – they don’t allow you to show registers and saved bookmarks! Another vi plugin I use for Firefox (Vimperator) does allow you to show saved bookmarks and registers by using the standard vim commands :bookmarks and :registers, respectively. I have filed bug reports for them and hope to see this functionality appear soon!

This entry was posted in Programming and tagged , , . Bookmark the permalink.

One Response to Using vim Buffers and Marks for Efficient Code Editing

  1. Buffers
    Ctrl-6 …………. alternate file –> see :h alternate-file

    Normal mode
    mA …………….. global mark access all docs
    `A …………….. link to global mark
    gv …………….. repeat last visual mode
    gi …………….. start insert mode in last edition position

    set path and…

    Ctrl-w-f ………. open file under cursor
    [i …………….. show lines with
    gd …………….. go to definition

    insert mode
    Ctrl-y …………. repeat line above char by char
    Ctrl-e …………. repeat line below char by char
    Ctrl-x Ctrl-l …… repeat whole line
    Ctrl-t …………. indent line
    Ctrl-d …………. remove indent line

    Windows
    Ctrl-w-n ……….. open new window

    meny tips:
    http://www.slideshare.net/ZendCon/vim-for-php-programmers-presentation

    despite being directed to php this slide is an excelent document!

    other directed to python:
    http://dancingpenguinsoflight.com/2009/02/python-and-vim-make-your-own-ide/

    Vim best tips:
    http://rayninfo.co.uk/vimtips.html

    Snippets great!
    http://www.vimeo.com/3535418

    Even more!

Leave a Reply