Correct indenting in vim

Posted on 2011-05-07
Last Modified: 2012-05-11
I have a partial HTML file (It's actually Drupal template file with *.tpl.php extension). The root tag is <div>. The file has bad indentation of nested tags (They're on the same level and should be shifted more to the right).

Do you know how to correct the indentation with some vi command? Not manually because the file is quite large.

Things that does not work:

Open in new window

filetype plugin indent on

Open in new window

in .vimrc file

Open in new window

thank you
Question by:xRalf
    LVL 31

    Accepted Solution

    First get the line numbers by going into command mode (Esc) and typing
    :se nu

    Suppose you want to indent once from line 5 to line 30, you do
    This will take you to line 5

    Then still being in command mode do

    If you want to indent to the end of file, in command mode do

    For indenting one line

    Indent back

    Also you would need to show set tabs so that you can see it well
    :se shiftwidth=4
    :se tabstop=4
    The above two lines should be put in the .vimrc file
    LVL 31

    Expert Comment

    If you want to indent from current line to the first line you can do


    NOTE: All of the indentation commands would work in the command mode.  Don't put then in the .vimrc file.

    Indentation is a very old feature, you should not need any plugins for it.

    ONLY put se shiftwidth tabstop commands in the .vimrc file.
    LVL 6

    Author Comment

    Thank you, but it's quite complex and I've written
    no manually
    LVL 6

    Author Comment

    I thought that somebody could wrote a script that is able to utilize XHTML rules and format the file correctly with one command.
    LVL 31

    Expert Comment

    Well, looks like you need a script.  I don't think you can correct all the indentation automatically.  I am not sure if there are any plugins that would do it.
    LVL 6

    Author Comment

    I tried to indent it manually as you advised, with coldfolding it was easier and acceptable. But I will leave the question for sometime open if somebody know about that script.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
    Join Greg Farro and Ethan Banks from Packet Pushers ( and Greg Ross from Paessler ( for a discussion about smart network …
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now