Copy file name and path in Sublime - windows

I have tried to find command, plugin or anything that can do this. Essentially I need two keyboard shortcuts. One to copy current file path(only current directory, not file name). Another to copy only file name(with extension eg; "test.txt" ).

You can do this with a simple plugin such as this (see this video on using plugins in Sublime if you're not sure how to put it in place):
import sublime
import sublime_plugin
import os
class CopyFilePathCommand(sublime_plugin.TextCommand):
def run(self, edit, name=False):
path, filename = os.path.split(self.view.file_name())
sublime.set_clipboard(filename if name else path)
self.view.window().status_message(
'Copied file %s' % ("name" if name else "path"))
def is_enabled(self, name=False):
return self.view.file_name() is not None
This implements a command named copy_file_path which will copy the path of the current file to the clipboard by default, switching to copying the name of the file instead if you pass true for the value of the name argument.
A message is displayed in the status bar to tell you which thing was copied, and the command will disable itself for any file that hasn't been saved to disk yet (since it would thus have no path to copy).
Sample key bindings might look something like the following:
{ "keys": ["ctrl+shift+alt+p"], "command": "copy_file_path", "args": {"name": false}},
{ "keys": ["ctrl+shift+alt+f"], "command": "copy_file_path", "args": {"name": true}},

Related

How to associate .install file extension and PKGBUILD file name with shell scripts in VSCode?

In Visual Studio Code I have tried to associate *.install (i.e., files with the extension .install) and PKGBUILD files with shell script syntax highlighting, using:
// Place your settings in this file to overwrite the default settings
{
"file.associations": {
"PKGBUILD": "shell",
"*.install": "shell-script",
"*.desktop": "ini"
}
}
but it hasn't worked (for either file name/extension), that is, neither file name/extension is getting syntax highlighting from shell. I have tried changing shell/shell-script with shellscript, after looking at the shellscript package's source code (https://github.com/Microsoft/vscode/blob/master/extensions/shellscript/package.json).
You're missing the "s" in "files":
{
"files.associations": {
"PKGBUILD": "shellscript",
"*.install": "shellscript",
"*.desktop": "ini"
}
}
Once the setting is corrected, you can use any of the aliases in the file you linked https://github.com/Microsoft/vscode/blob/master/extensions/shellscript/package.json

How to (easily) get current file path in Sublime Text 3

How to (easily) get current file path in Sublime Text 3
I don't often use ST console (I used it only once to install package manager), but I suppose it could be good way to :
get current file path like some kind pwd command.
But it doesn't work.
Does anyone know an easy way to get current file path?
to clipboard : better not a strict objective in the answer
not necessary by ST command, maybe package?
Right click somewhere in the file (not on the title tab) --> Copy file path
If you don't want to use the mouse, you could set up a keyboard shortcut as explained here https://superuser.com/questions/636057/how-to-set-shortcut-for-copy-file-path-in-sublime-text-3
To easily copy the current file path, add the following to Key Bindings - User:
{ "keys": ["ctrl+alt+c"], "command": "copy_path" },
Source
Key Bindings - User can be opened via the command palette (command + p on OSX)
Easy to understand using image. On Right Click you will get this.
Transcribed code in image for convenience:
import sublime, sublime_plugin, os
class CopyFilenameCommand(sublime_plugin.TextCommand):
def run(self, edit):
if len(self.view.file_name()) > 0:
filename = os.path.split(self.view.file_name())[1]
sublime.set_clipboard(filename)
sublime.status_message("Copied file name: %s" % filename)
def is_enabled(self):
return self.view.file_name()... # can't see
Mac OS X - Sublime Text 3
Right click > Copy File Path
A lot of these answers involve touching the mouse. Here's how to do get the path without any mouse clicks using SideBarEnhancements
Install SideBarEnhancements using PackageControl.
Click super + shift + P to open the command palette
In the command palette begin typing path until you see File: Copy Path
Select File: Copy Path
Now the path to file you are working in is copied into your clipboard.
There is a Sublime Package which gives your current file location inside a status bar. I just cloned them directly to my /sublime-text-3/Packages folder.
git clone git#github.com:shagabutdinov/sublime-shell-status.git ShellStatus;
git clone git#github.com:shagabutdinov/sublime-status-message.git StatusMessage;
You have to check/read the description on GitHub. Even it is listed in package control it would not install properly for me. You can actually edit the shell output as you want. If you have the right skills with python/shell.
Looks like this (Material Theme)
If you're like me and always click on items in the sidebar just to realize that copying the path only works when clicking in the editor area, have a look at the SideBarEnhancements package. It has a huge bunch of options to copy file paths in a variety of different ways.
Installation is available via Package Control (despite the webpage only mentions installation via manual download).
Note: The package “sends basic, anonymous statistics”. The webpage explains how to opt out from that.
Go to this link. The code in the link is given by robertcollier4.
Create a file named CpoyFileName.py or whatever you like with .py extension.
Save the file in Sublime Text 3\Packages\User folder. Then paste the above given key bindings in your Preferences: Key Bindings file.
Now, you can use the specified key bindings to copy just filename or total (absolute) filepath.
Please note that the filename or filepath do contain file extension.
Fastest Solution ( No Packages Needed + Comprehensive ):
Folder Path:
Folder in "Sidebar"
Right Click
"Find In Folder"
"Where" field contains everything you need
File Path:
File in current "Tab"
Right Click
"Copy File Path"

Lowercasing and underscoring text in Sublime Text

Hi I wondering how I could make a snippet in Sublime Text for doing this.
selected text:
Lorem Ipsum 200
result (in lowercase):
lorem_ipsum_200
This can be done with a macro. First, however, you'll need to install the Case Conversion plugin via Package Control in order to get the convert_to_snake command.
Open a new file in Sublime (use JSON syntax if you want) with the following contents:
[
{
"command": "lower_case", "args": null
},
{
"command": "convert_to_snake", "args": null
}
]
Save the file in your Packages/User directory (Packages can be located by selecting Preferences -> Browse Packages...) as lower_snake.sublime-macro.
Next, select Preferences -> Key Bindings-User and add the following entry. If the file is empty, surround the keybinding with square brackets [ ].
{ "keys": ["ctrl+alt+shift+l"], "command": "run_macro_file", "args": {"file": "res://Packages/User/lower_snake.sublime-macro"} }
Save the file, highlight the text you want to modify, and press CtrlAltShiftL to run the macro. You can change the keybinding to whatever you want, just make sure it doesn't conflict with anything else on your system.
Good luck!

How to record a macro in sublime text 2?

I am using sublime text 2 and want to record a macro for uploading the current file vie transmit docsend (super+u) and saving it locally (super+s).
So I start to record a macro ctrl+q and see the status notification in the footer
Starting to record macro ...
Then I hit super+s and it saves and the upload it vie super+u. After hitting ctrl+q for stopping macrorecording the footer says
Stopping recording macro
But I do not have the Option in the menu "Tools/Playback macro" and when I hit save Macro it doesnt do anything.
Any Idea?
PS: I am working on OS X and I have no directory
/Library/Application\ Support/Sublime\ Text\ 2
Have I to create it as admin?
Thanks a lot
Now I realized that I dont need the folder /Library... but ~/Library...
But after saving the File ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/transmit-upload-and-save.sublime-macro
[
{"command": "save"},
{"command": "transmit_docksend"}
]
and adding the following to user shortcuts
[
{ "keys": ["super+u"], "command": "run_macro_file", "args": {"file": "Packages/User/transmit-upload-and-save.sublime-macro"} }
]
it does not save but uploads ... whats wrong?
To simply playback last recorded Macro press Ctrl+Shift+q.
In Sublime Text2 Macros are not saved to file by default they reside in the macro buffer instead. More here
The reason it's uploading is because your key is only defined as [super+u] which is for upload - it should be [super+s] for save.
Hope that helps

Sublime Text 2 - Default Document Type

Is there a way to set a default document type when saving a NEW FILE?
I created several new files and I want to have a default value of .txt when saving a NEW FILE.
Create a new plugin Tools > Developer > New Plugin...
Paste this in:
import sublime, sublime_plugin
class EverythingIsPowerShell(sublime_plugin.EventListener):
def on_new(self, view):
view.set_syntax_file('Packages/PowerShell/Support/PowershellSyntax.tmLanguage')
Save and call it NewTabSyntax.py. New tabs will now default to Powershell.
You can change the syntax to whatever you prefer. To find out the "path" of a particular syntax, simply open a file of that syntax, open the console (View > Show Console) and type:
view.settings().get('syntax')
This plugin does it:
https://github.com/spadgos/sublime-DefaultFileType
seems pretty great.
Edit:
Ok, two things, there currently seems to be a small bug so the text file syntax is not being correctly picked up due to the whitespace in the filename. In addition you need to set the "use_current_file_syntax" to false, (otherwise the new file will default to whatever filetype you have open already when you hit Ctrl-N)... So the fix/workaround is this:
Put the following code in: Packages/User/default_file_type.sublime-settings
{ "default_new_file_syntax": "Packages/Text/Plain_text.tmLanguage",
"use_current_file_syntax": false }
NOTE THE UNDERSCORE.
Next, find the "Plain text.tmLanguage" file and copy and rename it (in the same folder) as "Plain_text.tmLanguage". [be sure to copy/duplicate it, do not just rename it, as it may have dependancies]
Restart, just to be sure, and this should do the trick.
Also note this plugin only works for new files created with Ctrl-N.
Working after these steps:
1.Uninstalled
2.Installed using Package Control
3.Test using default install (type Jave) <-- worked
4.Copy and Renamed file Sublime Text 2\Packages\Text\Plain text.tmLanguage > Sublime Text 2\Packages\Text\Plain_text.tmLanguage
5.Changed file Sublime Text 2\Packages\Default File Type\default_file_type.sublime-settings >
`{ "default_new_file_syntax": "Packages/Text/Plain_text.tmLanguage", "use_current_file_syntax": true }`
-- All working.
I did not need to copy any files into the 'Packages/User' folder
#fraxel _ Thanks for all the help and quick response.

Resources