219 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, ...
SebMa's user avatar
  • 2,738
91 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
ljs.dev's user avatar
  • 2,507
26 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,...
Mass's user avatar
  • 14k
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 ...
garyjohn's user avatar
  • 6,284
19 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 ...
Gordon Bai's user avatar
17 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. ...
PragmaticProgrammer's user avatar
15 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 ...
8bittree's user avatar
  • 1,487
12 votes

Blocking changes to read only file

Add this to your .vimrc: augroup NoModWhenReadOnly autocmd! autocmd BufRead * let &l:modifiable = !&readonly augroup END
s4y's user avatar
  • 316
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 ...
Mass's user avatar
  • 14k
11 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: ...
Prajit Patil's user avatar
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 ...
Rich's user avatar
  • 31.3k
10 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/...
abcq2's user avatar
  • 101
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 ##, ...
muru's user avatar
  • 24.7k
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 ...
statox's user avatar
  • 49.4k
9 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 =:...
Karl Yngve Lervåg's user avatar
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)...
nobe4's user avatar
  • 15.9k
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.
jmreicha's user avatar
  • 201
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 ...
statox's user avatar
  • 49.4k
8 votes
Accepted

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

The Unix convention is that a line is a sequence of zero or more characters ending with a newline character. A text file is a sequence of such lines. This is just a convention, but adhering to that ...
garyjohn's user avatar
  • 6,284
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}&...
Vivian De Smedt's user avatar
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 ...
Ingo's user avatar
  • 986
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><...
Rainymood's user avatar
  • 273
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 ...
B Layer's user avatar
  • 19.6k
7 votes
Accepted

Avoid breaking words before or after asterisk ('*')

If you use the use 'soft wrap' and 'line break` i.e. set wrap set linebreak set textwidth=0 " To avoid confusion with hard wrap Then Vim splits the lines only at the breakat characters. If you ...
Vivian De Smedt's user avatar
6 votes
Accepted

Auto Indent (the = key) of C switch block

The configuration of C indenting in Vim is controlled by the cinoptions option. That particular aspect of indenting is controlled by the l (lower-case L) flag. When set to any value other than 1, it ...
garyjohn's user avatar
  • 6,284
6 votes
Accepted

Formatting with par and justified option

(Not to let this without an answer; even if a short one) A quick digging on the internet shows the right par option: g1. To quote the source: set formatprg=par\ 72q\ g1 " Use par for gq. use gw ...
VanLaser's user avatar
  • 9,620
6 votes
Accepted

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

From one of my previous answers, you can get this to work if: You have nb:> in your comments setting. And you have q in your formatoptions setting. So: setlocal comments+=nb:> setlocal fo+=q ...
muru's user avatar
  • 24.7k
6 votes

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

Here's a little something a cooked up really fast. It's less robust I think, but it's also much lighter. Maybe you could incorporate it into yours to make it faster? I'll probably revise this a bit ...
Tumbler41's user avatar
  • 7,706
6 votes

How to set the relative indent after breaking Python lines?

The script that controls Vim's indentation of Python files is located at $VIMRUNTIME/indent/python.vim. You can see the help file for it with :help ft-python-indent. From that, we learn there are a ...
Gonçalo Ribeiro's user avatar
6 votes
Accepted

Line number specific text-width setting

Thanks to @Carpetsmoker's comment on my question, I was able to solve this exactly like I wanted it to have. With the following function in .vimrc (or in my case in a special file that it only loaded ...
mxmehl's user avatar
  • 261

Only top scored, non community-wiki answers of a minimum length are eligible