Skip to main content
249 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
  • 3,068
97 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,767
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
  • 14.2k
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
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,546
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
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
12 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
  • 14.2k
11 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
  • 32.2k
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

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
10 votes

How to disable autoformating on save on LazyVim?

I received a warning using the above answer. Looks like it should be set in ~/.config/nvim/lua/config/options.lua vim.g.autoformat = false
davidfmatheson's user avatar
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
  • 50.1k
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

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
  • 50.1k
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,369
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
8 votes
Accepted

How to disable autoformating on save on LazyVim?

The auto-formatting on save is a LazyVim functionality associated to the nvim-lspconfig plugin. To disable it you have to create a lua file in ~/.config/nvim/lua/plugins folder with the following ...
Vivian De Smedt's user avatar
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
  • 20k
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

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,756
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
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

simple map to right align text selection within visual block?

Here's a simple mapping to do the task: vnoremap <silent> rs "zy:call setreg("z", system("echo \"" . @z . "\" \| sed 's/[ \\t]\\+//' \| rs -j 0 1"), "b")<CR>gv"zp It uses z register to ...
grodzik's user avatar
  • 4,658
6 votes

Remove new line from result of system()

You probably want to use :h systemlist(): systemlist({expr} [, {input}]) *systemlist()* Same as system(), but returns a List with lines (parts of output separated by NL) ...
statox's user avatar
  • 50.1k
5 votes
Accepted

"Change" as much whitespace as necessary

Have you tried the R operator? This will let you overwrite whatever text you need to without moving the |. If your word is shorter than what you're replacing, you can simply fill the rest with spaces. ...
Tumbler41's user avatar
  • 7,756
5 votes
Accepted

gqip for VHDL comments does not work

You should tell Vim what a comment is using: :set comments+=:--,b:-- Explanation: The comments option contains a comma separated list of strings that can start a comment line. :-- specifies the ...
Jair López's user avatar
  • 1,854
5 votes
Accepted

Vim: plugin to change all decimal points?

Why don't you define a command from this line? Drop it in $HOME/.vim/plugin/yourpluginname.vim, and you're done. (at least, this is enough for something that simple) command! -range=% ChangeDotToComa ...
Luc Hermitte's user avatar
  • 17.5k
5 votes

How to format JSON file in Vim

I use this :Jsonf command. It's can format unicode. command! Jsonf :execute '%!python -c "import json,sys,collections,re; sys.stdout.write(re.sub(r\"\\\u[0-9a-f]{4}\", lambda m:m.group().decode(\"...
han xi's user avatar
  • 51

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