Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save colinta/1590661 to your computer and use it in GitHub Desktop.
Save colinta/1590661 to your computer and use it in GitHub Desktop.
A version of the Sublime Text 2 plugin at http://www.sublimetext.com/forum/viewtopic.php?f=5&t=2260&start=0 that makes for TextMate-like clipboard history

Clipboard Manager plugin for Sublime Text 2

A version of the Sublime Text 2 plugin at http://www.sublimetext.com/forum/viewtopic.php?f=5&t=2260&start=0 that makes for TextMate-like clipboard history.

Originally written by AJ Palkovic (ajpalkovic), modified by Martin Aspeli (optilude), and further modified and packaged for Package Control by Colin Thomas-Arnold (colinta)

Installation

  1. Using Package Control, install "Clipboard Manager"

Or:

  1. Open the Sublime Text 2 Packages folder

    • OS X: ~/Library/Application Support/Sublime Text 2/Packages/
    • Windows: %APPDATA%/Sublime Text 2/Packages/
    • Linux: ~/.Sublime Text 2/Packages/
  2. clone this repo

Commands

clipboard_manager_cut: Self Explanatory

clipboard_manager_copy: Self Explanatory

clipboard_manager_paste: Self Explanatory. Options: indent (default: False): Determines whether to use paste or paste_and_indent built-in command.

clipboard_manager_next_and_paste: Goes to the next entry in the history and pastes it. Options: indent (default: False)

clipboard_manager_previous_and_paste:Goes to the previous entry in the history and pastes it. Options: indent (default: False)

clipboard_manager_next: Goes to the next entry in the history, but doesn't paste. (the content will appear as a status message)

clipboard_manager_previous: Goes to the previous entry in the history, but doesn't paste. (the content will appear as a status message)

clipboard_manager_choose_and_paste: Shows the clipboard history in a "quick panel".

import sublime
import sublime_plugin
class HistoryList(list):
"""List type for storing the history - fairly
inefficient, but useful.
"""
SIZE = 256
index = 0
def append(self, item, update_index=True):
self.insert(0, item)
if update_index:
self.index = 0
if len(self) > self.SIZE:
del self[self.SIZE:]
def current(self):
if len(self) == 0:
return None
return self[self.index]
def next(self):
if self.index > 0:
self.index -= 1
def previous(self):
if self.index < len(self) - 1:
self.index += 1
HISTORY = HistoryList()
class ClipboardManagerBase(sublime_plugin.TextCommand):
def update_clipboard(self, content):
sublime.status_message("Set Clipboard to " + content)
sublime.set_clipboard(content)
def next(self):
HISTORY.next()
self.update_clipboard(HISTORY.current())
def previous(self):
HISTORY.previous()
self.update_clipboard(HISTORY.current())
def appendClipboard(self):
# append the contents of the clipboard to the history if it is unique
if not self.onCurrent():
HISTORY.append(sublime.get_clipboard())
def onCurrent(self):
return sublime.get_clipboard() == HISTORY.current()
class ClipboardManagerPaste(ClipboardManagerBase):
def run(self, edit, indent=False):
# If the user pastes something that was copied in a different program, it will not be in sublime's buffer, so we attempt to append every time
self.appendClipboard()
if indent:
self.view.run_command('paste_and_indent')
else:
self.view.run_command('paste')
class ClipboardManagerCut(ClipboardManagerBase):
def run(self, edit):
# First run sublime's command to extract the selected text.
# This will set the cut/copy'd data on the clipboard which we can easily steal without recreating the cut/copy logic.
self.view.run_command('cut')
self.appendClipboard()
class ClipboardManagerCopy(ClipboardManagerBase):
def run(self, edit):
self.view.run_command('copy')
self.appendClipboard()
class ClipboardManagerNext(ClipboardManagerBase):
def run(self, edit):
self.next()
class ClipboardManagerNextAndPaste(ClipboardManagerBase):
def run(self, edit, indent=False):
self.next()
if indent:
self.view.run_command('paste_and_indent')
else:
self.view.run_command('paste')
class ClipboardManagerPrevious(ClipboardManagerBase):
def run(self, edit):
self.previous()
class ClipboardManagerPreviousAndPaste(ClipboardManagerBase):
def run(self, edit, indent=False):
self.previous()
if indent:
self.view.run_command('paste_and_indent')
else:
self.view.run_command('paste')
class ClipboardManagerChooseAndPaste(ClipboardManagerBase):
def run(self, edit):
def on_done(idx):
if idx >= 0:
HISTORY.index = idx
self.update_clipboard(HISTORY.current())
self.view.run_command('paste')
def format(line):
return line.replace('\n', '$ ')[:64]
lines = map(format, HISTORY)
sublime.active_window().show_quick_panel(lines, on_done)
[
{ "keys": ["ctrl+x"], "command": "clipboard_manager_cut" },
{ "keys": ["ctrl+c"], "command": "clipboard_manager_copy" },
{ "keys": ["ctrl+v"], "command": "clipboard_manager_paste", "args": { "indent": true } },
{ "keys": ["ctrl+ctrl+v"], "command": "clipboard_manager_paste", "args": { "indent": false } },
{ "keys": ["ctrl+alt+v"], "command": "clipboard_manager_next_and_paste" },
{ "keys": ["ctrl+shift+v"], "command": "clipboard_manager_previous_and_paste" },
{ "keys": ["ctrl+pagedown"], "command": "clipboard_manager_next" },
{ "keys": ["ctrl+pageup"], "command": "clipboard_manager_previous" },
{ "keys": ["ctrl+alt+v"], "command": "clipboard_manager_choose_and_paste" }
]
[
{ "keys": ["super+x"], "command": "clipboard_manager_cut" },
{ "keys": ["super+c"], "command": "clipboard_manager_copy" },
{ "keys": ["super+v"], "command": "clipboard_manager_paste", "args": { "indent": true } },
{ "keys": ["super+ctrl+v"], "command": "clipboard_manager_paste", "args": { "indent": false } },
{ "keys": ["super+alt+v"], "command": "clipboard_manager_next_and_paste" },
{ "keys": ["super+shift+v"], "command": "clipboard_manager_previous_and_paste" },
{ "keys": ["super+pagedown"], "command": "clipboard_manager_next" },
{ "keys": ["super+pageup"], "command": "clipboard_manager_previous" },
{ "keys": ["super+alt+ctrl+v"], "command": "clipboard_manager_choose_and_paste" }
]
[
{ "keys": ["ctrl+x"], "command": "clipboard_manager_cut" },
{ "keys": ["ctrl+c"], "command": "clipboard_manager_copy" },
{ "keys": ["ctrl+v"], "command": "clipboard_manager_paste", "args": { "indent": true } },
{ "keys": ["ctrl+ctrl+v"], "command": "clipboard_manager_paste", "args": { "indent": false } },
{ "keys": ["ctrl+alt+v"], "command": "clipboard_manager_next_and_paste" },
{ "keys": ["ctrl+shift+v"], "command": "clipboard_manager_previous_and_paste" },
{ "keys": ["ctrl+pagedown"], "command": "clipboard_manager_next" },
{ "keys": ["ctrl+pageup"], "command": "clipboard_manager_previous" },
{ "keys": ["ctrl+alt+v"], "command": "clipboard_manager_choose_and_paste" }
]
[
{
"caption": "Clipboard Manager: Cut",
"command": "clipboard_manager_cut"
},
{
"caption": "Clipboard Manager: Copy",
"command": "clipboard_manager_copy"
},
{
"caption": "Clipboard Manager: Paste",
"command": "clipboard_manager_paste"
},
{
"caption": "Clipboard Manager: Next & Paste",
"command": "clipboard_manager_next_and_paste"
},
{
"caption": "Clipboard Manager: Previous & Paste",
"command": "clipboard_manager_previous_and_paste"
},
{
"caption": "Clipboard Manager: Next",
"command": "clipboard_manager_next"
},
{
"caption": "Clipboard Manager: Previous",
"command": "clipboard_manager_previous"
},
{
"caption": "Clipboard Manager: Choose & Paste",
"command": "clipboard_manager_choose_and_paste"
}
]
@colinta
Copy link
Author

colinta commented Mar 7, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment