Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 01-29-2015, 11:43 PM   #1
LQ Newbie
Registered: Jan 2015
Posts: 2

Rep: Reputation: Disabled
Post Search and replace strings with file paths in vim

Hello all,

I have a config file containing file paths.
I am wondering how one would go about searching and replacing the strings in vim.

Here is an excerpt of my file I am modifying in vim, so you guys can get a better idea about the situation:
Host america
    IdentityFile ~/.ssh/id_rsa.local
Host germany
    IdentityFile ~/.ssh/id_rsa.local
Host canada
    IdentityFile ~/.ssh/id_rsa.local
Host japan
    IdentityFile ~/.ssh/id_rsa.local
Host amsterdam
    IdentityFile ~/.ssh/id_rsa.local
I understand the the general format for a [s]ubstitute is:
:%s/<search string>/<replacement string>/g
I want to replace the line with the file path, so naturally I tried:
:%s,IdentityFile ~/.ssh/id_rsa.local,this is a test,gc
I changed the delimiter to ",", so that I wouldn't have to mess with correctly escaping the slashes.
After executing the command, vim unfortunately comes back with a "E486: Pattern not found: IdentityFile ~/.ssh/id_rsa.local" result. This is certainly not correct, since we can see the string is in the file above.

I assume that I need to escape something else, but I am not sure what or how. Can anyone shed some light on this issue?


Last edited by geeksquads; 01-29-2015 at 11:46 PM. Reason: Being more specific in my title
Old 01-30-2015, 12:42 AM   #2
Senior Member
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
:%s,IdentityFile ~/.ssh/id_rsa.local,this is a test,gc
There is an error in your vim regex. You should escape '~' character.

Try instead:

:%s,IdentityFile \~/.ssh/id_rsa.local,this is a test,gc
Old 01-30-2015, 02:03 AM   #3
LQ Newbie
Registered: Jan 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Hi veerain,

I can't believe I didn't try that. I was thinking it was going to be a lot more complicated.
I totally thought it would be an issue related with something regarding "." or something. I guess not.

It works now. Thanks again, veerain!


On a sidenote, after looking up what "~" actually means, it seems to be a backreference to a previous substitute string. As to how you actually use it is beyond me... If anyone is in the mood to add more details about it, I'd love to learn a bit more.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Search and replace problem with VIM jstephens84 Programming 2 02-18-2010 08:34 PM
Help with Regular Expression in VIM search and replace vijay_babu1981 Linux - Newbie 8 10-22-2009 07:49 AM
vim search and replace only one line hakmed Programming 5 11-07-2006 07:13 PM
vim: search pattern / replace with file naflan Programming 5 01-07-2006 03:16 PM
vim search and replace - split line phreak7t7 Programming 2 08-16-2004 05:02 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration