Questions tagged [formatting]

Questions about how to format a file with vim - this includes, but is not limited to, foldings, indents, wrappings, and so on.

Filter by
Sorted by
Tagged with
58
votes
3answers
20k views

How can I get vim to stop putting comments in front of new lines?

I'm editing my .vimrc file, and I am including comments. When I have a comment like this: " example comment And then hit enter at the end of the line after finishing the comment, the next line is ...
54
votes
8answers
32k views

How to format JSON file in Vim

Given a file with a large JSON object all on one line like {"versions":[{"count":2,"version":""},{"count":1,"version":"1.1.1"},{"count":14,"version":"2.9"},{... How can I format this to be human ...
26
votes
4answers
2k views

How can I reformat a multi-line string in vim when using the Python filetype?

Let's assume I am editing some Python code in vim that looks a bit like: myobj.myfunc("Some string parameter that goes on and on and on and on and sometimes doesn't" "split very" ...
22
votes
3answers
2k views

How to write vertically?

Lets say I want to produce the following text in my document: % % Not Important % O -------------------------> % | % S | % o | % m | % e | % | % M | % s | % g | % | % ...
19
votes
4answers
3k views

How to set up Vim to work with one sentence per line?

I'm currently writing a lot of plain text (and LaTeX with minimal formatting in paragraphs), and it would be nice if I could set up vim to keep each sentence (for simplicity, text terminated by a '. ',...
17
votes
4answers
1k views

Merge blocks by interleaving lines

Is there a dedicated way to merge two blocks of text by interleaving lines, like passing from this: a1 a2 a3 a4 b1 b2 b3 b4 to that: a1 b1 a2 b2 a3 b3 a4 b4 in a few commands? ...
17
votes
3answers
2k views

Blocking changes to read only file

When using Vim to edit a read only file it will just provide a warning the first time it is edited but allow any changes to take place. I can understand this behaviour could be useful for someone ...
14
votes
2answers
3k views

Why Vim sets max text width to 79 instead of 80?

I'm a little puzzled about some default values in Vim. In particular, for gq{motion}, is said that [...] If the 'textwidth' option is 0, the formatted line length is the screen width (with a maximum ...
13
votes
1answer
1k views

Unexplained gq indentation issue

I insert this text in Vim: t tttt ttttttttt tt tttt tt ttt tttttt-tttt-ttttttt tttttttttttttttttttttttttttttttttttttttttttttttttttttt ttttt ttt ttt tttttt ttttttt tttt ttttttt: ttttttt tt ttttttt, ...
13
votes
3answers
4k views

automatically rewrap lines when writing markdown in VIM

Very often, I write markdown in VIM, and there will be paragraphs in those markdown. To help my editing, I setup my Vim to wrap a line on 80 chars. It works nice if I just keep typing, but the problem ...
13
votes
3answers
1k views

How to automatically lower case the second letter of a word?

When I have to type a lot of text I often tend to keep my finger pressed on shift when I write the first letter of a sentence which often give: [...]end of sentence. NEw sentence[...] Here the E of ...
11
votes
2answers
1k views

Remove new line from result of system() [duplicate]

I would like to store the result of a system() call in a variable without the trailing ^@ character. At the moment I do something like var = system('command | xargs -i echo -n "{}"') but it feels ...
10
votes
5answers
4k views

set formatoptions in vimrc is being ignored

Vim automatically continues a comment when I press Enter in Insert mode, which I find helpful. However, I do not want the comment continued after pressing 'o' from normal mode to insert a new line ...
10
votes
4answers
774 views

Line number specific text-width setting

I use gvim for writing emails and use tw=72 to break long lines. Now I also edit basic mail headers in the same document, e.g. I am adding recipients to my mail. Now with tw=72 it also breaks every ...
10
votes
1answer
179 views

How to temporarily make gq consider hyphen (-) a normal word, to not indent the following lines in the same paragraph

I have tried this in a JavaScript file after disabling smartindent and autoindent options, with textwidth = 80, but it is still the same. It also happens automatically because of the textwidth option ...
9
votes
1answer
786 views

Auto-format: wrapping later in line when inserting text

After reading help textwith and help fo-table, I expected that when I set set textwidth=20 " for demonstration set formatoptions=tcq then inserting text would always eventually cause a line to wrap,...
7
votes
1answer
792 views

Neovim format ruby code like rubocop

is there any chance to format code to satisfy rubocop? By default my neovim is indenting code like that: class Test attr_accessor :a, :b, :c, :d, :e, :f end However, it would be nicer to have ...
6
votes
2answers
464 views

Is it possible to create a folding inside a single line?

The chapter 28 of User Manual says the following about folding: Folding is used to show a range of lines in the buffer as a single line on the screen. And all the examples of folding that I've ...
6
votes
2answers
10k views

How to format xml or html's attributes into multiple lines instead of lining up on one line?

I'm using plugin Chiel92/vim-autoformat auto formatting xml files. So the default format program used for xml file is tidy. The way it formats xml file is like below: <List id='list' items='{/sss}'...
5
votes
2answers
584 views

How do I make Vim respect Markdown headers when formating text?

I want to use the combination gggqG to go to the top of the file and format the whole file from there, so that it fits into my 80 col textwidth border. But when I do, vim ignores my markdown headers. ...
5
votes
1answer
3k views

Expand <TAB> into 2 spaces when and only when editing HTML

My current configuration makes vim automatically expand TAB to 4 spaces for me: " Define tab as 4 spaces " Taken from http://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces set tabstop=4 ...
5
votes
1answer
3k views

what are the usage of equalprg and formatprg

If I want to format a few lines, should I choose equalprg (using = operator) or formatprg (using gq operator)? Both of them are for some kind of formatting. But what are the differences.
5
votes
1answer
169 views

gqip for VHDL comments does not work

After I set syntax=vhdl and set fo=tcq, running gqip on the following paragraph does not work as expected: -- Lorem ipsum dolor sit amet, -- tempor incididunt ut labore et dolore magna aliqua. Ut ...
5
votes
1answer
121 views

Edit comments as if separate lines?

Is there a way to edit comments so that they will all be aligned without having to align them manually with tab? i.e., How can I get something like that? some text %comment some more text ...
5
votes
1answer
164 views

Is there a way to push existing text left while inserting (e.g., insert right-aligned text)?

In insert mode, typing normally moves the cursor right and pushes text to the right of the cursor farther to the right on the line. I am typing some right-aligned text and am wondering - is there a ...
5
votes
1answer
335 views

How can I make text formatting ignore concealed characters?

Question Working with text and various markup languages in Vim I have recently experimented with the conceal feature to hide some of the more verbose markup. I switch conceal off when writing and ...
5
votes
2answers
691 views

Reformat within comment

In my vimrc I have the following: vmap Q gw nmap Q gwap set formatoptions+=cj set formatoptions-=t This disables automatic text wrapping, and allows me to correctly wrap the paragraph my cursor is ...
5
votes
1answer
332 views

Can auto-format break at hyphens?

I write my email in vim, which dynamically inserts newlines after 72 characters. formatoptions=awtcql This works nicely, breaking at spaces. However, is there a way to make it break at hyphens as ...
5
votes
1answer
456 views

Make autoformating insert a line break instead of two spaces after full stop

I noticed that vi's auto format adds two spaces after a full stop. I wonder how this can be changed, i.e., insert just one space after a full stop, or even a line break. The application is in using a ...
5
votes
1answer
164 views

How to wrap plain text honoring existing text indent?

With at least these settings: expandtab textwidth=79 sw=4 ts=4 fo=tcq If I have a long line of text that starts with a * (like a C/javadoc comment) it will honor the current indent level when I gqq, ...
5
votes
1answer
169 views

Text formatting CVS-friendly

I have seen this very interesting article: http://dustycloud.org/blog/vcs-friendly-patchable-document-line-wrapping/ --- basically it is a suggestion for hard-breaking lines of text (for example, in ...
4
votes
1answer
2k views

How to clang-format the current buffer on save?

Whats the best way to clang-format a C/C++/GLSL a buffer on save, that does nothing in the case there is no clang-format file found for a project?
4
votes
2answers
1k views

How can I make vim's flowing aware of email replies?

Problem summary I use vim to edit emails that I send with mutt. I also use format=flowed, which "reflows" text. That is, lines are limited to a fixed number of characters, and a "soft wrap" is ...
4
votes
2answers
1k views

How to set the relative indent after breaking Python lines?

When I insert a line break inside a line of Python 3 code, the remaining part of the line is indented by two additional tab widths (2 time 4 spaces) in the next line. # Before example_function(...
4
votes
1answer
4k views

What is the significance of “Incomplete last line” when opening a file?

I occasionally open a file with vim and see something like this: "<file name>" [Incomplete last line][dos format] 71 lines, 2912 characters I understand all of that information except ...
4
votes
1answer
357 views

Formatting with par and justified option

I just learnt from a vimcast (#18) that one can format paragraphs justified. This seemed very attractive to me, but I had one problem with it: I use double spaces after the end of sentences to ...
4
votes
1answer
1k views

Autoindent second line of plain text bullet list item

When using plain text I write bullet lists like this (second and further lines indented with two spaces): * blah blah blah blah blah * yadda yadda yadda Problem is, when I start out a new bullet ...
4
votes
2answers
914 views

Remove unecessary indents while keeping the relative indentation

I am looking for a transformation that would change this text: text indented text double indented text indented again to: ...
4
votes
1answer
246 views

Using `gq` to correctly wrap function arguments

Consider editing the code foo = long_function_name(var_one, var_two, var_three, var_four) where the textwidth 'column' occurs just after var_two. I would like to be able to press gqq on the line ...
4
votes
1answer
129 views

Format options setting collapses lines

I set up this nifty custom function to help me use Vim for word processing: func! WordProcessorMode() setlocal formatoptions=ant setlocal textwidth=60 setlocal wrapmargin=0 endfu com! WP ...
4
votes
1answer
362 views

Vim Pandoc & Headers

When using vim-pandoc, I'm having some funny behavior with headers: For instance, if I position the cursor on a word and press v-a-w and then <localleader> - i, I get italics. if I position ...
4
votes
1answer
1k views

How to prevent vim-jsx from mangling my indentation?

Looks like the issue is caused by vim-jsx. Given: componentDidMount: function() { ▐$.ajax({ }); I press enter to move $.ajax onto a new line and the top line folds as well: componentDidMount: ...
4
votes
1answer
213 views

Insert comma on new line in array

I like to format my arrays like this: myArray = [ "lorem" , "ipsum" , "dolor" , "sit" , "amet" ]; If I open a new line below the "amet" line, I ...
4
votes
0answers
183 views

How to stop vim from reformatting lines inside a codeblock in a markdown file?

I keep the a formatoption in vim when I'm modifying markdown files, so my lines are automatically kept to textwidth characters long, with automatic reformatting when I modify text. How can I make ...
4
votes
0answers
140 views

How to align a Latex table that uses \multicolumn?

I want to align a Latex table the way it appears in the final document. There are various Vim plugins for aligning tables (2, 3, 4), but none of them handle the spanning cell correctly. The code ...
3
votes
5answers
801 views

How to conveniently add ornate headers to source code?

Currently I’m adding source sections manually, e.g.: /******************************************************************************* * entry point *******************...
3
votes
3answers
3k views

Run macro over a visual selection with line movements

I can find an answer to a similar problem here, however although I can get that example to function I can't get what I want to do to work. Here is the example text to be edited, a partial section of ...
3
votes
1answer
151 views

Indentation with “#.” start of paragraph

When I type 1. blah blah blah Vim recognizes the 1. prefix and it indents the rest of the paragraph tidy. I would like to configure Vim to do the same thing with #. literal prefix, since ReST files ...
3
votes
1answer
434 views

Auto Indent (the = key) of C switch block

For the following switch block: If I select the switch block and apply the auto format command, the = key, I get the following: The block is formatted correctly in the first image, but it gets ...
3
votes
1answer
416 views

Vim “gq” command doesn't recognize Haskell-style comments

I have a Haskell file on VIM with a Haskell-style comment block: When I select that comment block on visual mode... And press gq, I expected VIM to format my comment to something like: But instead, ...