VIM search and replace special characters

Published by Torry Crass on

vim is a wonderful text editor. Wait. What? emacs? who uses that anyhow… 😉

I recently needed to do a quick search and replace on ALL punctuation within a file and decided to use vim in combination with regex to complete the task and post a quick how-to here.

First, open the text file with vim as you normally would (kinda required right?)

Now use the vim command line by invoking the colon :.

Now use the regex search (see command below) and note that you need to use the \ escape character or you’re going to end up with everything replaced vs just your punctuation (yes, that is the voice of experience).

:%s/\./\_/g

In the above example all periods are replaced with underscores while leaving all other aspects of the file untouched. Simple right?

Categories: Helpful Commands

1 Comment

Torry Crass · August 20, 2019 at 12:48 pm

A great resource that I found for using regex search and replace in vim can be found here: https://vim.fandom.com/wiki/Search_and_replace

Leave a Reply