tpope / vim-commentary

commentary.vim: comment stuff out

Home Page:http://www.vim.org/scripts/script.php?script_id=3695

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keep cursor position + commenting in insert mode

thomasqueirozb opened this issue · comments

This is more of a feature request if anything.

I have made this to comment while in insert mode and keep the cursor in the same character:

function CommentInsert()
    let col = col('.')
    let line = line('.')
    let size = strwidth(getline('.'))
    Commentary
    let moved_length = strwidth(getline('.')) - size
    let new_col = col + moved_length
    if new_col < 0
        let new_col = 0
    endif
    call cursor(line, col + moved_length)
endfunction

and added this to call the function:

imap <C-_> <C-o>:call CommentInsert()<CR>

I barely anything about scripting in vim but this is what I came up with (I know it is probably not the correct way to do it).

The function has some problems that I don't think can be fixed without adding something to the plugin. When I call it and the cursor is before the start of the comment, the cursor moves to the right. If it is a html file for example the cursor moves way more to the right than it should because the comment changes the start and end of the line.

I know my function does these things I mentioned by design but I wanted to make it work properly and cant figure out how.

As I said this is a feature request so I don't think it can work without something being added to the plugin. Also I think this could be implemented so that when you call Commentary or use gcc the cursor keeps the same position.

is there any way to keep the cursor with the default gcc keybinding

Every Vim built-in that operates on a full line moves the cursor to the beginning when it's done. My design philosophy is to match this. I don't have the bandwidth to offer help on changing it.

Finally wrote a little hack for this:
(doesn't work for "nested" comments and for visual mode)

function CommentaryWithCursor()
    let line_number = line('.')
    let old_col = col('.')
    let old_size = strwidth(getline('.'))

    Commentary

    " A space is inserted so + 1 is needed
    let split_len = strlen(split(&commentstring, "%s")[0]) + 1

    if strlen(getline('.')) > old_size
        call cursor(line_number, old_col + split_len)
    else
        call cursor(line_number, old_col - split_len)
    endif
endfunction

inoremap <C-_> <C-o>:call CommentaryWithCursor()<CR>
nnoremap <C-_> :call CommentaryWithCursor()<CR>

Finally wrote a little hack for this:
(doesn't work for "nested" comments and for visual mode)

function CommentaryWithCursor()
    let line_number = line('.')
    let old_col = col('.')
    let old_size = strwidth(getline('.'))

    Commentary

    " A space is inserted so + 1 is needed
    let split_len = strlen(split(&commentstring, "%s")[0]) + 1

    if strlen(getline('.')) > old_size
        call cursor(line_number, old_col + split_len)
    else
        call cursor(line_number, old_col - split_len)
    endif
endfunction

inoremap <C-_> <C-o>:call CommentaryWithCursor()<CR>
nnoremap <C-_> :call CommentaryWithCursor()<CR>

FYI, I tried this and seems like the + 1 isn't necessary since split will include the space with the first element. After I removed + 1 it worked for me.