LoginSignup
30
22

More than 3 years have passed since last update.

Vimでキーワードにマッチした数を表示

Last updated at Posted at 2014-02-19

vim 8.1以降 (patch 8.1.1270以降)

下記のようにshortmessオプションからSフラグを削除すると、 /, ,, *などで検索した際にコマンドラインウィンドウの右隅に[2/20]というように[現在フォーカスしている番号/マッチした件数]が表示される。

set shortmess-=S

vim 8.0以前

vim8.0以前ではshortmessオプションにSフラグがないようなので、vimでは普通に/で検索してもキーワードマッチした数はわからない。

調べると、以下のコマンドならhogeにマッチした数を表示できる。

:%s/hoge//gn

毎回これを入力するのはめんどうなので、以下を設定すればノーマルモードで/入力時に
<Cursor>の部分にカーソルがセットされた状態で検索開始できる。

nnoremap <expr> / _(":%s/<Cursor>//gn")

function! s:move_cursor_pos_mapping(str, ...)
    let left = get(a:, 1, "<Left>")
    let lefts = join(map(split(matchstr(a:str, '.*<Cursor>\zs.*\ze'), '.\zs'), 'left'), "")
    return substitute(a:str, '<Cursor>', '', '') . lefts
endfunction

function! _(str)
    return s:move_cursor_pos_mapping(a:str, "\<Left>")
endfunction

http://d.hatena.ne.jp/osyo-manga/20130424/1366800441
http://d.hatena.ne.jp/miho36/20100621/1277092415

30
22
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
22