programing tip

VI에서 HTML 파일의 들여 쓰기를 어떻게 정리합니까?

itbloger 2020. 7. 13. 21:40
반응형

VI에서 HTML 파일의 들여 쓰기를 어떻게 정리합니까?


모두 엉망인 그의 거대한 HTML 파일의 들여 쓰기를 어떻게 수정합니까?

코드 파일의 들여 쓰기를 수정하는 데 사용 하는 일반적인 "gg = G"명령을 시도했습니다 . 그러나 HTML 파일에서는 제대로 작동하지 않는 것 같습니다. 단순히 모든 서식을 제거했습니다.

나는 또한 :filetype = xml이것이 XML 파일이라고 생각하도록 속이는 것이 도움이 될지 알기 위해 설정을 시도 했지만 여전히 그것을하지 않았다.


함께 filetype indent on안에 내 .vimrc, 빔 들여 쓰기 HTML은 아주 잘 파일.

shiftwidth가 2 인 간단한 예 :

<html>
  <body>
    <p>
    text
    </p>
  </body>
</html>

모두 제자리에 있어야 할 것이 몇 가지 있습니다. 한 곳에서 모두 요약하면 다음과 같습니다.

다음 옵션을 설정하십시오.

:filetype indent on
:set filetype=html           # abbrev -  :set ft=html
:set smartindent             # abbrev -  :set si

그런 다음 커서를 파일의 상단으로 이동하고 끝까지 들여 쓰기 gg =G
하십시오 . 또는 들여 쓸 텍스트를 선택하고 =들여 쓰기하십시오.


스마트 들여 쓰기를 사용하는 주된 문제는 XML (또는 HTML)이 한 줄에 있으면 curl 요청에서 다시 나타날 수 있고 gg=G그 트릭을 수행하지 않는다는 것입니다. 대신 저는 VI에서 직접 호출 한 깔끔한 방식을 사용하여 좋은 들여 쓰기를 경험했습니다.

:!tidy -mi -xml -wrap 0 %

이것은 기본적으로 VI를 깔끔하게 호출하여 라인을 줄 바꿈하지 않은 XML 파일을 정리하여 기본 68 자 너비 라인에 맞도록합니다. 큰 29MB XML 파일을 처리했으며 5 초 또는 6 초가 걸렸습니다. HTML 파일의 경우 명령은 다음과 같아야합니다.

:!tidy -mi -html -wrap 0 %

주석에서 언급했듯이 tidy많은 기본 Linux / MacOS 시스템에서 찾을 수있는 기본 도구입니다. 다음은 원하는 경우에 대비하여 프로젝트 페이지입니다. HTML Tidy .

이것이 도움이되기를 바랍니다.


tylerl이 위에서 설명했듯이 다음을 설정하십시오.

:filetype indent on
:set filetype=html
:set smartindent

하나!

정력 7.4 HTML 태그에 있습니다 html, head, body, 일부 다른 사람이되어 있지 기본적으로 들여 쓰기. html 파일의 거의 모든 내용이 해당 태그에 포함되므로 이치에 맞습니다. 정말로 원한다면 태그를 들여 쓰기 할 수 있습니다.

:let g:html_indent_inctags = "html,body,head,tbody" 

자세한 내용은 여기여기 를 참조하십시오.


이것은 "간편한"HTML을 깔끔한 간격으로 여는 데 잘 작동하는 내 솔루션입니다.

vim fileIn.html -c "set sw=2 | %s/>/>\r/ | execute 'normal gg=G' | set nohlsearch | g/^\\s*\$/d"

The sw command is because my default is 4, which is too high for HTML.

The next part adds a newline (Vim thinks it's a carriage return, sigh) after each element (>)

Then re-indent the entire file with =

Then unhighlight > (since I have set hlsearch in my vimrc)

Then remove all empty / whitespace-only lines (see here for more, also this is double-escaped because it's in the shell)

You can even add | wq! fileOut.html to the end if you don't want to enter Vim at all, but just clean up the file.


I use this script: https://github.com/maksimr/vim-jsbeautify

In the above link you have all the info:

  1. Install
  2. Configure (copy from the first example)
  3. Run :call HtmlBeautify()

Does the job beautifully!


Have you tried using the HTML indentation script on the Vim site?


Here's a heavy-weight solution that gets you indenting, plus all the HTML pretty-printing you don't necessarily want to care about while you're editing.

First, download Tidy. Make sure you add the binary to your path, so you can call it from any location.

Next, create a config file describing your favorite HTML flavor. Documentation is not great for Tidy, but here's an overview, and a list of all the options. Here's my config file:

bare: yes
break-before-br: no
clean: yes
drop-proprietary-attributes: yes
fix-uri: yes
indent-spaces: 4
indent: yes
logical-emphasis: yes
markup: yes
output-xhtml: yes
quiet: yes
quote-marks: yes
replace-color: yes
tab-size: 4
uppercase-tags: no
vertical-space: yes
word-2000: yes
wrap: 0

Save this as tidyrc_html.txt in your ftplugin folder (under vimfiles).

One more file: add the following line to (or create) html.vim, also in ftplugin:

map <leader>tidy :%! tidy -config ~/vimfiles/ftplugin/tidyrc_html.txt <CR>

To use it, just open an HTML file, and type /tidy. (That / is the <leader> key.)

There you go! Not a quick solution, by any means, but now you're a bit better equipped for editing those huge, messed-up HTML files.


You can integrate both tidy and html-beautify automatically by installing the plugin vim-autoformat. After that, you can execute whichever formatter is installed with a single keystroke.


I tried the usual "gg=G" command, which is what I use to fix the indentation of code files. However, it didn't seem to work right on HTML files. It simply removed all the formatting.

If vim's autoformat/indent gg=G seems to be "broken" (such as left indenting every line), most likely the indent plugin is not enabled/loaded. It should really give an error message instead of just doing bad indenting, otherwise users just think the autoformat/indenting feature is awful, when it actually is pretty good.

To check if the indent plugin is enabled/loaded, run :scriptnames. See if .../indent/html.vim is in the list. If not, then that means the plugin is not loaded. In that case, add this line to ~/.vimrc:

filetype plugin indent on

Now if you open the file and run :scriptnames, you should see .../indent/html.vim. Then run gg=G, which should do the correct autoformat/indent now. (Although it won't add newlines, so if all the html code is on a single line, it won't be indented).

Note: if you are running :filetype plugin indent on on the vim command line instead of ~/.vimrc, you must re-open the file :e.

Also, you don't need to worry about autoindent and smartindent settings, they are not relevant for this.

참고URL : https://stackoverflow.com/questions/815548/how-do-i-tidy-up-an-html-files-indentation-in-vi

반응형