😎

Vim でペーストしたときに自動でインデントするようにする

に公開1

Vim でコードをペーストしたときに『貼り付けた箇所で自動でインデントしてほしい』と思ったんで簡易的なスクリプトをつくりました。

" p P でペーストした時に自動で = で整形する
function! s:auto_indent_paste(key) abort
	let line = count(getreg(v:register), "\n")
	if line < 1
		return a:key
	endif
	return a:key .. "=" .. line .. "j"
endfunction

nnoremap <expr> <Plug>(auto-indent-p) <SID>auto_indent_paste("p")
nnoremap <expr> <Plug>(auto-indent-P) <SID>auto_indent_paste("P")
vnoremap <expr> <Plug>(auto-indent-p) <SID>auto_indent_paste("p")
vnoremap <expr> <Plug>(auto-indent-P) <SID>auto_indent_paste("P")

nnoremap <Plug>(original-p) p
nnoremap <Plug>(original-P) P

nmap p <Plug>(auto-indent-p)
nmap P <Plug>(auto-indent-P)
vmap p <Plug>(auto-indent-p)
vmap P <Plug>(auto-indent-P)

ペーストしたときに自動で『貼り付けた範囲に対して = で整形する』みたいなことをしています。
地味に便利。

GitHubで編集を提案

Discussion