146 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, ...
user avatar
  • 1,968
80 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 ...
user avatar
65 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
user avatar
  • 1,717
24 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 ...
user avatar
  • 7,240
21 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,...
user avatar
  • 12.8k
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 ...
user avatar
  • 5,934
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 ...
user avatar
  • 1,628
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 ...
user avatar
15 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 ...
user avatar
13 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. ...
user avatar
13 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: Provide static type checking from a Python virtualenv (e.g. I have created a venv called coc with pyenv, which resides in ~/....
user avatar
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 ...
user avatar
  • 12.8k
11 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 ...
user avatar
  • 1,357
10 votes

Blocking changes to read only file

Add this to your .vimrc: autocmd BufRead * let &l:modifiable = !&readonly
user avatar
  • 286
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:...
user avatar
  • 3,879
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 ##, ...
user avatar
  • 23.9k
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 ...
user avatar
  • 46.5k
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/...
user avatar
  • 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.
user avatar
  • 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)...
user avatar
  • 15.2k
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 ...
user avatar
  • 46.5k
8 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 ...
user avatar
  • 28.2k
8 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: ...
user avatar
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 ...
user avatar
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-...
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 ...
user avatar
  • 916
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><...
user avatar
  • 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 ...
user avatar
  • 18.4k
6 votes

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

If this is a regular occurrence, you'd be best off looking for a plugin or using @Sukima 's function. If I were doing this on the fly however, here's what I'd probably do: Add a newline after the ...
user avatar
  • 2,868
6 votes

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

Oh that's a tough one. I think the best approach is possibly a macro but more likely a plugin. Here is an example I whipped up (I need a better hobby). It seemed to work for me but would need the ...
user avatar
  • 1,152

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