Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional replace in vim

Tags:

vim

vi

I'd like do use vim search-and-replace to replace all " with ' and vice-versa. Is there a way to achieve this in one step? I'm thinking of something like this:

:s/\("\|'\)/\1=="?':"/

Where of course the \1=="?':"-part is something that works in vim.

Thanks in advance!

like image 867
Vince Avatar asked Jun 27 '13 08:06

Vince


2 Answers

Another approach (that's more suited to scripting) is to use the built-in tr() function. To apply it on the buffer, getline() / setline() is used:

:call setline('.', tr(getline('.'), "'\"", "\"'"))
like image 86
Ingo Karkat Avatar answered Nov 15 '22 03:11

Ingo Karkat


power of unix tools ;)

:%!tr "'\"" "\"'"

like image 3
ernix Avatar answered Nov 15 '22 04:11

ernix