Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim option for automatically inserting ">" at start of line

Tags:

vim

Suppose I have a file test.c containing the following:

// line 1
// line 2

If I open this file in Vim and navigate to the first line in normal mode, then type o, I get the following:

// line 1
// 
// line 2

Now suppose I have a file test.lhs (literate Haskell) containing

> data X = A | B
> data Y = C | D

If I open this file and navigate to the first line in normal mode, then type o, I get

> data X = A | B

> data Y = C | D

Question: How can I make Vim automatically insert > at the start of the line for the .lhs file, similar to how // is automatically inserted for the .c file?

like image 855
Snowball Avatar asked May 28 '12 15:05

Snowball


People also ask

How do you move to the first character of a line in Vim?

In Vim normal mode, you can type 0 to move to the start of the line and $ to the end of the line. The start or end of the line can be blank characters. But if you just want to move to the first or the last non-blank character of the line, you can type ^ and g_ respectively.

How do you go to the beginning of text in Vim?

By default, Vim command line uses Ctrl-B to go to the beginning of the line. Execute :h cmdline-editing to see more key bindings. Save this answer.


1 Answers

Got it! To .vimrc, add

set formatoptions+=o

This automatically inserts the "comment leader" (character sequence indicating a comment, or, in the case of literate Haskell, the Haskell code) at the start of the line.

For more information on the options accepted by formatoptions, type :help fo-table.

like image 154
Snowball Avatar answered Oct 25 '22 22:10

Snowball