0

I'm writing a vim function to comment the selected lines in visual mode. Then I have written the following function to toggle the "#" character. I want insert the # when thereisn't and remove it when it's there.

function Mycomment()
    if col(1.)=="^#"
        :s/^#//g
    else
        :s/^/#/g
    endif
endfunction

Obviously doesn't work. The biggest problem is to know what's the content of the column 1.

1
  • 3
    May have figured this out already but col() is not used to get content...it's used to get the column number/index (of the cursor, or a mark, or ...). And it takes a string or a two-element list.
    – B Layer
    Oct 28 '20 at 3:58
1

Ok I solved in this way:

function Commenta()
    let curline = getline(line('.'))
    let primo = strpart(curline, 0, 1)
    if primo=="#"
        :s/^#//g
    else
        :s/^/#/g
    endif
endfunction
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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