Sublime Forum

How to modify backslash for Windows

#1

Hi, I’m using Windows and everytime I paste in a path, I have to include extra backslash because it gives me an error if I don’t, how do I modify it so every time I paste in a file path, sublime text will automatically include double backslash instead of a single backslash?

For example:

C:\Users\some_name\

to

C:\\Users\\some_name\\

0 Likes

#2

There’s not a setting for that, but you could create a simple plugin such as the below that would help with this (see this video if you’re not sure how to use plugins):

import sublime
import sublime_plugin


class PastePathCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        text = sublime.get_clipboard().replace('\\', '\\\\')
        self.view.run_command('insert', {'characters':  text})

This implements a command named paste_path that will insert the text from the clipboard, replacing all of the backslashes with a double backslash. You could then bind that to a specific key and use that key when you’re pasting a path in.

0 Likes

#3

I guess you’ll first have to inspect the clipboard contents to make sure it’s a windows path before going forward with the replace operation ? Otherwise you’d be replacing the single \ with \\ on a clipboard content, that may not be a path at all, but something different entirely.

0 Likes

#4

The implication above is that you bind the plugin to a dedicated key that you use when you know you’re pasting a path. You could in theory detect if the clipboard looks like a path, but then you run into issues with a situation where you have a path and you actually want to paste it normally, which would also be blocked.

1 Like