Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Change between backslash and forward slash
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Change between backslash and forward slash
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{TipImported |id=431 |previous=428 |next=432 |created=2003 |complexity=basic |author= |version=6.0 |rating=8/4 |category1=Windows |category2= }} In Windows, file paths use a backslash as a delimiter. This tip helps easily change between backslash and forward slash. For example, you could change <code>C:\data\doc.txt</code> to <code>C:/data/doc.txt</code>, or vice versa. ==Change to forward/back slashes== The following mappings (for your [[vimrc]]) allow easily changing slashes in the current line. <pre> 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> </pre> By default the <code><Leader></code> key is backslash, and <code><Bslash></code> is a way to refer to a backslash in a mapping, so by default these commands map <code>\/</code> and <code>\\</code> respectively. Press <code>\/</code> to change every backslash to a forward slash, in the current line. Press <code>\\</code> to change every forward slash to a backslash, in the current line. The mappings save and restore the search register (<code>@/</code>) so a previous search can be continued, if wanted. The <code>:noh</code> command is used to remove search highlighting (if enabled) to avoid search hits being highlighted after setting the search register. In the substitute command (<code>:s</code>), a colon (<code>:</code>) is used as a delimiter, so the slashes do not need to be escaped. The substitute flags (<code>ge</code>) cause all occurrences on the line to be substituted (<code>g</code>), and no error to be reported if no slash is found (<code>e</code>). ==Toggle forward/back slashes== The following script provides a command and a mapping to easily toggle slashes in the current line, or a range of lines. <pre> function! ToggleSlash(independent) range let from = '' for lnum in range(a:firstline, a:lastline) let line = getline(lnum) let first = matchstr(line, '[/\\]') if !empty(first) if a:independent || empty(from) let from = first endif let opposite = (from == '/' ? '\' : '/') call setline(lnum, substitute(line, from, opposite, 'g')) endif endfor endfunction command! -bang -range ToggleSlash <line1>,<line2>call ToggleSlash(<bang>1) noremap <silent> <F8> :ToggleSlash<CR> </pre> After sourcing the script, press F8 or enter <code>:ToggleSlash</code> to toggle between forward and back slashes. If no lines are selected, the command operates on the current line. If multiple lines are selected, the command operates on each selected line. A line is ignored if it does not contain a slash. By default, each line is handled independently: if the first slash in the line is a forward slash, all slashes in that line are changed to backslashes; if the first slash is a backslash, all slashes are changed to forward slashes. Enter <code>:ToggleSlash!</code> to force changes to each selected line to be the same: if the first slash found is a forward slash, all slashes in all selected lines are changed to backslashes; otherwise, all slashes in all selected lines are changed to forward slashes. ==See also== *[[VimTip432|Putting the current file on the Windows clipboard]] ==Comments==
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab