I use Sublime Text to edit my Snippets. One of the nice features is that you can triple-click a line, and it will select the whole lie. By holding down Command (super) you can select multiple, non-contiguous lines, and then copy and paste them somewhere else. Unfortunately, the triple-click also selects the newline, and so when you paste, you get lots of additional blank lines. This “package” will remove blank lines from the clipboard, so I can paste without the additional blanks.

The Package

Calling it a package is a bit of a stretch. It’s a single file, and it’s not even a plugin. It’s just some Python that uses the API to manipulate the clipboard.

I’m sure I could have written this in a more elegant way, but this works…

import sublime
import sublime_plugin
import logging


# This will cut lines and remove empty blank lines.
#
# I use this so I can triple-click non-contiguous lines (e.g in Snippets)
# and then paste them without blank lines between them - normally the
# triple-click also copies the implied newline at the end of the line.
class CutWithoutNewlineCommand(sublime_plugin.TextCommand):
    def run(self, _: sublime.Edit) -> None:

        new = ""
        logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s - %(message)s')
        logging.debug('Running CutWithoutNewlineCommand')
        if self.view.has_non_empty_selection_region():
            self.view.run_command('cut')


            # Note: This will fail if the clipboard size exceeds max_size (16777216)
            # see: https://www.sublimetext.com/docs/api_reference.html
            for line in sublime.get_clipboard().split("\n"):
                logging.debug("Line: %s" % line)
                line = line.rstrip()
                if line == "":
                    #logging.debug("Skipping blank line: %s" % line)
                    pass
                else:
                    #logging.debug("Addind: %s ---" % line)
                    new +=line+"\n"

            sublime.set_clipboard(new)
            #logging.debug('Updated clipboard.')

        return

Installation

  • Put the in Library/Application Support/Sublime Text/Packages/User/cut_without_newline.py

  • Add the following to your Default (OSX).sublime-keymap. Hint: Sublime Text -> Preferences -> Key Bindings:

{ "keys": ["super+alt+x"], "command": "cut_without_newline" },
  • To use, select some text, and then press super-alt-x to cut the text without the newline.
    • For debugging, View -> Show Console will show the logging output. Uncomment the logging.debug lines to see more detail.