【一人 vimrc advent calendar 2017】コマンドを実行した際にカーソル位置を移動させないスクリプト【21日目】
一人 vimrc advent calendar 2017 21日目の記事になります。
そろそろネタが枯渇してきているのですが、今日 vimrc
から便利そうなスクリプトを発掘したのでそれの初回でも。
カーソル位置などを移動させないでコマンドを実行するスクリプト
元々、他の方の vimrc
に書かれていたスクリプトになります。
" 元ネタ " https://github.com/bling/dotvim/blob/d84e501bd12f9c6887599ace398fe26f542e8f6e/vimrc#L105 function! s:preserve(command) "{{{ " preparation: save last search, and cursor position. let _s=@/ let view = winsaveview() " do the business: execute a:command " clean up: restore previous search history, and cursor position let @/=_s call winrestview(view) endfunction "}}} command! -complete=command -nargs=* \ Preserve call s:preserve(<q-args>)
上記のスクリプトを使うことで例えば :substitute
した時などに『カーソル位置を移動させずに』置換を行うことが出来ます。
:Preserve %s/\s\+$//g
また、以下のようなコマンドを定義しておくと :Preserve %s
を :S
として使えるようになります。
command! -complete=command -range -nargs=* \ S Preserve %s <args>
今までほとんど使ったことがなかったんですが普通に便利そう…。