vim - :s/var\zs\d\+/\=(submatch(0)+1)/

ruseel의 이미지

명예의 전당에 있는 "Vi vs Editplus" 글타래에 Santana 님께서

Quote:
var1 => var2
var2 => var3
var3 => var4
var4 => var5
var5 => var6
. .
. .
var999 => var1000

이런 치환을 하고 싶을 때

:s/var\zs\d\+/\=(submatch(0)+1)/

위와 같은 명령을 사용하셨습니다.

여기서 '\z' 가 뭘까요?
(vim 매뉴얼에서 어디를 찾아봐야 할까요?)

jellypo의 이미지

정규표현식에서 찾아보셔야 되고요.
vim help 에서 찾아보실 수 있습니다.
*regular-expression* *regexp* *Pattern*

근데 \z는 뭔지 모르겠군요.;

5. An atom can be one of a long list of items.  Many atoms match one character
   in the text.  It is often an ordinary character or a character class.
   Braces can be used to make a pattern into an atom.  The "\z(\)" construct
   is only for syntax highlighting.

   atom    ::=      ordinary-atom               |/ordinary-atom|
                or  \( pattern \)               |/\(|
                or  \%( pattern \)              |/\%(|
                or  \z( pattern \)              |/\z(|

영어가 짧아서 해석은 ........T_T;

|/\zs| \zs \zs anything, sets start of match 이거 같기도 하네요. ...

제 생각엔 \z가 아니라 \zs 같습니다.
var 뒤에 나오는 패턴이 숫자(digit?)라면 그 숫자에 +1을 한다는거 여기까진 이해하겠는데 \= 사용 이후는 모르겠군요.

ruseel의 이미지

\zs	

Matches at any position, and sets the start of the match there: The next char is the first char of the whole match. |/zero-width| Can be used multiple times, the last one encountered in a matchingbranch is used.	Example: "^\s*\zsif" matches an "if" at the start of a line, ignoring white space.
{not in Vi} {not available when compiled without the +syntax feature}

늘 var1 -> var2로 바꾸는 것 처럼 패턴의 일부분을 바꿀 일이 있다면
\(, \) 을 앞뒤로 치는 수고를 하곤 했는데 이런 좋은 방법이 있었네요. 감사합니다.

\= 뒷부분에 들어갈 수 있는 표현식은 manual에서 eval.txt 안에서 찾았습니다.

help \\zs
help submatch

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.