Last active
December 13, 2015 23:59
-
-
Save pferreir/4995278 to your computer and use it in GitHub Desktop.
Killring Plugin for Sublime Text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
# Implements an Emacs-style kill ring feature | |
# Example bindings (.sublime-keymap) | |
# | |
#{ "keys": ["ctrl+k"], "command": "kill_area"}, | |
#{ "keys": ["alt+y"], "command": "yank_list"}, | |
#{ "keys": ["ctrl+y"], "command": "yank_last"}, | |
#{ "keys": ["ctrl+shift+k"], "command": "copy_area_ring"} | |
killring = [] | |
class KillAreaCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
global killring | |
killring.append(self.view.substr(self.view.sel()[0])) | |
edit = self.view.begin_edit() | |
for s in self.view.sel(): | |
self.view.erase(edit, s) | |
self.view.end_edit(edit) | |
class CopyAreaRingCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
global killring | |
killring.append(self.view.substr(self.view.sel()[0])) | |
class YankListCommand(sublime_plugin.TextCommand): | |
def _yank(self, result): | |
if result != -1: | |
edit = self.view.begin_edit() | |
for s in self.view.sel(): | |
self.view.erase(edit, s) | |
self.view.insert(edit, s.begin(), | |
killring[len(killring) - 1 - result]) | |
self.view.end_edit(edit) | |
def format_killring(self, kr): | |
return list(l.split('\n')[:5] for l in kr[::-1]) | |
def run(self, edit): | |
global killring | |
kr = self.format_killring(killring) | |
self.view.window().show_quick_panel(kr, self._yank) | |
class YankLastCommand(YankListCommand): | |
def run(self, edit): | |
global killring | |
self._yank(len(killring) - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment