I often work with files full of filepaths inside. I want to be able to quickly select my filepath in Visual Mode and replace it with some other filepath.
For example I have the file like this:
balvadsd /mnt/Windows/Documents\ and\ Settings/stuff/file.exe blablabalba albla
/mnt/Windows/Documents\ and\ Settings/stuff/file.exe bla2 vslva 21
stuff foo bar dsad /mnt/Windows/Documents\ and\ Settings/stuff/file.exe
I need to
/mnt/Windows/Documents\ and\ Settings/stuff/file.exe/mnt/Windows/Documents\ and\ Settings/stuff/file.exe with some other path. The tricky bit is that the solutions like this one don't work with filepaths because of slashes, backslashes and dots. You can escape the regex metacharacters with \. So foo/bar becomes foo\/bar for your regex. Or you can use a different seperator like #. However you still need escape the .. You can avoid the need to escape . and other regex metacharacters by using \V for very nomagic. Using \V means all regex metacharacters now must be escaped meaning non escaped charters match their literal selves.
:%s#\V/mnt/Windows/Documents\ and\ Settings/stuff/file.exe#replacement#g
However all that escaping can become annoying. I usually use a visual star mapping and/or plugin. Meaning I visually select the text then press *. Then you can just do :%s//replacement/ or :%s##replacement# to make the replacement.
There are some nice Vimcast episodes by Neil Drew that talk about this:
You can take this idea further with the gn motion and the . command. See the following vimcast episode: Operating on search matches using gn.
For more help see:
:h /\V
:h gn
Instead of using slashes in the body of the substitute command you can use any other single-byte character, but not an alphanumeric character, '\', '"' or '|'. (in this example @).
%s@/mnt/Windows/Documents\\@/some/other/path
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With