카테고리 없음

Learn vimscript the hard way 01-10

seul chan 2020. 3. 9. 20:21

Learn Vimscript the Hard Way

Echoing Message

  • instead of echo, echom will save the output and let you run :messages to view it later
    :echom "hello world"
    :messages

fold example

Mapping

excersize

imap <c-u> <esc>gUiwA

nmap <c-u> gUiw

Strict mapping

:nmap - dd
:nmap \ -
  • Vim will take other mapping : Pure evil

Recurlsion

:nmap dd O<esc>jddk

  • it looks like clear the line but...
  • It calls the new dd(mapping to clear previous line) again and again and again...

Side effect

  • Danger of recursing
  • Mapping key of plugin

Nonrecursive mapping

  • Each *map has *noremap
    nmap x dd
    nnoremap / x

When should you use these nonrecursive variants instead of their normal counterparts?
Always.
No, seriously, always.

Leaders

  • What if we need the mapping key later?
  • Vim doesn't use H, L, -, <space>, <bs>, <cr>: Only six

Mapping key sequences

  • Easy to map more then just single key
    nnoremap -d dd
    nnoremap -c ddO
  • Vim had mechanism for this "prefix" key
  • Vim called this prefix key the "leader"

Leader

:let mapleader = "-"
  • You can replace - with any key you like
  • I personally like ,
  • Then you can use leader like below
nnoremap <leader>d dd
  • Why don't use - directly instead of <leader>?
      1. Can change leader later easily
      1. Share easily
      1. Many plugins create mappings based on leader

Local leader

  • is to be used for mappings which are local to a buffer
:let maplocalleader = "\\"

Editing your VIMRC

The idea of this chapter is that you want to make it easier to make it easier to edit text.

:nnoremap <leader>ev :vsplit $MYVIMRC<cr>
  • $MYVIMRC: special variable that points your vimrc file
  • Personally, I use global mark V so that I can open vimrc file by \V`

Sourcing mappings

  • Once add config in ~/.vimrc, it doesn't immediately take effect
  • like so % or source ~/.vimrc
  • Let's make it easier
nnoremap <leader>sv :source $MYVIMRC<cr>

Abbreviations

:abbrev adn and
  • Will automatically chagne adn to and

Keyword Characters

  • Vim will substitute an abbreviation when you type any "non-keyword character" after an abbreviation
  • "non-keyword character" means any character not in the iskeyword option.
    :set iskeyword?
    iskeyword=@,48-57,_,192-255
  • it means all ASCII characters, digits, underscore, and so on

More Abbreviations

  • Abbreviations are useful for more than just correcting typos
:iabbrev @@    steve@stevelosh.com
:iabbrev ccopy Copyright 2013 Steve Losh, all rights reserved.

Why Not Use Mappings?

:inoremap ssig -- <cr>Steve Losh<cr>steve@stevelosh.com
  • if type Larry Lessig wrote the book "Remix". after mapping, vim expand ssig into name
  • Mapping don't take into account what characters come before or after the map
:iunmap ssig
:iabbrev ssig -- <cr>Steve Losh<cr>steve@stevelosh.com

More Mappings

  • Can mapping in sequence of multiple keys
    :nnoremap jk dd

A more complicated mapping

:nnoremap <leader>" viw<esc>a"<esc>bi"<esc>lel

Exercises

  • Use vnoremap to quote whatever inside visually selected

    :vnoremap <leader>" <esc>`>a"<esc>`<i"<esc>
  • Map H in normal mode to go to the end of the current line.

  • Originally, H is mapping to line from top of window. :help H

    :nnoremap H 0
  • Map L in normal mode to go to the end of the current line.

  • Originally, H is mapping to line from bottom of window. :help L

    M is mapping to middle of window.

    :nnoremap L $

Training your fingers

:inoremap jk <esc>
  • Many ways to exit insert mode by default
<esc>
<c-c>
<c-[>
  • Using jk is great becasue the keys are right under two of your strongest fingers
  • What about if you write in a language where jk ? Change to different mapping (...)

Learning the Map

  • To excersize new mapping above, you can force yourself to use it by disabling(!) the old key (lol)
:inoremap <esc> <nop>
  • <nop> means no operations