LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-31-2017, 01:44 PM   #1
dedec0
Member
 
Registered: May 2007
Posts: 743

Rep: Reputation: 29
Question Vim: why these steps are not making an HTML|XML file indented?


Vim: why these steps are not making an HTML|XML file indented?

I found these steps, which are basically what I tried to do before, did not work. But someone said the steps worked... so I tried them. Did not work either. Maybe you can point to me what is missing in this machine.

The question: How do we auto-format HTML in Vim?

I am using Vim 8.0.707. I am pretty sure that I have used my usual indent choices in Vim, which are shiftwidth=softtabstop=4,tabstop=8,smartindent and not much beyound that. Months and years ago, it always worked for XML/HTML/etc. tag files (did not really matter), including reduced indentation when some tag is closed (with several lines of content, including other tags or groups of tags).

The answer for that question, which should have worked, is given below, together with a small problematic code (built just for this thread).

I got an ugly HTML which started with the two lines:

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" 
    "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
With the whole code opened in Vim, I do:

1) join all the lines in one: ggVGgJ

2) Add newlines at every tag end: :%s/>\s*</>\r</ge

3) Set HTML filetype: :set ft=html

4) Indent everything: gg=G

After 4, all lines are with zero indentation! I must be missing something basic...

To make the question context complete (as much as possible, I hope), :set output (with a few probably unrelated things removed) is:

Code:
  ttyfast
  cinoptions=(0,W4    smartindent         ttymouse=sgr
  noequalalways       scrolloff=8         softtabstop=4     nowrap
  filetype=xml        shiftwidth=4        syntax=xml
  backspace=indent,eol,start
  cinkeys=0{,0},0),!^F,o,O,e
  fileencodings=ucs-bom,utf-8,default,latin1
I have just tried to use Vim 7.4.1689 in another machince. Similar options, and I also tried to: turn off cindent; turn on autoindent; turn on smart indent. All of these were done while filetype=html. The third line with a valid code starting with

Code:
<!DOCTYPE html>
<html>
  <head>
was always left shifted to the line start when I autoindented it with ==

Lets see what is sillyness...
 
Old 01-01-2018, 10:03 AM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: Slackware
Posts: 8,235

Rep: Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344Reputation: 3344
Don't bother with that. Just set HTML Tidy as your "equalprg" and then use the equals key.

http://www.html-tidy.org/
http://vim.wikia.com/wiki/Cleanup_your_HTML

Last edited by dugan; 01-01-2018 at 09:36 PM.
 
1 members found this post helpful.
Old 01-02-2018, 06:32 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166Reputation: 3166
XML and HTML are verbose-enough as they are – they don't require extra whitespace.
 
Old 01-02-2018, 08:02 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.4
Posts: 1,245

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
Quote:
Originally Posted by sundialsvcs View Post
XML and HTML are verbose-enough as they are – they don't require extra whitespace.
True, but they can be pretty challenging to read without indenting.
Decdec0: have you looked at SciTE? Excellent GUI editor with syntax highlighting.
(Sorry for not helping with your OP. I do SciTE or nano from the command line. — only use vi when forced to, as with crontab -e)
 
Old 01-03-2018, 01:40 AM   #5
dedec0
Member
 
Registered: May 2007
Posts: 743

Original Poster
Rep: Reputation: 29
Arrow

Quote:
Originally Posted by scasey View Post

Quote:
Originally Posted by sundialsvcs View Post
XML and HTML are verbose-enough as they are they don't require extra whitespace.
True, but they can be pretty challenging to read without indenting.
I agree with you, scasey, we read those kinds of file much easier when they are indented, including broken tag variables, if there is several of them, or if they are long.

Quote:
Originally Posted by scasey View Post
Decdec0: have you looked at SciTE? Excellent GUI editor with syntax highlighting.
No, I have not tried that editor. But I am pretty much satisfied with Vim, which is an editor I have used most, since more than a decade ago. Vim has syntax highlighting. HTML and similar files are highlighted as we could want.

Quote:
Originally Posted by scasey View Post
(Sorry for not helping with your OP. I do SciTE or nano from the command line. only use vi when forced to, as with crontab -e)
Vi (which is usually the same executable as Vim, in most distros nowadays) is very limited. And Vim limits or changes (for full vi compatibility) several of its features when it is called as vi. The name "Vim" comes from the merge of "Vi" with "improved" - and it is a *much better* editor than Vi! It has basically all features everyone would want in (any kind of) advanced text or source code editor.

------------------------------------

And there is another post's reply that I am without much time for doing. It is about the "setup of Vim to indent HTML" post above. There are two options for doing that, and for both I need to better learn a few things about Vim. And I decided to learn them for myself instead of wanting the answers as detailed as possible, which are, sometimes, hard to happen.
 
Old 01-04-2018, 08:38 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,354

Rep: Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509Reputation: 1509
Quote:
Originally Posted by scasey View Post
only use vi when forced to, as with crontab -e)
You're not force to, you can set VISUAL or EDITOR environment variables, crontab(1):

Code:
       -e     Edits the current crontab using the editor specified by the VISUAL
              or EDITOR environment variables.  After you exit from the editor,
              the modified crontab will be installed automatically.
 
1 members found this post helpful.
  


Reply

Tags
html autoindent format


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Clean up html file and generete a xml corfuitl Programming 11 12-26-2012 10:32 AM
[SOLVED] Strip HTML tags from XML file corfuitl Programming 6 03-26-2012 04:39 PM
how to import XML or HTML file into mediawiki file?? apzc2529 Linux - Server 0 11-10-2006 06:58 AM
RSS XML File to be read and generate HTML page redhatrosh General 10 02-15-2006 02:16 AM
How can i read an write to a HTML or xml file using C alix123 Programming 1 11-24-2004 05:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration