Skip to content

Instantly share code, notes, and snippets.

@mvalipour
Created July 15, 2017 18:37
Show Gist options
  • Save mvalipour/80071a63125776bd75e6fbb0a0693856 to your computer and use it in GitHub Desktop.
Save mvalipour/80071a63125776bd75e6fbb0a0693856 to your computer and use it in GitHub Desktop.
Hacky code to generate new file based on text selection in atom
toSnakeCase = s => s.replace(/(?:^|\.?)([A-Z])/g, (x,y) => "_" + y.toLowerCase()).replace(/^_/, "")
editor = atom.workspace.getActiveTextEditor();
path = editor.getPath();
fileName = editor.getFileName();
fileExt = fileName.split('.').pop();
dir = path.substring(0, path.length - fileName.length);
selectedText = editor.getSelectedText();
newPath = dir + toSnakeCase(selectedText) + '.' + fileExt;
atom.workspace.open().then(e => e.saveAs(newPath));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment