Image of the glider from the Game of Life by John Conway
Skip to content

Search and Indent in Vim

Vim is my beloved editor of choice. Not because I am opinionated about Vi versus Emacs, but because vi was the editor that I picked up, and I have been using it since. I would probably come to enjoy Emacs as well, if I were to take the time to use it.

Anyway, I don't want to start that discussion here. What I do want to discuss is a couple handy tricks that I learned today, while coding. If you're a Vim user, you will probably find these helpful as well.

First, searching with super star. That is, while in normal mode (not visual or insert mode), while pressing the "*" key, you will search for more words in the text file that you cursor is over. Put your cursor over the word "for" in your python source file, and press the "*" key to see all other "for" loops in the file. Frankly, this will save me a ton of typing, as I don't have to reach for ":/" anymore. Nice.

However, "*" only searches forward. Perhaps you want to search in reverse. Easy. The hash "#" character comes in handy for that. It behaves exactly the same way as super star, that is searching for more words beneath your cursor, just towards the beginning of the file, rather than the end. Very nice.

Oh, we're not done with searching yet, though. Find yourself in a lot of nested parens? Unsure which beginning paren matches its ending mate? "%" will become your best friend. "%" not only matches married parens, but it works on braces and brackets as well. Also, it will match multiline C-style comments "/* */". Sweet.

Last, indenting. Being a web developer, I hate taking an existing HTML document that hasn't been properly indented, and trying to parse through it finding beginning and ending tags. So, I would indent the file as appropriate going into insert mode, and pressing the following keystrokes in order: home, tab, down, home, tab, down, etc. As obvious, you are pressing at least 3 keystrokes for every line that needs indenting, not to mention, you need to be in insert mode, and if only indenting one line, this can be a pain. Even worse, if you have to reverse indent. Well, fortunately, there is a quick solution, all of which can be done in normal mode.

">>" will indent the line the cursor is on forward.
"< <" will indent the line the cursor is on backward.
"." will repeat the previous edit.

So, lets take a look. In normal mode, how would my previous keystrokes look: >>, down, ., down, ., down, ., etc. Only 2 keystrokes per line, and I never need to leave normal mode. So, you can see that this would come in handy:

>>... will indent the line forward 3 times.
<<..... will indent the line backward 3 times.

Ooooh I'm in love. I can't tell you how much time this will save me, as well as the saved number of keystrokes. Searching and indenting with as little typing as possible. Hope this was helpful to some.

{ 11 } Comments

  1. bartman using Debian IceWeasel 2.0.0.3 on Debian GNU/Linux 64 bits | May 4, 2007 at 10:13 pm | Permalink

    If you want to indent a block use V to highlight a block and then to indent it.

    For help read:
    :h v_>

  2. Markus Bertheau using Firefox 2.0.0.3 on Ubuntu | May 4, 2007 at 10:15 pm | Permalink

    With the xml.vim plugin % matches opening and closing tags, too.

  3. Frank using Konqueror 3.5 on Kubuntu | May 4, 2007 at 10:34 pm | Permalink

    The "%" also works on #if/#else/#endif. And when searching using "#", "*", or some other technique, you can use ":set hlsearch={on|off}" to turn on/off visual highlighting of search terms.

    If you want to reformat HTML or XML, first install "xmllint". Then from inside VIM, go to line 1 ("1G") and type "!Gxmllint --xmlout --format -" and your current buffer will be replaced with a nicely formatted version. :) Play with different options for formatting HTML instead of XML, for example.

    I also use "gf" a lot (open the filename that the cursor is on), but it's helpful to add to your "file search path" using ":set path+=newdir"

    I highly recommend that my students browser through the ":help" documents. There is a HUGE amount of functionality there...

  4. beza1e1 using Epiphany 2.18 on GNU/Linux | May 5, 2007 at 12:01 am | Permalink

    You can use "=" to "indent right". It's not as good as EMACS "tab", though. To indent the whole file (well, buffer) correctly, you then use "g=G". ;)

  5. dtlin using Firefox 2.0.0.3 on Ubuntu | May 5, 2007 at 1:56 am | Permalink
    1
    &gt;5&gt;

    will indent 5 lines, same as

    1
    &gt;4j

    (indent, plus four lines down)

    As 4 says,

    1
    =
    1
     reformats (
    1
    :set equalprg

    to a custom indenter), which is also very handy.

  6. dtlin using Firefox 2.0.0.3 on Ubuntu | May 5, 2007 at 1:58 am | Permalink

    Gah! What happened to the formatting?

    >5&> will indent 5 lines, same as
    >4j (indent, plus four lines down)

    As 4 says,
    = reformats (:set equalprg to a custom indenter), which is also very handy.

  7. EvilDead using Firefox 2.0.0.3 on Ubuntu | May 5, 2007 at 3:05 am | Permalink

    Hi,

    I'm using the following map to reformat the whole buffer:

    function! ReformatFile()
    exe "normal msHmtgg"
    exe "normal ggVG="
    exe "normal 'tzt`s"
    endfunction

    map :call ReformatFile()

    The first and the third exe are used to save/restore the cursor position.

  8. EvilDead using Firefox 2.0.0.3 on Ubuntu | May 5, 2007 at 3:08 am | Permalink

    Sorry, some parts of the map have been removed, it should be:

    map <silent> <F3> :call ReformatFile()<CR>

    gt and lt being angle brackets.

  9. Jeff Schroeder using Firefox 2.0.0.3 on Ubuntu | May 5, 2007 at 10:11 am | Permalink

    Thanks for the nice vi shortcuts. *, #, and especially % were new to me.

    Awesome!

  10. lisa using Firefox 1.5.0.11 on Ubuntu 64 bits | June 5, 2007 at 5:03 pm | Permalink

    I found that for me, it's sufficient (in cases) to repeat indent (==) very many times, such as,

    1
    99999==

    . I hope that helps!

  11. Ben Hayes using Safari 531.9 on Mac OS | September 18, 2009 at 8:37 am | Permalink

    To format the indentation on the entire buffer just type gg=G and everything will get correctly indented. Much quicker!

{ 1 } Trackback

  1. Baby names search - Search for aaron | May 14, 2009 at 11:16 pm | Permalink

    [...] Aaron Toponce : Search and Indent in Vim reddit_url='http://www.baby-parenting.com/baby/search_babynames.php?babyname=aaron&gender=Male' reddit_title='Baby names search - Search for aaron' [...]

Post a Comment

Your email is never published nor shared.

Switch to our mobile site