Vim Tips Wiki
(Move categories to tip template)
(Remove html character entities)
Line 15: Line 15:
   
 
<pre>
 
<pre>
:nnoremap &lt;silent&gt; f/ :let tmp=@/&lt;CR&gt;:s:\\:/:ge&lt;CR&gt;:let @/=tmp&lt;CR&gt;
+
:nnoremap <silent> f/ :let tmp=@/<CR>:s:\\:/:ge<CR>:let @/=tmp<CR>
:nnoremap &lt;silent&gt; f&lt;Bslash&gt; :let tmp=@/&lt;CR&gt;:s:/:\\:ge&lt;CR&gt;:let @/=tmp&lt;CR&gt;
+
:nnoremap <silent> f<Bslash> :let tmp=@/<CR>:s:/:\\:ge<CR>:let @/=tmp<CR>
 
</pre>
 
</pre>
   
Line 27: Line 27:
 
In the substitute command (<tt>:s</tt>), a colon (<tt>:</tt>) is used as a delimiter, so the slashes do not need to be escaped. The substitute flags (<tt>ge</tt>) cause all occurrences on the line to be substituted (<tt>g</tt>), and no error to be reported if no slash is found (<tt>e</tt>).
 
In the substitute command (<tt>:s</tt>), a colon (<tt>:</tt>) is used as a delimiter, so the slashes do not need to be escaped. The substitute flags (<tt>ge</tt>) cause all occurrences on the line to be substituted (<tt>g</tt>), and no error to be reported if no slash is found (<tt>e</tt>).
   
You might prefer to use "<tt>&lt;Leader&gt;</tt>" rather than "<tt>f</tt>" in the mappings above. By default, your leader key is backslash.
+
You might prefer to use "<tt><Leader></tt>" rather than "<tt>f</tt>" in the mappings above. By default, your leader key is backslash.
   
 
See [[VimTip432]] for a related tip.
 
See [[VimTip432]] for a related tip.

Revision as of 08:42, 29 September 2008

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.