tools/vim

Learn Vimscript The Hard Way - 26. String

seul chan 2020. 4. 3. 23:57

Strings

vim은 에디터이기 때문에 string을 가장 많이 사용할 것이다.

:echom "Hello"

Concatenation

가장 흔한 strong 처리는 concat이다.

:echom "Hello, " + "world"

Hello, world를 기대했지만 0이 출력된다.

vim의 + 연산자는 오직 숫자에서만 사용 가능하다. string 타입에 +를 사용하면 vim은 숫자로 변환하여 더하려고 시도한다.

:echom "3 mice" + "2 cats"

5가 출력되는 것을 볼 수 있다.

string을 더하기 위해서는 .을 사용해야 한다. .은 vim에서 concatenate string이다.

:echom "Hello, " . "world"

vim은 필요한 경우 string으로 타입변환을 해준다.

:echom 10 . "foo"

10foo가 출력되는 것을 볼 수 있다.

하지만, Number 타입에서만 가능하고 float 타입에서는 에러가 발생한다.

:echom 10.1 . "foo"

vimscript가 이렇게 된 이유는 javascript와 비슷하다. 빠르고 느슨하게 타입을 사용할 수 있지만, 나중에는 업보로 돌아올 것이다.

요 책에서는 vim의 자동 변환은 최대한 지양하고 사용하는 변수의 타입을 명확히 해서 사용하라고 권장한다.

Special Characters

다른 많은 프로그래밍 언어들과 비슷하게 escape 기능을 제공한다.

:echom "foo \"bar\""

쌍따움표(")를 escape하여 foo "bar"가 출력된다. escape는 대부분 예상대로 동작한다.

:echom "foo\\bar"
foo\bar 출력

다음은 조금 특이한 경우이다. echo를 사용한 경우 newline character인 \n를 인식하고 두 줄로 나오지만, echom을 사용하면 foo^@bar로 출력된다. echom을 사용하여 string을 출력하면 vim은 해당 string을 정확하게 출력한다.

^@는 vim에서 표현하는 newline character이다.

:echo "foo\nbar"
foo
bar

:echom "foo\nbar"
foo^@bar

Literal String

vim에서는 극단적인 escape를 피하기 위해 literal-string (문자 그대로 사용하는 string) 사용이 가능하다.

아래 두 예시는 따옴표만 다르게 사용한 같은 예시 같지만 전혀 다른 값이 출력된다.

:echom '\n\\'
\n\\

:echom "\n\\"
^@\

작은 따옴표 (')를 사용하면 vim은 해당 string을 따로 escape 하지 않고 있는 그대로 처리한다.

유일한 예외는 작은따옴표를 연속으로 사용하는 것이다.

:echom 'That''s enough.'
That's enough.

작은따옴표 자체를 escape 하기 위해서 작은따움표를 사용하는 것이 literal-string의 유일한 예외이다.

이후 정규표현식 장에서 다시한번 literal-string이 다뤄질 예정.

Truthiness

vim에서는 이전 챕터에서 다뤘던 것처럼 모든 스트링을 false로 처리한다.

더 궁금하다면 이전 포스트인 Conditional을 참고

:if "foo"
:  echo "yes"
:else
:  echo "no"
:endif

Exercises

  • Read :help expr-quote. Review the list of escape sequences you can use in a normal Vim string. Find out how to insert a tab character.

목록은 다음과 같다. 탭 캐릭터는 예상했던대로 \t를 사용하면 됨.

\...    three-digit octal number (e.g., "\316")
\..    two-digit octal number (must be followed by non-digit)
\.    one-digit octal number (must be followed by non-digit)
\x..    byte specified with two hex numbers (e.g., "\x1f")
\x.    byte specified with one hex number (must be followed by non-hex char)
\X..    same as \x..
\X.    same as \x.
\u....    character specified with up to 4 hex numbers, stored according to the
    current value of 'encoding' (e.g., "\u02a4")
\U....    same as \u but allows up to 8 hex numbers.
\b    backspace <BS>
\e    escape <Esc>
\f    formfeed <FF>
\n    newline <NL>
\r    return <CR>
\t    tab <Tab>
\\    backslash
\"    double quote
\<xxx>    Special key named "xxx".  e.g. "\<C-W>" for CTRL-W.  This is for use
    in mappings, the 0x80 byte is escaped.
    To use the double quote character it must be escaped: "<M-\">".
    Don't use <Char-xxxx> to get a utf-8 character, use \uxxxx as
    mentioned above.
  • Try to figure out a way to insert a tab character into a string without using an escape sequence. Read :help i_CTRL-V for a hint.

<C-v> (ctrl + v)를 사용해서 다음에 오는 non-digit character를 쓸 수 있다. special key는 터미널 코드와 동일하게 사용. tab의 keycode는 9이기 때문에 <C-v>9를 입력하면 탭이 입력된다. (입력 후 다른 문자를 입력하면 :^I로 표시될것이다)

다음 예시는 키보드 타이핑을 그대로 옮겨놓은것. 저대로 치면 안되고 저 키를 눌러야한다.

:echo "test<C-v>9test"<cr>
test    testt
  • Read :help literal-string.

잛으니 한 번 읽어보자.

Note that single quotes are used.

This string is taken as it is.  No backslashes are removed or have a special
meaning.  The only exception is that two quotes stand for one quote.

Single quoted strings are useful for patterns, so that backslashes do not need
to be doubled.  These two commands are equivalent: >
    if a =~ "\\s*"
    if a =~ '\s*'