Vim вставляет комментарии в стиле C

968
user339365

:command Linecomment :normal ^i/*<ESC>$a*/<ESC>
Вышеприведенная команда - это команда, которую я придумал, чтобы закомментировать целую строку в C, обычно для целей отладки и еще много чего.

Мне было интересно, какие модификации понадобятся, чтобы превратить это в функцию, и какую подобную функцию можно прокомментировать от позиции курсора до конца строки?

0

1 ответ на вопрос

2
Ingo Karkat

To factor out a function (which is useful when the mapping commands become longer), the only thing you need to consider is that the special keys like <Esc> that work in the right-hand side of the mapping don't work inside the function; you need to use :execute with double-quotes, and escape them:

function Linecomment() execute "normal ^i/*\<ESC>$a*/\<ESC>" endfunction command Linecomment call Linecomment() 

To comment from the cursor position, you just need to drop the ^ motion at the start.

However, for a truly powerful and robust commenting solution, I would recommend to use one of the popular plugins:

Спасибо тебе за это. Я схватил NERD Commenter, и он отлично работает. Мне потребовалась минута, чтобы понять, как установить плагины, но теперь все работает именно так, как я хотел! user339365 9 лет назад 0