175
votes
Accepted
How to format JSON file in Vim
Or you can install jq which is faster (written in C) from your package manager (e.g. sudo apt install jq in Ubuntu/Debian, sudo dnf install jq on Fedora/RHEL/CentOS) or from source and then in vim, ...
- 2,288
83
votes
Accepted
How can I get vim to stop putting comments in front of new lines?
This is controlled with the formatoptions setting; from :help fo-table:
You can use the 'formatoptions' option to influence how Vim formats text.
'formatoptions' is a string that can contain ...
- 59.7k
76
votes
How to format JSON file in Vim
This one-liner works well to format JSON in Vim into a human readable form:
:%!python -m json.tool
- 2,037
25
votes
Accepted
How to set up Vim to work with one sentence per line?
The 'formatexpr' option
You can achieve this using the 'formatexpr' option. From the help:
Expression which is evaluated to format a range of lines for the gq operator or automatic formatting (see ...
- 7,430
24
votes
Accepted
Remove new line from result of system()
If you don't want to use systemlist for whatever reason, you can explicitly remove the newline and/or whitespace. Since vim 8.0.1630 (very recent), there is a trim() function which removes whitespace,...
- 13.5k
19
votes
Accepted
Unexplained gq indentation issue
The problem is that you have set smartindent in your ~/.vimrc. The smartindent options causes Vim to assume that your text is some C-like programming language when indenting. Among other things, it ...
- 6,144
17
votes
Accepted
How to write vertically?
Really good solution would probably require some more work, but "not bad" is
not very hard to achieve.
Idea
All we need is to move one line down after each character, so let's do just
that via ...
- 1,708
16
votes
Accepted
Blocking changes to read only file
I found a solution. Setting nomodifiable prevents the file being edited (as used in help windows). I created a simple function to set or unset modifiable depending on if readonly is set and attached ...
16
votes
Merge blocks by interleaving lines
Here is another alternative:
:g/^a/+4t .
:+,+5d
First copy the lines which are 4 lines below to the after the current line (:h :t) then delete the consecutive b lines (:h :d)
Even better is this ...
- 24.1k
16
votes
How to clang-format the current buffer on save?
You can add the following to your .vimrc
1. function FormatBuffer()
2. if &modified && !empty(findfile('.clang-format', expand('%:p:h') . ';'))
3. let cursor_pos = getpos('.')
4. ...
16
votes
Accepted
How to format files on save using black with neovim and coc
This is a coc.nvim setup procedure that works for Neovim, which aims to:
Use coc-pyright as language server.
Provide static type checking from a Python virtualenv (e.g. I have created a venv called ...
- 315
13
votes
Accepted
automatically rewrap lines when writing markdown in VIM
Even simpler: the a flag for 'formatoptions' enables automatic formatting of paragraphs whenever text is inserted or deleted. See :help fo-table for details on 'formatoptions' flags and :help ...
- 1,437
11
votes
Why Vim sets max text width to 79 instead of 80?
It is important to realize that this "default" only applies to the gq and gw commands and auto-format as described in that section. The default textwidth is 0. Furthermore, :right and :center ...
- 13.5k
10
votes
Blocking changes to read only file
Add this to your .vimrc:
autocmd BufRead * let &l:modifiable = !&readonly
- 296
10
votes
Why Vim sets max text width to 79 instead of 80?
I don't have any evidence that this is why 79 was originally chosen, but one good reason to leave it at that value is because if you use 'list' with a value included for eol in 'listchars' then the ...
- 30.1k
10
votes
How to format JSON file in Vim
To format in a deterministic way, we need to sort the hash. None of the other answers did that for me, so I created my own:
function! FormatJson()
python << EOF
import vim
import json
try:
...
- 101
9
votes
Accepted
Merge blocks by interleaving lines
There is no dedicated way to do that (as far as I know), but yeah, it can be done with a few commands:
function! Interleave(start, end, where)
if a:start < a:where
for i in range(0, a:...
- 3,949
9
votes
Accepted
How do I make Vim respect Markdown headers when formating text?
Since you don't have wrapping for comments enabled, the workaround in this Stack Overflow post can be used:
set comments+=n:#
set fo+=q
This adds # as a comment marker (allowing nesting, so that ##, ...
- 24.4k
9
votes
Transforming a long table into multiple columns
If you want to do that in vim you can use the following macro:
qa^V3j$d"_4dd^V3jI<tab><Esc>Pq
Which can be decomposed like this:
qa Record the macro in the register a
^V ...
- 48.1k
9
votes
set formatoptions in vimrc is being ignored
When vim starts, it runs $VIMRUNTIME/ftdetect.vim to find out what type of file you're editing; then, if you have 'ftplugin' set, it sources $VIMRUNTIME/ftplugin.vim which sources $VIMRUNTIME/ftplugin/...
- 91
8
votes
Accepted
visual mode gq not reflowing comment block
In my vimrc file I had set paste turned on.
When I removed this configuration I was able to get comments to flow correctly.
- 201
8
votes
Accepted
Is it possible to create a folding inside a single line?
You can hack around with some custom syntax and the use of the conceal:
syntax region FunctionArguments start=+(+hs=e+1 end=+)+he=e-1 conceal cchar=…
set concealleval=1
e.g.
function (a, b, c, d, e)...
- 15.6k
8
votes
How to conveniently add ornate headers to source code?
Note 1 As I said in the comment this kind of box is not a good thing: it distract the eye from the important part of your file (the comment itself or the actual code). There are better way to create ...
- 48.1k
8
votes
Accepted
what are the usage of equalprg and formatprg
From :help equalprg:
'equalprg' 'ep' string (default "")
global or local to buffer |global-local|
External program to use for "=" command. ...
From :help =:...
- 9,159
8
votes
Accepted
What does breakat&vim mean?
It resets the breakat option to the Vim default: ^I!@*-+;:,./?.
Where ^I stands for the tab character.
You get the corresponding help paragraph with the :help :set-default command.
:se[t] {option}&...
- 6,440
7
votes
Accepted
How to format xml or html's attributes into multiple lines instead of lining up on one line?
This should almost work, it aligns the attributes slightly different than your example, but it's probably "close enough":
autocmd FileType xml let g:formatprg_args_expr_xml .= '." --indent-attributes ...
- 59.7k
7
votes
Accepted
Indentation with "#." start of paragraph
:help 'formatlistpat' says:
A pattern that is used to recognize a list header. This is used for the "n" flag in 'formatoptions'.
And :help 'formatoptions' says:
Vim default: "tcq"
:help fo-...
- 59.7k
7
votes
Accepted
Opening a new line without returning the carriage
An easy way to achieve this using no vimscript is the following:
nnoremap <leader>o yyp^v$r A
This assumes you want to start the mapping in normal mode (otherwise just add <Esc> to the ...
- 936
7
votes
How to conveniently add ornate headers to source code?
I actually wrote a blog post about this:
http://rainymood.github.io/jekyll/update/2016/06/25/vim-macro.html
tl;dr use a Vim Macro
Macro used for the image below
qai###<Esc>A<Esc><...
- 253
7
votes
Accepted
Expand <TAB> into 2 spaces when and only when editing HTML
Put this in your .vimrc file:
autocmd BufRead,BufNewFile *.htm,*.html setlocal tabstop=2 shiftwidth=2 softtabstop=2
Briefly, autocmds get processed when the specified events occur for the specified ...
- 19k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
formatting × 185indentation × 25
comments × 15
neovim × 14
wrapping × 14
vimrc × 11
filetype-markdown × 10
line-breaks × 8
vimscript × 7
key-bindings × 7
alignment × 7
autocmd × 6
visual-mode × 6
filetype-tex × 6
insert-mode × 5
filetype-xml × 5
regular-expression × 4
substitute × 4
filetype-python × 4
syntax-highlighting × 3
search × 3
cursor-motions × 3
filetype × 3
options × 3
plugin-coc × 3