Vim Tips Wiki
Register
Advertisement
Tip 431 Printable Monobook Previous Next

created 2003 · complexity basic · version 6.0


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> <Leader>/ :let tmp=@/<Bar>s:\\:/:ge<Bar>let @/=tmp<Bar>noh<CR>
:nnoremap <silent> <Leader><Bslash> :let tmp=@/<Bar>s:/:\\:ge<Bar>let @/=tmp<Bar>noh<CR>

By default the <Leader> key is backslash, and <Bslash> is a way to refer to a backslash in a mapping, so by default these commands map \/ and \\ respectively.

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

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

The mappings save and restore the search register (@/) so a previous search can be continued, if wanted. The :noh command is used to remove search highlighting (if enabled) to avoid search hits being highlighted after setting the search register.

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).

See also

Comments

Advertisement