I added something like this to my tex.vim
ftplugin
(I tried to simplify it because there are other plugins involved, but hopefully it makes sense):
set comments+=b:\\item
inoremap <expr> <cr> getline(".") =~ "\item $" ? "<c-w><c-w>" : "<cr>"
So when I type \item foo
and tap <cr>
another \item
is automatically added in the next line, and then if I tap <cr>
again without typing anything that last \item
is deleted.
This works great and it got me thinking that it should be like that for all comments. A second (and third and fourth) <cr>
when no text is entered should remove the last comment character instead of keeping adding comment lines forever. So I thought I'd write a function somewhat like this:
function CROrComment()
for comment in comments
if getline(".") =~ comment . " $"
return "\<c-w>\<c-w>"
endif
endfor
return "\<cr>"
endfunction
And then I'd use it to figure out if <cr>
should do a <cr>
or delete the last comment. This has many problems, though, some of which I can't figure out.
First of all when I use comments
there it doesn't get the comment characters. I don't know how to get them to use them in the function.
Then I'd have to convert them into an array. I'd probably be able to figure that out by myself, though, for now I'm stuck at the first problem, but if anybody can help with that too it'll be even better.
And then there's the return "\<c-w>\<c-w>"
part, which I used to delete \item
, but it would be better if it adapted to the length of whatever the comment is.
I'm sorry if any of this is obvious, I just started playing around with Vim and I still don't know much about it.