Sublime Forum

I could use a plugin skeleton with example of clipboard mgmt

#1

I’m in the need of a feature where I have something like this in my editor:

db.kittens.aggregate(
    {$unwind : "kitty.name"},
    {$sort : {"kitty.age" : -1}}
])

This is just example code for showing formatting

I want to be able to mark the sentence, click on a keyboard shortcut and have the marked sentence filtered without newlines and tabs and double-spaces and added to the clipboard. I don’t want the sentence in the editor to be changed.

I read throw the “how to create a plugin” document but it’s a bit overwhelming at the moment, so if someone has an example of something similar to the request above it would really kick start me.

Thanks alot in advance,

Joche

0 Likes

#2

sublimetext.com/docs/2/api_reference.html

There is a set_clipboard method available, so i guess you could hook into it.

Later edit: i re-read your request, so here is a starting point:

import sublime, sublime_plugin, re;

class CopyAndProcessCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    view = self.view
    selection = view.sel() # get selection coords
    selection = view.substr( selection[0] ) # get selected text

    processedSelection = re.sub( "(\r|\n|\t)", "", selection ) # removes all new lines and tabs
    sublime.set_clipboard( processedSelection ) # set clipboard
0 Likes

#3

Thank you a lot - this really got me started. Actually, your code worked exactly as I was intending. Thanks again! For other readers I can also tip that in “Preferences -> Key bindings - user” you can add a key binding if you don’t prefer to run the plugin from console.

{ "keys": "ctrl+shift+c"], "command": "copy_and_process" }
]
0 Likes

#4

[quote=“Joche”]
I want to be able to mark the sentence, click on a keyboard shortcut and have the marked sentence filtered without newlines and tabs and double-spaces and added to the clipboard. I don’t want the sentence in the editor to be changed.[/quote]

The proposed code by iamntz does not remove double (or multiple spaces), this will:

    processedSelection = ' '.join(selection.split()) # replaces all whitspaces (also multiple) with a single space

Not sure about performance, but it should be faster as well.

If you were to mimimc the default copy behaviour with multiple selections, try this:

import sublime
import sublime_plugin


class CopyAndProcessCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        lines = ]
        for r in self.view.sel():
            text = self.view.substr(r)

            # replaces all whitspaces (also multiple) with a single space
            lines.append(' '.join(text.split()))

        sublime.set_clipboard('\n'.join(lines))

Or, if you’re fancy:

import sublime
import sublime_plugin


class CopyAndProcessCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        # replaces all whitspaces (also multiple) with a single space
        sublime.set_clipboard('\n'.join(' '.join(self.view.substr(r).split())
                              for r in self.view.sel()))
0 Likes