Sublime Forum

How to programatically delete first character of each line?

#1

Hi,

I’m total noob to both .py and the sublime api.

As a simple exercise I thought I’d create a vbscript commenting plugin - it should insert an apostrophe at the beginning of each line of selected text. Then another plugin will remove the apostrophe.

I am using this code to insert the apostrophe

[code]
import sublime, sublime_plugin

class AspAddCommentCommand(sublime_plugin.TextCommand):
def run(self, edit):

    for selectedRegion in self.view.sel():
        selectedLines = self.view.lines(selectedRegion)
        adjustBy = 0
        for line in selectedLines:
             insertPoint = line.begin() + adjustBy
             self.view.insert(edit, insertPoint, "'")
             adjustBy += 1[/code]

Questions

For every line I iterate, I need to adjust the position of line.begin() to allow for the additional apostrophe added. Am I approaching this in the right way by incrementing a counter, or is there a better approach?

How would I go about removing the apostrophe I’ve added? (Basically, deleting the first character of each line).

It took me about 5 hours to create the few working lines of code you see above, so I was hoping somebody kind could point me in the right direction for the deleting side of things.

0 Likes

#2

This is not really any help to your plugin question, but I would approach this specific problem a different way (without plugins). I would select the lines to be commented out, then split that selection into lines (Selections \ Split into lines), then press home to get to the beginning of each of the lines and then type in the missing apostrophe.

0 Likes

#3

Hey thanks. That is actually what I do in practice. But it seems a bit clumsy, I’d rather have a way of automating it.

I started playing around with the plugin syntax and got carried away… but now I would like to finish my little plugin if possible.

0 Likes

#4

Come to think of it, if comments are defined correctly in the vbscript language definition then you should be able to comment out the selected lines with Edit \ Comment \ Toggle Comment (on windows the shortcut is ctrl + keypad_divide)

0 Likes

#5

It doesn’t seem to work, at least not on .asp files.

0 Likes

#6
  1. CTRL+A
  2. CTRL+SHIFT+L
  3. HOME
  4. DEL
1 Like

#7

Thanks tito.

It’s no big deal really I suppose… but when I do that, I find it untidy because the comment always goes to the beginning of the text, not the beginning of the line. So that the comment appears after any indent.

Ideally I’m looking for a way to do it programatically via a plugin.

0 Likes

#8

okeii, this then
github.com/facelessuser/RegReplace

0 Likes

#9

Thank you, again. That looks very useful. I wonder if it’s a bit overkill for my noob requirements.

Is there a simpler way to remove the first character of a line/string? For example, can I do anything along the lines of:

line = line.substr(1)

…or…

line = left(line, len(line)-1)
0 Likes

#10

Yes, sublimetext.com/docs/3/api_reference.html

0 Likes

#11

Can you point me to a method you’d recommend or give an example? I am still struggling to get my head round that doc. I didn’t see any obvious way to do what I wanted there.

0 Likes

#12

AS you said that that package was overkill … I’ll let you investigate this by your self, so you can evaluate which thing is more overwhelming. Good luck

0 Likes

#13

[quote=“mhl”]Thank you, again. That looks very useful. I wonder if it’s a bit overkill for my noob requirements.

Is there a simpler way to remove the first character of a line/string? [/quote]

stackoverflow.com/questions/6631 … -in-python

0 Likes

#14

@tito - I didn’t mean it like that, I do appreciate your help. I just wanted a bit of guidance from someone more experienced on how to do it with python and/or the sublime api

@jbjornson - thanks for the link, I will have a go and see what I can do with that

0 Likes

#15

Personnaly, if I have to do something like that, I’ll use a (hand-made) macro like:

{ "args": null, "command": "split_selection_into_lines" }, { "args": { "extend": false, "to": "hardbol" }, "command": "move_to" }, { "args": null, "command": "right_delete" } ]
and bind it to a shortcut with:

{ "keys": "alt+shift+left"], "command": "run_macro_file", "args": {"file": "Packages/User/Delete first char of line.sublime-macro"} },

You can write a plugin that call the same commands:

def run(self, edit): self.view.run_command("split_selection_into_lines"} self.view.run_command("move_to", {"extend": false,"to": "hardbol"} self.view.run_command("right_delete"}

If you want to do all by yourself, you can use the view.erase(edit, region) API method, but it’s a bit trickier.

0 Likes

#16

Thanks bizoo, I’ll try both of those. I didn’t realise you could put commands together like that, makes it much easier. If I can create an insert macro too, I’m sorted.

0 Likes

#17

And to get back to how it’s actually done, here is how you get any syntax to work with the toggle_comment command: docs.sublimetext.info/en/latest/ … ments.html

0 Likes

#18

Not what OP wanted, but this is exactly what I needed. Thanks, tito!

0 Likes