Vim Tips Wiki
Advertisement
Tip 431 Printable Monobook Previous Next

created March 3, 2003 · complexity basic · author maxiangjiang/salmanhalim · version 5.7


In Windows, file paths use a backslash as a delimiter. The mappings below allow you to easily change between backslash and forward slash. For example, you could change C:\data\doc.txt to C:/data/doc.txt, or vice versa.

:nnoremap <silent> f/ :let tmp=@/<CR>:s:\\:/:ge<CR>:let @/=tmp<CR>
:nnoremap <silent> f<Bslash> :let tmp=@/<CR>:s:/:\\:ge<CR>:let @/=tmp<CR>

Press f/ to change every backslash to a forward slash, in the current line.

Press f\ to change every forward slash to a backslash, in the current line.

The mappings save and restore the search register (@/) so you can continue a previous search, if wanted.

In the substitute command (:s), a colon (:) is used as a delimiter, so the slashes do not need to be escaped. The substitute flags (ge) cause all occurrences on the line to be substituted (g), and no error to be reported if no slash is found (e).

You might prefer to use "<Leader>" rather than "f" in the mappings above. By default, your leader key is backslash.

See VimTip432 for a related tip.

Comments

The original tip (see the link "Imported from" above), also has a more complex procedure to toggle between backslash and forward slash, with a single mapping.


Advertisement