The "global" option does not work as expected (this seems to have changed only recently, but I'm not able to track down the reason for the difference yet).
Given the line:
aba
I issue the command:
:s/a//g
I expect the result to be:
b
However, the resulting line is:
ba
What have I missed?
The % is a shortcut that tells vi to search all lines of the file for search_string and change it to replacement_string . The global ( g ) flag at the end of the command tells vi to continue searching for other occurrences of search_string . To confirm each replacement, add the confirm ( c ) flag after the global flag.
Basic Find and Replace In Vim, you can find and replace text using the :substitute ( :s ) command. To run commands in Vim, you must be in normal mode, the default mode when starting the editor. To go back to normal mode from any other mode, just press the 'Esc' key.
Replace mode allows you replace existing text by directly typing over it. Before entering this mode, get into normal mode and put your cursor on top of the first character that you want to replace. Then press 'R' (capital R) to enter replace mode. Now whatever you type will replace the existing text.
You most likely have gdefault
set in your .vimrc
.
From :help gdefault
:
When on, the ":substitute" flag 'g' is default on. This means that
all matches in a line are substituted instead of one. When a 'g' flag
is given to a ":substitute" command, this will toggle the substitution
of all or one match.
If you are not setting this in your config you can see where it is set by issuing :verbose set gdefault?
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With