Vim에서 정규표현식을 이용해서 특정 영역만 복사하기.

natas999의 이미지

예를 들어서 html 파일이라고 하면,

<table>
 .... arbitrary code ....
</table>

과 같은 부분 (파일 내에 여러 군데 존재함)만을 한꺼번에 복사하고 싶습니다.

VIM에서 해결 가능 할까요?

다른 프로그램을 사용하는 방법이라도 좋습니다.

cinsk의 이미지

글쎄요, 원하는 일은 일반 editor에서는 조금 무리일 수도 있겠군요. 찾아보면 그런 기능이 있을지도 모르겠지만..

vim은 잘 모르겠고, emacs면 (아마 비슷한 기능이 이미 있을 수도 있지만) 만들어 쓰면 됩니다.

일단 html 파일인 것 같으니, html-mode를 쓴다고 가정하고,

(require 'sgml-mode)
 
(defun html-end-of-table (&optional arg)
  "Move forward to the end of table"
  (interactive "p")
  (if (or (null arg) (= arg 0)) (setq arg 1))
  (push-mark)
  (if (> (or arg 0) 0)
      (dotimes (i arg)
        (search-forward "</table>" nil t))))
 
(defun html-beginning-of-table (&optional arg)
  "Move backward to the beginning of the table"
  (interactive "p")
  (if (or (null arg) (= arg 0)) (setq arg 1))
  (push-mark)
  (if (> arg 0)
      (dotimes (i arg)
        (forward-char -1)
        (search-backward-regexp "<table[^>]*>" nil t))))
 
(defun html-mark-table ()
  "Put mark at end of this table, point at beginning"
  (interactive)
  (let ((opoint (point)) begin end)
    (html-beginning-of-table)
    (setq begin (point))
    (html-end-of-table)
    (setq end (point))
    (while (looking-at "^\n")
      (forward-line 1))
    (if (> (point) opoint)
        (progn
          (push-mark begin nil t)
          (goto-char end)
          (exchange-point-and-mark))
      (goto-char opoint)
      (html-end-of-table)
      (push-mark (point) nil t)
      (html-beginning-of-table))
    (re-search-backward "^\n" (- (point) 1) t)))
 
(define-key html-mode-map [(control ?c) (control ?e)] #'html-end-of-table)
(define-key html-mode-map [(control ?c) (control ?a)] #'html-beginning-of-table)
(define-key html-mode-map [(control ?c) (control ?h)] #'html-mark-table)

위 코드를 $HOME/.emacs에 추가하고, Emacs를 시작한 다음,
HTML 파일을 열고 C-c C-e를 누르면 다음 table로 커서 이동, C-c C-a를 누르면 바로 앞 table로 이동 C-c C-h를 누르면 현재 table 복사(mark 지정)가 됩니다.

(대충 mark-defun 함수의 내용을 보고 베껴서 만들어서 버그가 있을 가능성이 높음)

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

yundorri의 이미지

:map q /

^Mj8yyGp^O

같이 하면 'q' 키를 누를 때 마다

아래 라인부터 밑으로 8라인이 복사되어 파일의 가장 밑으로
붙여넣게 됩니다. ^M은 한 후 키를, ^O는 를 누르시면 됩니다.

만약,

사이에 라인이 항상 일정하다면 말씀드린 방법으로
할 수 있으나 일정하지 않다면 사용하기 힘들것으로 보입니다.

특정문자가 찾아질 때 까지 삭제나 변경은 가능한데 복사가 가능한지는 잘 모르겟네요.
아시는 분 계시면 리플달아주시면 감사하겠네요. ^ ^;

yundorri의 이미지

:map q /

^MjV/\/table^MkyGp^O

이렇게 하니까 줄개수가 달라도 가능하군요. ^^

natas999의 이미지

감사합니다. 작업중인 파일은 사실은 HTML이 아니고, 어떤 외국어의 어휘집의 원본입니다. (분야 내 베스트셀러인 책입니다. ^^)

내용은 그대로 두고 책의 편제만 바꾸기 위해

VIM을 이용해서 각 장/절을 XML과 비슷한 형식의 (XML을 잘 몰라서) 태그를 붙이는 작업을 하고 있습니다.

그런 다음에 이 태그를 기준으로 다양한 편제의 편집본을 뽑아 볼려고 합니다.

(물론 저자가 시켜서 하는 일입니다 ^^;)

XML을 열심히 공부하는 게 나을까요?

지금은 수동으로 편집하는 중이지만 나중에는 전체를 자동화 할려고 합니다.

자동화 가능한 스크립트/유틸리티를 소개 해 주시면 더 도움이 되겠습니다.

감사합니다.

# emerge girl-friend
Calculating dependencies
!!! All wemen who could satisfy "girl-friend" have been masked.

# emerge girl-friend
Calculating dependencies
!!! All wemen who could satisfy "girl-friend" have been masked.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.