Sublime Forum

Text highlighting

#1

This has been discussed here before, but I can’t tell if it’s really been resolved. More likely I’m not understanding something in the threads–I’m not a programmer. I’m using ST for prose. What I’d like is the ability to highlight a portion of text: I select the text, then, say, hit a key combo, and the background of that selected section turns a different color and stays that way (maybe to be toggled off later…). Possible? As I say, I’m not a programmer, but I can implement plugins, key bindings, etc. Any thoughts appreciated.

1 Like

#2

https://packagecontrol.io/packages/Text%20Marker
https://packagecontrol.io/packages/Text%20Highlighter

0 Likes

#3

Thanks. Those look like good candidates. I’ll check them out.

0 Likes

#4

Both of these packages seem dedicated to highlighting all instances of the same string. I undertand the utility of that for programming, but that’s not what I’m after. I just want to be able to highlight single words or different chunks of text and have that highlighting persist after I save/close the file.

1 Like

#5

You could do that with a little plugin like this:

import sublime
import sublime_plugin


REGIONS_KEY = "highlight_text"

class HighlightTextCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = self.view.settings().get("highlight_regions", [])

        # this seems to be required because sublime.Settings.set() doesn't like sublime.Region values
        for sel in self.view.sel():
            regions.append([sel.begin(), sel.end()])

        self.view.settings().set("highlight_regions", regions)
        self.view.add_regions(REGIONS_KEY, [sublime.Region(region[0], region[1]) for region in regions], "region.yellowish", "", sublime.DRAW_NO_OUTLINE)


class ClearHighlightCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        self.view.settings().set("highlight_regions", [])
        self.view.erase_regions(REGIONS_KEY)

Then you need to add the commands in a .sublime-commands file to make it available in the command palette:

[
    {
        "caption": "Highlight Text",
        "command": "highlight_text"
    },
    {
        "caption": "Clear Highlight",
        "command": "clear_highlight"
    }
]

You can also add keybindings for these commands then. To configure the highlighting color and style, see the description for the add_regions method under https://www.sublimetext.com/docs/api_reference.html#sublime.View.

The highlighting doesn’t persist after closing the file, though.

1 Like

#6

You could try adding the flag sublime.PERSISTENT to store the regions in session and then restore the region when the file is loaded again. Not tried, though.

1 Like

#7

Excellent! You’ve given me some good avenues to explore here. I’ll give them a shot and report results. I appreciate your time and help, folks.

0 Likes

#8

Indeed sublime.PERSISTENT is likely a good idea if you want to quit and restart Sublime and keep the marks. However persistent regions are saved in the session/workspace and that only tracks files that are open. So if you want to persist the marks across closing and opening the actual file, you also need to keep track of that separately.

One way to do that would be to listen for on_close to know when a file is closing and save the regions, and on_load to know when a file is loading and put them back. You have to watch out for the possibility that the file might have been changed externally between those events though (in which case your saved regions will be in the wrong place).

1 Like

#9

Thanks for that tweak, OdatNurd. I’ll explore that.

0 Likes

#10

I know that this is heresy in this forum, but wouldn’t it be simpler just to use a word processor? You can do what you want quite easily in LibreOffice. It saves the highlighting. It probably can be done in Microsoft Word as well.

But if you want to do it the hard way… :grin:

0 Likes

#11

Ha! I hear ya, Jackeroo. Sure it would be easier, and I’ve been doing it that way for years. What you fail to appreciate, though, is that my fiddling around with ST, in addition to being a lot of fun, is a sure-fire way to put off some of the hard writing work I should really be doing. :wink:

4 Likes

#12

I think a lot of writers prefer simple text editors. There is a really good talk on using Org Mode (Sublime has an Org Mode plugin, by the way) for writing here:

1 Like

#13

Very cool. Thanks, rpcm. I got about halfway through the vid on my lunch break–I’m enjoying it. I’m in a similar boat as that fella. But I’m an older guy–60–and after I left the world of typewriters, just after college, I took to writing using a text-based word processor called Xywrite. It shipped with its own little macro-programming language, XPL, that could do some amazing things. I could really put Xywrite through its paces; it was command-line driven and my brain was hardwired to its keyboard shortcuts and the macros I’d made. As far as writing goes, it was the closest thing to an extension of my brain that I’ve ever used for writing. Professionally, though, I was soon in the land of Word and have been there for decades, but I’ve flirted with text-based systems for my own writing off and on over the years. Sublime Text reminds me a lot of Xywrite in its look and its flexibility, but of course it’s far more capable. And I’m enjoying learning to make it do the things I want it to do.

I’ve coupled ST with an older Thinkpad I’ve recently revived, and Ubuntu–my first foray into Linux–to create a writing-only machine for myself. So I’m learning about Linux at the same time. Fun stuff.

I’m definitely going to look into the ST Org Mode plugin. Thanks again for the note.

4 Likes

#14

Thanks for sharing your story. It is so neat to read about someone using Sublime Text in this way. :slight_smile:

0 Likes

#15

ST being a terrific tool however if you’re into prose why not look at a tool that is more geared to writing instead of editing? Yes I know is there a difference yes obviously why else have two different words? Could I point you to Scrivener for your prose writing needs perhaps?

0 Likes

#16

Thanks, ruurd. I’ve been using Scrivener–on Mac and PC–for maybe ten years. I’m a fairly experienced Word user as well, having used it in my profession (publishing and editing) since the mid-90s. I use Scrivener for my personal writing projects–i.e., outside my work-related writing and editing. But I often find myself getting restless with it. It does a lot of useful things, but it can sometimes feel like you’re driving a Boeing 747 to go get your groceries. And for my own creative purposes, a change of writing environment can help shake things loose. For me that usually means simplifiying (see my note above, in this thread, about my early infatuation with Xywrite). I am especially drawn to the simplicity of plain text but with the ability to shape the writing tool almost any way I’d like–hence, ST.

3 Likes