tools/vim

Learn vimscript the hard way: 18장 - Responsible Coding

seul chan 2020. 3. 20. 17:09

Responsible Coding

지금까지는 vimscript 자체보다는 vim과 vimrc에 넣을 설정들 위주로 설명되어 있었는데, 이 챕터부터는 본격적으로 vimscript에 대한 설명이 시작된다.

이번 챕터는 "어떻게 vimscript를 작성하면서 제정신을 유지하는지" (...)에 관한 챕터이다. (악명높은 vimscrip..)

Commenting

vimscript의 옵션과 코멘드들은 읽기 힘들기 때문에 주석을 많이많이 달아주자!

Grouping

~/.vimrc에 추가한 매핑들은 사용하기 편하지만 점점 많아지면서 관리가 힘들어 질 수 있다.

이를 해결하기 위해 코드 라인과 섹션을 그룹화하고 folding해주자

vimscript 파일에서 fold를 사용하려면 다음 명령어를 추가해주자

augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal foldmethod=marker
augroup END

vimscript 파일에 대해 folding에 marker 방식을 사용한다는 뜻이다.

vimrc 파일을 적용하고 다음을 추가하면 자동으로 folding이 되는 것을 볼 수 있다.

" Vimscript file settings ---------------------- {{{
augroup filetype_vim
    autocmd!
    autocmd FileType vim setlocal foldmethod=marker
augroup END
" }}}

normal 모드에서 해당 라인에 커서를 놓은 뒤에 za를 입력하면 fold가 on/off 되는 것을 볼 수 있다. (zo로 열고 zc로 닫을 수도 있다.)

이런 식으로 folding을 작성하는 것이 이상할 수도 있는데, vimscript 파일은 예외적으로 대부분의 파일을 보는 사람들은 vim으로 볼 가능성이 높기 때문에 이런식의 작성이 유의미하다고 한다. (나름 맞는 말인듯?)

Shart Names

Vim은 대부분의 명령어나 옵션에서 약어를 제공한다.

:setlocal wrap
:setl wrap

여기서는 ~/.vimrc 파일이나 플러그인 작성시 약어를 사용하는데 주의하라고 강력하게 주장한다.

Exercises

  • Go through your entire ~/.vimrc file and arrange the lines into related groups. Some places to start might be: "Basic Settings", "FileType-specific settings", "Mappings", and "Status Line". Add folding markers with headings to each section.

  • Find out how to make Vim fold everything automatically the first time you open the file. Look at :help foldlevelstart for a good place to start.

  • Go through your ~/.vimrc file and change any abbreviated commands and options to their full names.

  • Look through your ~/.vimrc file and make sure you don't have any sensitive information inside. Create a git or Mercurial repository, move the file into it, and symlink that file to ~/.vimrc.

  • Commit the repository you just made and put it on Bitbucket or GitHub so other people can see it and get ideas for their own. Be sure to commit and push the repository fairly often so your changes are recorded.

  • If you use Vim on more than one machine, clone down that repository and symlink the file there as well. This will make it simple and easy to use the exact same Vim configuration on all machines you work with.