Sublime Forum

Is "Copy Bookmarked Lines" doable in Sublime?

#1

Is there a feature like Notepad++'s Copy(Cut/Paste/Delete) Bookmarked Lines? That feature lets me do a find on all lines containing “my text”, marks them, and then I can copy just those marked lines into, say, a new file.

0 Likes

#2

You probably can skip bookmarks for that in Sublime Text:

  1. Perform search
  2. Alt+F3
  3. Dismiss search panel with Esc
  4. Ctrl+L
  5. Ctrl+Shift+L
  6. Copy
  7. Create new buffer
  8. Paste

As a bonus, you can now edit those lines and paste them back into place in the previous buffer.

0 Likes

#3

Thanks. That’s part of the way there. But I want to do more searches, add more to the select before doing the copy. So, for example, with the following 4 lines, I’d like to first search on “foo” to mark or select lines 1 and 4, and then search on “bar” to mark or select line 2. Only then do I want to copy the mark or selected lines. (What I’ve described is doable in Notepad++ and the commercial Textpad and I depend on it)

Doable?

1 foo
2 bar
3 something else
4 foo

0 Likes

#4

Not out of the box, but if I got it right, this is exactly what I created UberSelection for:

bitbucket.org/guillermooo/ubers … n/overview

As a side note, while this plugin isn’t especially hard to use, I’m sure this kind of thing can be simplified further, like a multi-step additive selection via keyboard only.

The command you would use in UberSelection would be this one:

%V/foo|bar/

It’s working for me now, but I might have left a couple of loose ends when I ported it from ST1.

0 Likes

#5

Btw, I think I’ve spoken too quick. This might already be doable with bookmarks.

Perform search
Bookmark all
repeat ad lib

Select all bookmarks
Split into lines

That should do it for your use case, I believe… UberSelection can still do more than that, though.

0 Likes

#6

Sublime Text is like that saying that goes along the lines of “can’t see the forest of features for the forest of features” but in the good sense.

0 Likes

#7

After select all bookmarks, instead of your “Split into lines”, I do “Expand select to lines”, and then copy, and it works, BUT… the result pasted text has additional blank lines.

( This is my first hour evaluating Sublime Text 2 so I haven’t quite figured out how to install your UberSelection, but if it adds vi like commands to ST2, well, I’ve only dabbled with vi… )

0 Likes

#8

Crtl+L and Ctrl+Shift+L will first expand the selection up to and including the newline char, and then shrink it to the full line except the newline char. That’s become and idiom for me in Sublime Text. Doesn’t that get rid of the blank lines for you?

0 Likes

#9

UberSelection is heavily inspired (read: rip-off) in vi’s ex mode, but it just implements a handful of standard commands (and some of them are broken IIRC). The interesting ones --to me, at least-- resemble ex commands, but they don’t even share the same syntax --save for the ranges (.,$, etc).

The installation instructions are wrong because they include information valid for ST1 only… It should be a matter of dropping the .sublime-package file inside your Installed Packages folder (a sibling of Packages, which is accessible via Preferences from the main menu).

In any case, I believe bookmarks should work in this case, and they are far simpler to use.

0 Likes

#10

Thanks again, yes ctrl-l, ctrl-shift-l works. Just for future reference, below are the steps:

1 ctrl-f to find
2 enter text to find
3 alt-f3 to find all with selection
4 escape to get rid of find bar
5 ctrl-f2 to bookmark
6 go back to step one to find and bookmark again

7 alt-f2 to select all bookmarks
8 ctrl-l to expand selection to lines

To copy:
ctrl-shift-l to split selection to lines, which has the effect of trimming the line breaks
ctrl-c to copy

Or, to delete:
don’t hit ctrl-shift-l
ctrl-x to delete

0 Likes

#11

btw, ‘Find All’ on the find panel (which is bound to alt+enter) will do steps 3 and 4 for you

0 Likes

#12

Even better:

class CopyBookmarkedLinesCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command(“select_all_bookmarks”)
self.view.run_command(“expand_selection”, {“to”: “line”})
self.view.run_command(“split_selection_into_lines”)
self.view.run_command(“copy”)

0 Likes