Html not properly formatted when pasting code in Vim

Hi,

A couple of weeks ago I started to use Vim and Tmux. One of the problems I’m having, is that when I paste html-markup from the clipboard into Vim the code isn’t properly formatted.

it takes me lot’s of time to format the html properly.

Is anyone familiar with this problem? How can I fix this?

thanks for your help,

Anthony

Hi Anthony,

I had this happen when pasting in insert mode. Try adding the following to your vimrc file, using your desired mapping, ‘p’ here. Then paste from normal mode with the mapping.

nnoremap <leader>p :r!pbpaste<cr>

(probably from a @christoomey video).

Additionally,

You can also add:

noremap <leader>i mmgg=G`m<cr>

From @benorenstein, this will indent whole file - by dropping a mark named m (mm), go to top of file (gg) and indent (=) all the way to the bottom (G) and return to mark (`m).

1 Like

Hi @acandael, I personally use the pbpaste trick that @Paz231listed, and the reformatting tip is also great. I would also add to the list looking into Vim’s paste mode, which you can read up on by running :h paste. This setting instructs Vim to accept pasted content as text rather than trying to process as keystrokes (which causes the duplicated indentation you see in your screenshot).

@Paz231 @christoomey Thanks guys, I pasted the configuration

nnoremap <leader>p :r!pbpaste<cr>

into my .vimrc file, and now I can past properly with p

greetings,

Anthony

There’s an Upcase video on this: https://thoughtbot.com/upcase/videos/indenting-with-ben-orenstein

1 Like

Thanks Andy, very useful indeed.