문제

내가 사용하는 이맥스 편집 xml 파일(nxml-mode)과 파일을 생성하여 기계에 있지 않는 모든 예쁜 포맷의 태그가 있습니다.

나는 검색에 대한 꽤 인쇄 전체 파일이 들여쓰기와,저장하지 않았을 찾을 수있는 자동적인 방법입니다.

있는 방법이 있을까?거나,일부 편집기 리눅스에서는 그것을 할 수 있습니다.

도움이 되었습니까?

해결책

내가 사용하는 nXML 드 편집 정돈 면 나는 원하는 포맷하고 들여쓰기 또는 XML HTML.도 는 이맥스 인터페이스를 깔끔합니다.

다른 팁

당신도 필요하지 않을 쓰는 자신의 기능-sgml-모드(gnu emacs 핵심 모듈)내에 꽤 인쇄 라는 함수(sgml-꽤 인쇄...)는 지역의 시작과 끝을 인수입니다.

는 경우 붙여넣기 xml 과 당신의 터미널은 도마에 라인을 임의의 수 있는 곳이 예쁜 프린터 수정하는 부서지는 선 처음이다.

해야 하는 경우에만 꽤 들여쓰기하지 않고 어떤 새로운 라인에-휴식에 적용할 수 있습니다 indent-region 명령을 전체 버퍼를 이러한 키 입력:

C-x h
C-M-\

는 경우에 당신은 또한 필요를 소개하는 라인에 나누기,그래서는 열고 닫는 태그는 별도의 줄에 사용할 수 있습니다 다음주 elisp 기능에 의해 작성된 벤자민 Ferrari.나는 그것이 자신의 블로그에 그것을 즐길 수 있기를 바랍인 나를 위해 그것을 재현하기:

(defun bf-pretty-print-xml-region (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
      (nxml-mode)
      (goto-char begin)
      (while (search-forward-regexp "\>[ \\t]*\<" nil t) 
        (backward-char) (insert "\n"))
      (indent-region begin end))
    (message "Ah, much better!"))

이에 의존하지 않는 외부 도구를 깔끔합니다.

Emacs 실행할 수 있는 임의의 명령 M|.이 있는 경우 xmllint 설치:

"M|xmllint--식-"형식을 지정합니다 지역 선택

"C-u M|xmllint--식-"할 것 같은 대체로 영역을 출력

감사 팀 헬름스테트 위에 내가 만들어 세인트 다음과 같다:

(defun nxml-pretty-format ()
    (interactive)
    (save-excursion
        (shell-command-on-region (point-min) (point-max) "xmllint --format -" (buffer-name) t)
        (nxml-mode)
        (indent-region begin end)))

빠르고 쉽습니다.많은 감사합니다.

를 소개하는 줄 바꿈하고 그 예쁜 인쇄

M-x sgml-mode
M-x sgml-pretty-print

여기에 몇 가지 바뀌는 제가 베냐민 페라리의 버전:

  • search-forward-regexp 지정하지 않은 끝났다,그래서 그것을 운영하는 물건에서 시작 부분의 영역을 끝내의 버퍼(신의 끝 지역)
  • 지금 증가 end 제 Cheeso 지적했다.
  • 그것은 삽입하는 사이에 휴식 <tag></tag>, 수정하는 해당 값입니다.Yes,기술적으로 우리가 값을 수정의 모든 것을 여기에 있지만,빈 시작/종료는 훨씬 더 가능성이 중요하다.지금 사용하는 두 가지 별도의 약간 더 엄격한 검색을 피할 것.

여전히"에 의존하지 않는 깔끔한 외부",등등.그러나,그것은 필요 없 clincf 매크로입니다.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; pretty print xml region
(defun pretty-print-xml-region (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
    (nxml-mode)
    (goto-char begin)
    ;; split <foo><foo> or </foo><foo>, but not <foo></foo>
    (while (search-forward-regexp ">[ \t]*<[^/]" end t)
      (backward-char 2) (insert "\n") (incf end))
    ;; split <foo/></foo> and </foo></foo>
    (goto-char begin)
    (while (search-forward-regexp "<.*?/.*?>[ \t]*<" end t)
      (backward-char) (insert "\n") (incf end))
    (indent-region begin end nil)
    (normal-mode))
  (message "All indented!"))

하나의 방법입니다 당신이 뭔가가있는 경우에는 아래와 같은 형식

<abc>     <abc><abc>   <abc></abc> </abc></abc>       </abc>

에서 이맥스,시험

M-x nxml-mode
M-x replace-regexp RET  > *< RET >C-q C-j< RET 
C-M-\ to indent

이 들여쓰기 위 xml 예 아래에 아

<abc>
  <abc>
    <abc>
      <abc>
      </abc>
    </abc>
  </abc>
</abc>

에 VIM 여 이렇게 할 수 있습니다

:set ft=xml
:%s/>\s*</>\r</g
ggVG=

이게 도움이 되었으면 좋겠습니다.

  1. Emacs nxml-모드에서 작업할 수 있습 제시하는 형식,그러나 당신 분입니다.
  2. 더 이상 파일은 단순히 가치가 있지 않습니다.실행 스타일 시트(이상적으로 색슨 는 이럴 얻 라인 들여쓰기에 대한 권리)에 대하여 더 이상 파일 을 얻을 좋은 꽤 인쇄합니다.에 대한 어떤 요소는 당신을 유지하려는 흰색 공간 자신의 이름을 추가 함께'programlisting'으로서'programlisting yourElementName'

HTH

나는 제이슨 Viers'전 추가 논리를 넣어 xmlns 선언에서 자신의 라인입니다.이 있는 것으로 가정 xmlns=및 xmlns:간 whitespace.

(defun cheeso-pretty-print-xml-region (begin end)
  "Pretty format XML markup in region. You need to have nxml-mode
http://www.emacswiki.org/cgi-bin/wiki/NxmlMode installed to do
this.  The function inserts linebreaks to separate tags that have
nothing but whitespace between them.  It then indents the markup
by using nxml's indentation rules."
  (interactive "r")
  (save-excursion
    (nxml-mode)
    ;; split <foo><bar> or </foo><bar>, but not <foo></foo>
    (goto-char begin)
    (while (search-forward-regexp ">[ \t]*<[^/]" end t)
      (backward-char 2) (insert "\n") (incf end))
    ;; split <foo/></foo> and </foo></foo>
    (goto-char begin)
    (while (search-forward-regexp "<.*?/.*?>[ \t]*<" end t)
      (backward-char) (insert "\n") (incf end))
    ;; put xml namespace decls on newline
    (goto-char begin)
    (while (search-forward-regexp "\\(<\\([a-zA-Z][-:A-Za-z0-9]*\\)\\|['\"]\\) \\(xmlns[=:]\\)" end t)
      (goto-char (match-end 0))
      (backward-char 6) (insert "\n") (incf end))
    (indent-region begin end nil)
    (normal-mode))
  (message "All indented!"))

깔끔한 것 같은 좋은 모드입니다.보고해야합니다.그것을 사용하는 경우에는 정말 필요한 모든 기능을 제공합니다.

어쨌든,이 문제가 있었 잔소리를 내게 주고 나가지 않았다 찾는다.게시 후,나는 검색을 시작하고 하나를 발견 사이트 elisp 기능 는 그것에 매우 좋습니다.저자는 또한 제안을 사용하여 깔끔합니다.

감사에 대한 답변 Marcel (너무 나쁜 나는 충분하지 않은 포인트를 upmod 당신).

게시할 것입니다 그것에 대해 즉시에 내 블로그입니다.게시 그것에 대해 (에 대한 링크와 함께 마르셀의 사이트입니다).

내가 사용하는 xml-reformat-tagsxml 구문 분석합니다.엘.일반적으로 당신이해야 할 것입 지점에서 시작 부분의 파일을 실행하는 경우 이 명령입니다.

그것은 재미있는 파일이 통합 Emacspeak.을 사용했을 때 Emacspeak 에 하루 하루를 기초로,생각 xml-reformat-tags 는 이맥스 제공.어느 날 나는 그것을 잃어 및 인터넷 검색에 대한 것,따라서 입력한 위키 페이지 위에서 언급된다.

나를 부착한 코드를 시작한 xml 구문 분석합니다.가 확실하지 않는 경우에 이 조의 이맥스 코드,그러나 작동하는 것 같습니다.

(if (file-exists-p "~/.emacs.d/packages/xml-parse.el")
  (let ((load-path load-path))
    (add-to-list 'load-path "~/.emacs.d/packages")
    (require 'xml-parse))
)

당신이 사용하는 경우 spacemacs, 그냥 사용하여 명령'spacemacs/을 들여쓰기 영역 또는 버퍼'.

M-x spacemacs/indent-region-or-buffer

나는 다음과 같 Benjamin Ferrari 버전을 훨씬 낫다.내부 꽤 인쇄는 항상 끝 태그에서 새로운 라인 가치 후 삽입하여 원하지 않는 CR 태그 값입니다.

으로 2017 년 이맥스는 이미 이 기능을 가진 기본적으로,하지만 당신은 이것을 쓰는 작은 기능으로 귀하의 ~/.emacs.d/init.el:

(require 'sgml-mode)

(defun reformat-xml ()
  (interactive)
  (save-excursion
    (sgml-pretty-print (point-min) (point-max))
    (indent-region (point-min) (point-max))))

그런 다음 전화 M-x reformat-xml

출처: https://davidcapello.com/blog/emacs/reformat-xml-on-emacs/

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top