How to access the last expression used when searching in Vim?

When I perform a more complex search & replace operation in Vim, I often try it in search , and only if it finds what I expect, I use search & replace .

Is there a way to access the last value from search and put it in search & replace or, alternatively, put that last value in register ?

+6
source share
3 answers

On the VIM wiki page:

 :%s//<cr>//g Replace each match of the last search pattern with the / register (the last search pattern). After pressing Ctrl-R then / to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change. 
+4
source

If you donโ€™t need to edit the search regular expression, you can omit it and vim will use the latest pattern search.

 :%s//<replacement>/ 

From :h :s | /If the {pattern} :h :s | /If the {pattern}

 If the {pattern} for the substitute command is empty, the command uses the pattern from the last substitute or `:global` command. If there is none, but there is a previous search pattern, that one is used. With the [r] flag, the command uses the pattern from the last substitute, `:global`, or search command. 
+1
source

You can change the search history by pressing q / .

Your search history will open in a separate file, rotate vertically.

You can move and edit each line, just like a normal Vim session.

To search, press ENTER on any line.

To cancel, press : q to close the search history window.

You can copy and paste things into and out of this window.

You can also edit your command history by pressing q :.

0
source

Source: https://habr.com/ru/post/971137/


All Articles