Paste from insert mode <Ctrl-r>0
You can also insert the yanked text from insert mode when autoindent has added the correct indentation:
yiw yank
o<CR> switch to insert mode and add new line
<Ctrl-r>0 paste text from register 0
Drew Neil has created a vimcast
I would also recommend two articles by Drew Neil:
Note pasting a code block in insert mode
If you want to paste not a single word but a code block,
this answer might not be satisfactory. Replace in step 1 yiw with yG and then execute steps 2 and 3, you will end up with
function Scene:get(...)
function Scene:get(...)
local l = {}
for i, e in ipaires(self.nodelist) do
if (e:is(...)) then
l[#l +1] = e
end
end
return l
end
local l = {}
for i, e in ipaires(self.nodelist) do
if (e:is(...)) then
l[#l +1] = e
end
end
return l
end
To fix this you would have to set paste after you switched to insert mode to take advantage of the autoindentation because paste disables autoindentation.
I do not know a simple way to do this and would recommend to paste from normal mode with the help of vim-unimpaired (=p). Also note yanking into the system clipboard and using the bracketed paste feature does not work for me flawlessly (macOS, Terminals: iTerm2.app v3.1.7 or Terminal.app v2.7.4).
inoremap <return> <return>yo<backspace><backspace>and it works as expected! i'm not sure if this is exactly what you meant but it worked. but still if i press<esc>the cursor goes back a full tab, but if i enter insert mode again it went back to the correct indent, and the indent seems actually there (can be selected), i don't understand why in normal mode it's ignoring the last indent.. – tga Jul 21 '18 at 03:10<esc>puts the indent back. For me, after<esc>the indent is gone, and pressingiputs you at the beginning of the line. – Shahbaz Jul 23 '18 at 13:52<enter><enter>to create an empty line between different pieces of your code, the empty line is gonna have the indent whitespace. In git, if you setcore.whitespacetoblank-at-eolfor example, you're gonna have a bad time. – Shahbaz Jul 23 '18 at 13:57taball the way is straight up bad experience haha – tga Jul 23 '18 at 14:33p==A. This will paste what I want to paste, fix the indentation of the current line, and then go to insert mode at the end of the line. – Shahbaz Jul 23 '18 at 15:44o<CTRL-R>0to paste the last yanked text and not mess with the indent. See also the various options at:h i_CTRL-R– Christian Brabandt Jul 24 '18 at 17:08