Sublime Forum

How do I get a file's name for use in a Search/Replace?

#1

I have a string in a bunch of files I’d like to replace with the file’s name. I’m not super experienced with RegEx, and not even sure if that’s the way to go or if it’s possible.

For example, if the string in the file is called “mystring1” and the filename is called “allstrings.txt” I’d like to replace “mystring1” with “allstrings”. File extension not needed.

Any help would be appreciated!

1 Like

#2

You can’t retrieve the file’s filename within the find in files functionality that ST provides.
Once you have a selection of all the strings within a file that you want to replace, you could execute import os; view.run_command("insert", {"characters": os.path.splitext(os.path.basename(view.file_name()))[0]}) to replace that with the file name, but you would have to do that for each file.
The only 100% automatic option would be to write an external script that parses the files and replaces its contents by itself.

2 Likes