Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim global replace (line)

Tags:

vim

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?

like image 286
yawmark Avatar asked Apr 07 '11 13:04

yawmark


People also ask

How do I do a global replace in vi?

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.

How do I replace text in Vim?

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.

What command is used to overwrite a single word in the Vim editor?

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.


1 Answers

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?.

like image 127
Randy Morris Avatar answered Nov 04 '22 10:11

Randy Morris