5

Vim で行の長さが特定の文字数を超えているコメントを修正する便利な方法を探しています。特にそれほど頻繁ではないため、これをコードで手動で行うことは問題ありません。さらに、長い行のリファクタリングは言語やコードスタイルに依存することがよくありますが、コメントを使用すると、これは純粋に面倒です。

何が起こるかというと、コメントに問題があることに気づき、1 つまたは 2 つの単語を微調整すると、行がたとえば 80 文字の制限を超えてしまうことがあります。最後の単語を次の行に移動すると、次の行がはみ出します。Vimでこれを自動的に行う方法を知っている人はいますか?

4

1 に答える 1

3

これが定期的な問題である場合は、次を vimrc に追加することをお勧めします。

nnoremap <leader>f gqip

textwidthこれは、現在設定されているまたはtwオプションの幅になるようにコメントをフォーマットする gq を使用して、コメント (いくつかの formatoption フラグを設定した後に段落と見なされる) をフォーマットするためのリーダー f ショートカット (f は「フォーマット」を表す) をマップします。.vimrc で textwidth を設定する必要がありますtextwidth=80

Formatoptions は、特にあなたの場合、acqフラグをformatoptions+=acq. 認識されたコメントだけでなく、すべてのコードが自動的にラップされるため、tフラグを削除するように注意してください。formatoptions-=tこれをすべて実行した後、空白行で囲まれているかどうかに関係なく、コメント内でのみ f を押してフォーマットできるはずです。

ここに formatoptions に関する関連情報があるので、自分で選択できます。

t       Auto-wrap text using textwidth

c       Auto-wrap comments using textwidth, inserting the current comment
    leader automatically.

r       Automatically insert the current comment leader after hitting
    <Enter> in Insert mode.

o       Automatically insert the current comment leader after hitting 'o' or
    'O' in Normal mode.

q       Allow formatting of comments with "gq".
    Note that formatting will not change blank lines or lines containing
    only the comment leader.  A new paragraph starts after such a line,
    or when the comment leader changes.

w       Trailing white space indicates a paragraph continues in the next line.
    A line that ends in a non-white character ends a paragraph.

a       Automatic formatting of paragraphs.  Every time text is inserted or
    deleted the paragraph will be reformatted.  See |auto-format|.
    When the 'c' flag is present this only happens for recognized
    comments.
于 2012-10-03T23:30:44.347 に答える