Sublime Forum

Keyboard short cut to append string to a static string

#1

Is there a method to append a string to a static string via a keyboard shortcut and then send all of the string to the clipboard or even directly to a browser?

I have the following keyboard shortcut "keys": "ctrl+f2"], "command": "side_bar_copy_name" }, to copy the filename of the page I am editing.

An editor of epubs, I use that data to append to a variable in a url I have in my browser

http://localhost/epubtesting/index.php?file=<pasting in name of file here>

Since I use Linux, my file naming standards are already to use files with no spaces, either camel case or underscores in place of spaces.

Is there a simple means to add an argument (if that is the right term) so a shortcut would look something like?:

 "keys": "ctrl+f2"], "command": "side_bar_copy_name",  args: {"http://localhost/epubtesting/index.php?file=<append name of file here> "} },
0 Likes

#2

nudge

0 Likes

#3

Not currently, no. But you can:

  1. Ask the author of the sidebarenhancements package to add that parameter, or
  2. write a custom command that does this, such as the following:

[code]import sublime
import sublime_plugin

import urllib.parse

class CopyFileNameAsUrlCommand(sublime_plugin.TextCommand):
def run(self, edit, fmt):
if self.view.file_name():
text = fmt % urllib.parse.quote(self.view.file_name())
sublime.set_clipboard(text)

[/code]

Then add a key binding like follows:

{ "keys": "ctrl+f2"], "command": "copy_file_name_as_url",  args: {"fmt": "http://localhost/epubtesting/index.php?file=%s"} }
0 Likes

#4

Thank you for your response. Your code works perfectly.

For anyone that finds this thread there is a small correction for the keyboard binding. Place quotes around “args”

{ "keys": "ctrl+f2"], "command": "copy_file_name_as_url",  "args": {"fmt": "http://localhost/epubtesting/index.php?file=%s"} }

The glaring highlighting Sublime provides in the keymap pointed that error out.

[quote=“FichteFoll”]Not currently, no. But you can:

  1. Ask the author of the sidebarenhancements package to add that parameter, or
  2. write a custom command that does this, such as the following:

[code]import sublime
import sublime_plugin

import urllib.parse

class CopyFileNameAsUrlCommand(sublime_plugin.TextCommand):
def run(self, edit, fmt):
if self.view.file_name():
text = fmt % urllib.parse.quote(self.view.file_name())
sublime.set_clipboard(text)

[/code]

Then add a key binding like follows:

{ "keys": "ctrl+f2"], "command": "copy_file_name_as_url",  args: {"fmt": "http://localhost/epubtesting/index.php?file=%s"} }
0 Likes