20091201 search and replace globally in vi - plembo/onemoretech GitHub Wiki

title: Search and replace globally in vi link: https://onemoretech.wordpress.com/2009/12/01/search-and-replace-globally-in-vi/ author: lembobro description: post_id: 211 created: 2009/12/01 18:56:37 created_gmt: 2009/12/01 18:56:37 comment_status: open post_name: search-and-replace-globally-in-vi status: publish post_type: post

Search and replace globally in vi

I have a rule that once I’ve been asked how to do something more than twice, the solution gets posted here.

To do a global search and replace in vi, use the following at the colon prompt:

:%s/oldtext/newtext/gi

The above will search and replace “oldtext” with “newtext ” throughout the document. The “%s” switch tells it to search every line in the file; “/g” says search the entire line; and “/i” says search case-insensitively.

To search only the current line, use “s” instead of “%s”.

Copyright 2004-2019 Phil Lembo