1

So, I'm trying to set up a sort of bookmark/marker kind of workflow, and I came to notice I'm used to put characters such as # and such for different meaning inside my own notes, etc.

Now, I already know how to do the first part (inserting text at the start of a/the current line):

.s/^/#

And I also already know how to make a mapping of it. Problem is, I want to be able to also remove the placed marker if it's already there, and place one on the current line if it's not.

I'm unsure on how this should look like in vimscript. I don't mind using an existing plugin for this, but would prefer not to if possible.

Any feedback/answer appreciated.

8
  • 3
    Here's a sketch: first check if the current line starts with # with getline('.') ~= '^#'. If it does, remove it with :s/^#//, otherwise insert one with :s/^/#. Does this help?
    – husB
    May 22, 2021 at 6:58
  • Think I could work with this yeah. Thanks a lot for the suggestion :) @husB May 22, 2021 at 6:59
  • 2
    Ok let me know how it goes. If you've figured it out feel free to post your own answer; otherwise comment here and I'll fill in more details :)
    – husB
    May 22, 2021 at 7:01
  • Made this so far. Don't think this work but the logic look like it could work. @husB May 22, 2021 at 7:27
  • 3
    Related: github.com/benknoble/Dotfiles/blob/… I remove the prefix before inserting it, and have separate mappings rather than toggle. You could also try a comment plugin like commentary
    – D. Ben Knoble
    May 22, 2021 at 12:45

1 Answer 1

3

Thanks to @husB's comment, seems I figured it out:

function Markerstart()
    if getline('.') =~ '^#'
        :s/^#//
    else
        :s/^/#/
    endif
endfunction

Then use :call Markerstart on a specific line, and it work.

Can be set up with vnoremap so it can work on multiple lines.

6
  • You don't need exe if the substitution consists of literal strings only. Just s/^#//.
    – B Layer
    May 22, 2021 at 9:57
  • 1
    Were you using "? You shouldn't use quotes. Otherwise, it should work. I'll try it out to be sure.
    – B Layer
    May 22, 2021 at 9:59
  • 1
    yep, you're right; works without quote and exe. Will edit :) @BLayer Thanks! May 22, 2021 at 10:00
  • 1
    You don't need the opening : either as you're in a purely Ex context...but it's harmless. :)
    – B Layer
    May 22, 2021 at 10:02
  • 1
    Gotcha. The more you know :D @BLayer May 22, 2021 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.