1

Using following .vimrc

" Just to see tab/spaces/endline clearly:
set encoding=utf-8
set list
set listchars=tab:▸\ ,eol:¬
nnoremap <F5> :set list!<cr>

" Tab to spaces settings
set tabstop=4
set softtabstop=4
set shiftwidth=4
set shiftround
set expandtab

" paste and pastetoggle settings
set nopaste
set pastetoggle=<F2>

Now open a test-file and type following: i<TAB><F2><TAB>

  • go to insert mode
  • TAB => gives space
  • to insert-paste mode
  • TAB => gives TAB but i would like spaces

Is it possible to have spaces in the insert-paste mode or do I really have to leave PASTE mode each time again? I just like to stay in paste mode as i copy-paste a lot of snippets constantly from other terminals.

2
  • 3
    paste mode is just a temporary hack, that should make pasting into the terminal much easier. It is not intended to be a permanent mode. And since the whole point of the paste mode is to get every character as pasted, it does not make sense to replace a tab by a space. Have you tried bracketed paste mode (or using the clipboard) instead? Feb 25, 2019 at 9:59
  • Thank you. I think that will do the trick. No more need the paste mode then. Feb 26, 2019 at 2:49

2 Answers 2

2

Yes, you have to.

To make it easier, you could add the following snippet to your vimrc:

set pastetoggle=<F9>
noremap  <F9>   :set invpaste paste?<CR>
inoremap <F9>   <C-O>:set invpaste<CR>

This toggles paste mode by simply hitting F9. This works in normal mode and insert mode.

If you like to use another key, just change <F9> to something else.

BTW: The setting pastetoggle defines a key that should NOT be inserted in the text, but handled normally by Vim. Without this setting, Vim would just insert <F9> in the text if you hit F9 while in insert mode.

5
  • Sorry, this one did not help. Still have <TAB>'s in the paste-insert mode. Feb 26, 2019 at 2:50
  • @aldwinaldwin You question was: "Do I have to leave PASTE mode?". The answer is "yes", and this shows how to toggle PASTE mode with just one key press.
    – Ralf
    Feb 26, 2019 at 5:29
  • @Ralf why add n and i map? is it necessary? what's the meaning of the paste? in n map command?
    – dedowsdi
    Mar 30, 2019 at 2:26
  • @XianDongZhang The i...map to toggle paste while inserting. The paste? shows the status of paste mode. This is not needed in the i-map, as I have showmode set.
    – Ralf
    Mar 30, 2019 at 5:46
  • @Ralf I think i'm spoiled by plugins.
    – dedowsdi
    Mar 30, 2019 at 8:41
2

I only use paste mode when I want to paste the origin text without modifying anything.(like indenting) After pasting, I leave paste mode and starting editing. IMO, you should not hack paste mode, because what it means is only do pasting stuff.

So one workaround is that you convert tab to spaces after finishing all paste stuff. For this, you can try retab.

This is my workflow, I believe that it works in most of the paste and edit scenarios.

# Enter paste mode.
:set invpaste
# Paste something.
...
# Leave paste mode.
:set invpaste
# Highlight tabs.
:hightlight Tabs ctermbg=red
:match Tabs "\t"
# Retab with range for those tabs, or just retab the whole buffer.(I perfer this)
:retab

You can set keybinding and highlighting in your vim config file for simplicity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.