Search for string and get count in vi editor

前端 未结 8 2036
小鲜肉
小鲜肉 2021-01-30 01:12

I want to search for a string and find the number of occurrences in a file using the vi editor.

8条回答
  •  清歌不尽
    2021-01-30 01:45

    (similar as Gustavo said, but additionally: )

    For any previously search, you can do simply:

    :%s///gn
    

    A pattern is not needed, because it is already in the search-register (@/).

    "%" - do s/ in the whole file
    "g" - search global (with multiple hits in one line)
    "n" - prevents any replacement of s/ -- nothing is deleted! nothing must be undone!
    (see: :help s_flag for more informations)

    (This way, it works perfectly with "Search for visually selected text", as described in vim-wikia tip171)

提交回复
热议问题