Autoindent on paste

Hello. I want my code to get indented when i paste it but I can’t get that beahaviour. My settings are:
set autoindent
set cindent

I’ve read that neovim has the option bracketed-paste-mode by default which should do the job if the terminal supports bracketed paste. I’m using kitty terminal and I believe it’s supported. Can you help me spot the problem.

Take a look at :h i_CTRL-R_CTRL-P. This allows you to insert the contents of a register and auto-indent the result.

This means you can paste from your clipboard with autoindent using <C-R><C-P>+ in Insert mode.

1 Like

First, thanks a lot for the answer.

I don’t know how to use that option, am I supposed to map it to a key combination? Also, it seems like it only works in insert mode I was looking for a solution that works in normal mode too.

I don’t know how to use that option, am I supposed to map it to a key combination?

The <C-R><C-P>+ syntax means you press the keys “Ctrl-R” followed by “Ctrl-P” followed by “+” while in insert mode. You can map it to another key if you wish but it’s not necessary.

I was looking for a solution that works in normal mode too.

The = key will re-indent a range in normal mode. E.g. using =10j will re-indent the next 10 lines (see :h = for more). The '] mark is placed at the last line of yanked or changed text. We can combine these two by using ='] (that is, press =, followed by ', followed by ']). This will re-indent all lines from the current cursor position to the end of the last changed block (likewise, '[ will move you to the beginning of the last changed block).

That’s probably as good as you’ll get for a built-in solution. As for bracketed-paste, if you read the help text for :h 'paste' you’ll see the following

	When the 'paste' option is switched on (also when it was already on):
		- mapping in Insert mode and Command-line mode is disabled
		- abbreviations are disabled
		- 'autoindent' is reset
		- 'expandtab' is reset
		- 'hkmap' is reset
		- 'revins' is reset
		- 'ruler' is reset
		- 'showmatch' is reset
		- 'smarttab' is reset
		- 'softtabstop' is set to 0
		- 'textwidth' is set to 0
		- 'wrapmargin' is set to 0
		- 'varsofttabstop' is made empty
	These options keep their value, but their effect is disabled:
		- 'cindent'
		- 'formatoptions' is used like it is empty
		- 'indentexpr'
		- 'lisp'
		- 'smartindent'

This is why your autoindent and cindent settings are not working.

1 Like

It might be useful to look into ]P (see :help ]p), which doesn’t reindent everything that you paste, but does respect the current indentation level. So you can use ]P to paste at the current indent level, followed by = to reindent whatever you pasted, if necessary.

Thanks for the explanation.

But I don’t understand because as posted in this forum:

Others seem to get this behabiour as default and I should be able to archive it using :set nopaste.

This is somewhat useful but doesnt quite solve the problem. Anyway, if this can be done archiving wright indentation should be possible.

1 Like