Skip to content

Instantly share code, notes, and snippets.

@tsukkee
Created September 25, 2011 05:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsukkee/1240267 to your computer and use it in GitHub Desktop.
Save tsukkee/1240267 to your computer and use it in GitHub Desktop.
Yokohama.vim #2 presentation by tsukkee
--
Yokohama.vim #2
キーマッピングを考える
tsukkee
--
自己紹介
@tsukkee (つっきー)
某メーカーで研究してます
lingr.vimとか作ってます
Yokohama.vimは#0以来2回目の参加
Vimテクニックバイブルが間に合わなかったorz
-> と思ったらさっき届いた!
--
本日のテーマ
Vimのキーマッピングについて考えてみる
私がキーマッピングについて考えていることを
実例を交えてお話したいと思います
--
というわけでさっそく!
--
その1!
押しにくいなら押しやすくすればいい!
--
たとえば、Vimは<Esc>を多用するけど<Esc>押しにくいから
クソだよねww みたいな物言いがありますが…
押しにくければ押しやすくすればいい!
--
たとえば、以下のような感じ
inoremap <C-e> <Esc>
vnoremap <C-e> <Esc>
cnoremap <C-e> <C-c>
--
Shougoさんぐらいになるとこうなる
inoremap jj <Esc>
onoremap jj <Esc>
cnoremap jj <C-c>
inoremap j<Space> j
onoremap j<Space> j
cnoremap j<Space> j
私の現在の設定は後ほど…
--
ついでに<Leader>も適当に押しやすくしておこう
(なぜか, (コンマ)に割り当てている人が多い)
let mapleader = ','
" デフォルトは\ (バックスラッシュ)
余談ですが, (コンマ)はデフォルトで
どういう挙動か知っていますか?
sample sample sample sample sample
--
他にも押しにくいと思ったら押しやすいキーを割り当てる
隣のタブへ移動
nnoremap L gt
nnoremap H gT
gvimでシステムのクリップボードを使う
nnoremap gy "*y
nnoremap gp "*p
fakeclip使ってる場合は以下のような感じでもOK
map gy "*y
map gp "*p
if exists('$WINDOW') || exists('$TMUX')
map gY <Plug>(fakeclip-screen-y)
map gP <Plug>(fakeclip-screen-p)
endif
--
みんな大好きsurrund.vimも使いやすく
nmap s <Plug>Ysurround
text texttext text
--
ポイント!
デフォルトマッピングでなんか押しにくいと感じたら即変更
普段あんまり使ってないマッピングは躊躇なく潰す
たとえば
nnoremap L gt
とか
nmap s <Plug>Ysurround
とかのように、あまり使わないところは
よく使うところに割り当ててしまう
--
その2!
Vimの挙動を変えてしまう(ような感じにする)!
--
カーソルの動きを直感的(?)にする
nnoremap j gj
nnoremap gj j
nnoremap k gk
nnoremap gk k
nnoremap 0 g0
nnoremap g0 0
nnoremap $ g$
nnoremap g$ $
すごーーーーーーーく一行が長いところーーーーーーーーーーーーーーーーーーーは微妙にカーソルを動かしにくいーーーーーーーーーーーーきがする?そんなこともないーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーですか?
--
Ex modeの履歴を遡るのもちょっと楽できる
cnoremap <C-p> <Up>
cnoremap <Up> <C-p>
cnoremap <C-n> <Down>
cnoremap <Down> <C-n>
--
i_CTRL-uやi_CTRL-wの操作を
操作履歴として覚えておく (Vim Hacks #81 より)
inoremap <C-u> <C-g>u<C-u>
inoremap <C-w> <C-g>u<C-w>
--
ちょっと高度だけどプラグインを使って
タグ検索をUniteで置き換える
nnoremap <buffer> <C-]>
\ :<C-u>UniteWithCursorWord -immediately tag<CR>
ヘルプ引くのをref.vimにする
(これはref.vimがデフォルトで定義するけど^^;)
nmap K <Plug>(ref-keyword)
などなど
--
ポイント!
Vimのデフォルトの挙動を拡張したような気になれる!
通常のマッピングから慣れるコストほぼ0で使える!
--
その3!
新たなキーマッピングを編み出せ!
--
最後に編集したところを選択 (kanaさんに教えてもらった)
nnoremap gc `[v`]
--
foldの開閉を簡単にする (ns9tksさんからパクった)
nnoremap <expr> h
\ col('.') == 1 && foldlevel(line('.')) > 0 ? 'zc' : 'h'
nnoremap <expr> l
\ foldclosed(line('.')) != -1 ? 'zo' : 'l'
{{{
folding
folding
}}}
--
もうちょい高度な例 (Vim Hacks #214 より)
onoremap ) t)
onoremap ( t(
onoremap ] t]
onoremap [ t[
function (argument) { hash['key'] }
--
ポイント!
`[ `]とか t とかあまり知られてない(?)けど
便利なのがあるので活用する!
<expr> も組み合わせると可能性が広がる!
--
その4!
Kana神より賜れしプラグインを活用する!
--
たとえば、ウィンドウのサイズ変更(<C-w>+とか)を
繰り返し入力するのは大変!
--
submode.vimを使う
call submode#enter_with(
\ 'winsize', 'n', '', '<C-w>>', '<C-w>>')
call submode#enter_with(
\ 'winsize', 'n', '', '<C-w><', '<C-w><')
call submode#enter_with(
\ 'winsize', 'n', '', '<C-w>+', '<C-w>+')
call submode#enter_with(
\ 'winsize', 'n', '', '<C-w>-', '<C-w>-')
call submode#map('winsize', 'n', '', '>', '<C-w>>')
call submode#map('winsize', 'n', '', '<', '<C-w><')
call submode#map('winsize', 'n', '', '+', '<C-w>+')
call submode#map('winsize', 'n', '', '-', '<C-w>-')
--
textmanipと組み合わせて
call submode#enter_with('textmanip', 'v', 'r', '<C-t>h',
\ '<Plug>(textmanip-move-left)')
call submode#enter_with('textmanip', 'v', 'r', '<C-t>j',
\ '<Plug>(textmanip-move-down)')
call submode#enter_with('textmanip', 'v', 'r', '<C-t>k',
\ '<Plug>(textmanip-move-up)')
call submode#enter_with('textmanip', 'v', 'r', '<C-t>l',
\ '<Plug>(textmanip-move-right)')
call submode#map('textmanip', 'v', 'r', 'h',
\ '<Plug>(textmanip-move-left)')
call submode#map('textmanip', 'v', 'r', 'j',
\ '<Plug>(textmanip-move-down)')
call submode#map('textmanip', 'v', 'r', 'k',
\ '<Plug>(textmanip-move-up)')
call submode#map('textmanip', 'v', 'r', 'l',
\ '<Plug>(textmanip-move-right)')
テキストのかたまり
テキストのかたまり
--
いろいろキーマッピングしすぎて割り当てるキーがなくなってきた!
--
smartchr.vimを使う
inoremap <expr> = smartchr#loop('=', ' = ', ' == ')
inoremap <expr> . smartchr#loop('.', '->')
--
arpeggio.vimを使う
必ず使う前にarpeggio#load()を呼んでおく
call arpeggio#load()
Uniteを一発で起動
Arpeggionnoremap km :<C-u>Unite buffer<CR>
2キー以上の同時押しも可能
Arpeggionnoremap asdf: oHello arpeggio!<Esc>
--
最初の方で触れた<Esc>押しにくい問題も解決!
Arpeggionmap fj <Esc>
Arpeggioimap fj <Esc>
Arpeggiocmap fj <Esc>
Arpeggiovmap fj <Esc>
--
arpeggio.vimを使うコツ
arpeggioに含まれてるキーに何か割り当てたいときは
以下のようにする必要がある
nnoremap <Plug>(arpeggio-default:j) gj
nnoremap <Plug>(arpeggio-default:k) gk
すごーーーーーーーく一行が長いところーーーーーーーーーーーーーーーーーーーは微妙にカーソルを動かしにくいーーーーーーーーーーーーきがする?そんなこともないーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーですか?
同時押しをミスったときにリスクが小さいキーを選ぶ
Arpeggionnoremap dj {rhs}
とかしちゃうとミスったときにテキストが消える
Arpeggionnoremap fj {rhs}
だと、せいぜいカーソルが移動するぐらい
--
まとめ
押しにくいキーは押しやすくすればいい!
デフォルトの挙動を拡張する!
新たなマッピングを編み出す!
Kanaさんはやっぱりすごい
arpeggio.vimとかは実装もかなり面白いので
興味ある方は見てみてください
他人のvimrcは宝の山!
--
最後に
皆様のvimrcに少しでも役に立てば嬉しいです
ご清聴ありがとうございました!
" ==================== Utilities ==================== "
set nocompatible
let s:runtimepath = expand('~/.vim')
" define and reset augroup used in vimrc
augroup vimrc
autocmd!
augroup END
" ==================== Vundle ==================== "
filetype off
set runtimepath&
let &runtimepath = &runtimepath . ',' . s:runtimepath . '/bundle/vundle'
call vundle#rc()
Bundle 'altercation/vim-colors-solarized'
Bundle 'kana/vim-arpeggio'
Bundle 'kana/vim-fakeclip'
Bundle 'kana/vim-smartchr'
Bundle 'kana/vim-submode'
Bundle 'kana/vim-surround'
Bundle 'Shougo/unite.vim'
Bundle 'Shougo/vimproc'
Bundle 't9md/vim-quickhl'
Bundle 't9md/vim-textmanip'
Bundle 'thinca/vim-quickrun'
Bundle 'thinca/vim-ref'
Bundle 'git@github.com:tsukkee/lingr-vim.git'
Bundle 'git@github.com:tsukkee/ttree.vim.git'
Bundle 'git@github.com:tsukkee/vundle.git'
Bundle 'git@github.com:tsukkee/unite-help.git'
Bundle 'git@github.com:tsukkee/unite-tag.git'
Bundle 'ujihisa/unite-colorscheme'
filetype plugin indent on
" ==================== Settings ==================== "
" tab
set tabstop=4 shiftwidth=4 softtabstop=4
set expandtab
set smartindent
set history=100
" input support
set backspace=indent,eol,start
set formatoptions+=m " add multibyte support
set nolinebreak
set iminsert=0
set imsearch=0
" command completion
set wildmenu
set wildmode=list:longest,full
" search
set notagbsearch " avoid bug about searching multibyte characters
set nowrapscan
set ignorecase
set smartcase
set incsearch
set hlsearch
nohlsearch " reset highlighting when reloading vimrc
" reading and writing file
set directory-=. " don't save tmp swap file in current directory
set autoread
set hidden
set tags=./tags; " search tag file recursively (See: :h file-searching)
" display
set showmatch
set showcmd
set showmode
set nonumber
set wrap
set scrolloff=0
set foldmethod=marker
set ambiwidth=double
augroup vimrc
autocmd WinLeave * setlocal nocursorline
autocmd WinEnter,BufRead * setlocal cursorline
augroup END
" detect encoding
set fileencodings=iso-2022-jp,euc-jp,cp932,utf-8,latin1
" use 'fileencoding' for 'encoding' if the file doesn't contain multibyte characters
" give up searching multibyte characters when searching time is over 500 ms
autocmd vimrc BufReadPost *
\ if &fileencoding =~# 'iso-2022-jp' && search("[^\x01-\x7e]", 'n', 0, 500) == 0
\| let &fileencoding=&encoding
\| endif
" detect line feed character
set ffs=unix,dos
" show quickfix automatically
autocmd vimrc QuickfixCmdPost * if !empty(getqflist()) | cwindow | endif
" persistent undo
if has('persistent_undo')
set undofile
let &undodir = s:runtimepath . '/undo'
endif
" ==================== Hightlight ==================== "
augroup vimrc
autocmd ColorScheme * call s:onColorScheme()
autocmd VimEnter,WinEnter * call matchadd('ZenkakuSpace', ' ')
augroup END
function! s:onColorScheme()
" Modify colorscheme
if !exists('g:colors_name')
return
endif
if g:colors_name == 'solarized'
" based on SpecialKey
highlight ZenkakuSpace ctermbg=12 guibg=#839496
" based on ErrorMsg
highlight User1 ctermbg=10 ctermfg=1 cterm=bold
\ guibg=#586e75 guifg=#dc322f gui=bold
" based on ModeMsg
highlight User2 ctermbg=10 ctermfg=4 cterm=bold
\ guibg=#586e75 guifg=#268bd2 gui=bold
" indent guids
highlight IndentGuidesOdd ctermbg=187
highlight IndentGuidesEven ctermbg=186
endif
endfunction
syntax enable
" colorscheme
if &t_Co == 256 || has('gui')
let g:solarized_contrast = 'high'
colorscheme solarized
endif
" ==================== Keybind and commands ==================== "
" use more logical mapping (See: :h Y)
nnoremap Y y$
" reset highlight
nnoremap gh :<C-u>nohlsearch<CR>
" next/prev
nnoremap <silent> <C-n> :<C-u>silent! call Set_to_top(0)<CR>
nnoremap <silent> <C-p> :<C-u>silent! call Set_to_top(1)<CR>
function Set_to_top(up)
set lazyredraw
/--
if a:up
normal NN
endif
let c = line('.') - line('w0')
if c > 0
execute 'normal!' string(c) . "\<C-e>"
endif
nohlsearch
set nolazyredraw
endfunction
" vim (use :help)
autocmd vimrc FileType vim setlocal keywordprg=:help
" quickhl
nmap <Space>m <Plug>(quickhl-toggle)
vmap <Space>m <Plug>(quickhl-toggle)
nmap <Space>M <Plug>(quickhl-reset)
" Quickrun
nmap <Space>q <Plug>(quickrun)
vmap <Space>q :QuickRun<CR>
nnoremap <Space>w :<C-u>update<CR>
@tsukkee
Copy link
Author

tsukkee commented Sep 25, 2011

vim -u /path/to/yokohamavim2.vimrc /path/to/yokohamavim2.vim
で使えます。<C-n>と<C-p>でページ(のようなもの)移動となります。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment