If you want to do more than one of them at a time as here, I think you need a plugin or package of some sort for that. To do it with regular copy/paste each section would need to be a single line, so it would need to have the heading and the body all concatenated together by you on copy, which you’d have to fix after you paste.
There may be a package on PackageControl that can help with this, but something like this plugin might help you or be a starting point for your own modifications (see this video if you’re not sure how to install plugins):
import sublime
import sublime_plugin
class MarkdownCopyCommand(sublime_plugin.ApplicationCommand):
"""
Special overridden copy command for use in markdown files where there is
more than one caret and all selections are non-empty.
"""
selections = []
def run(self):
# Get the active view and do a normal copy just so the clipboard has
# what it normally would have.
view = sublime.active_window().active_view()
view.run_command("copy")
# Store the text at each of the selections in the active view in our
# separate list.
MarkdownCopyCommand.selections = [view.substr(s) for s in view.sel()]
class MarkdownPasteCommand(sublime_plugin.TextCommand):
"""
Special overridden paste command for use in markdown files. Can restore
the selected data from a preview markdown_copy invocation, if the number
of selections is the same as when the copy happened.
"""
def run(self, edit):
# Get the last copied data (if any)
data = MarkdownCopyCommand.selections
# If there is a selection count mismatch, then just do a normal paste
# and leave.
if len(self.view.sel()) != len(data):
return self.view.run_command("paste")
# Iterate over all of the selections in reverse and put the text
# back where it came from
for sel, text in reversed(list(zip(self.view.sel(), data))):
self.view.replace(edit, sel, text)
The markdown_copy
command does a normal copy
and then also stores the content of each of the selections in it’s own buffer, to keep them distinct. The markdown_paste
command will do a normal paste if the number of carets is not the same as the last markdown_copy
saved, but if the selection counts match then each selection gets the copied text in one chunk.
A version for cutting the text is not here but could be added fairly easily. The copy command also requires the selections to be non-empty (i.e. it doesn’t handle the copy_with_empty_selection
preference, though it could with a bit more tweaking).
I tested it with key bindings something like the following, so that the keys only trigger in the correct circumstances; the contexts include triggering only in markdown files, but there’s nothing inherently markdown-y about them, so they could be used everywhere I would think.
Caveat: I didn’t do any super-exhaustive testing, but it seems to do what one would expect.
{ "keys": ["super+c"], "command": "markdown_copy",
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown" },
{ "key": "num_selections", "operator": "not_equal", "operand": 1 },
{ "key": "selection_empty", "operator": "equal", "operand": false },
]
},
{ "keys": ["super+v"], "command": "markdown_paste",
"context": [
{ "key": "selector", "operator": "equal", "operand": "text.html.markdown" },
]
}