Find and Replace within selection in `vi`

前端 未结 6 1614
不知归路
不知归路 2021-01-30 08:09

How do I do a Find and Replace within a selection in vi?

6条回答
  •  星月不相逢
    2021-01-30 08:39

    The range of Ex commands are specified line-wise (see *cmdline-ranges*), and when : is pressed while there is a visual selection, the line range is automatically specified on the command line as '<,'> (see *v_:*), which makes the :s[ubstitute] command operate on the whole lines unless the visual selection boundaries are specified in the search pattern with \%V (see */\%V*), e.g. /\%Vvi\%Vm matches "vim" only within the visual selection, where the end of the selection is specified right before the end of the search pattern since each \%V specifies the next character as the start or end of the visual selection, and thus /\%Vvim\%V would require the visual selection to continue after 'm' to match "vim". Note that using the second \%V in a search pattern isn't necessary unless a match is required to be right at the border of or only partly in the visual selection.

提交回复
热议问题