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.
195
questions
146
votes
10
answers
97k
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"},{&...
66
votes
3
answers
26k
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 ...
29
votes
5
answers
3k
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 ...
23
votes
3
answers
3k
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 |
% |
% ...
20
votes
2
answers
3k
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 ...
20
votes
5
answers
2k
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?
...
20
votes
4
answers
4k
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 '. ',...
18
votes
3
answers
3k
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 ...
18
votes
2
answers
5k
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 ...
18
votes
3
answers
7k
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
1
answer
2k
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
5
answers
7k
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 ...
13
votes
3
answers
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 ...
13
votes
1
answer
5k
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.
10
votes
3
answers
18k
views
How to format files on save using black with neovim and coc
I'm trying to have python files format on save using black and neovim, with coc.
Coc config
I have the following coc config:
{
"coc.preferences.useQuickfixForLocations": true,
"coc....
10
votes
4
answers
1k
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
1
answer
193
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
1
answer
6k
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?
9
votes
1
answer
939
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,...
8
votes
1
answer
393
views
Avoid breaking words before or after asterisk ('*')
If you have set wrap, sometimes you can see something like this:
5. A soft-wrapped line with an *
italic* word.
I would prefer Vim to not soft-wrap before or after asterisks, and so the same ...
8
votes
6
answers
23k
views
How to configure neovim to properly format python code?
I have neovim 0.8.0 installed.
I have neovim/nvim-lspconfig plugin installed.
I have installed pyright language server on my system (pip install pyright)
I have configured neovim to use the pyright ...
7
votes
2
answers
11k
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}'...
7
votes
1
answer
2k
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
2
answers
993
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. ...
6
votes
2
answers
666
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
1
answer
536
views
What does breakat&vim mean?
I'm trying to understand this SO answer. The snippet reads:
:set nolist wrap linebreak breakat&vim
6
votes
2
answers
1k
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 ...
6
votes
0
answers
495
views
How to get bold comments in vim
I am trying to add bold comments to my vim files. What i mean, is to be able to get something like this:
I saw other articles like this that allow to toggle bold comment.
The proposed solutions from ...
5
votes
2
answers
2k
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(...
5
votes
2
answers
5k
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
1
answer
6k
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 ...
5
votes
2
answers
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 ...
5
votes
1
answer
244
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
1
answer
215
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
1
answer
126
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
1
answer
456
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
1
answer
488
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
1
answer
603
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
1
answer
216
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
1
answer
209
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 ...
5
votes
0
answers
332
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 ...
5
votes
0
answers
194
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 ...
4
votes
3
answers
4k
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 ...
4
votes
1
answer
381
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
1
answer
510
views
Highlight whitespace using matchadd
I'm trying to use matchadd to highlight different occurrences of whitespace. However, sometimes it doesn't work for some reason. Here is the code:
highlight GroupA guibg=lightgreen
highlight ...
4
votes
1
answer
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
2
answers
2k
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
1
answer
461
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
1
answer
161
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
1
answer
444
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 ...