9

When I move lines with m (e.g. :m+1) all my folds are closed. I can then open the folding with zv again and the cursor will be at the position I moved to. I use folding with {{{ }}} marks and have set foldmethod=marker in my .vimrc.

How can I prevent vim from closing folds when moving lines?

dnieder
  • 91
  • 2
  • 1
    I can't reproduce this using vim -u NONE -U NONE -N. It might be an issue with your vimrc or a plugin. – EvergreenTree May 05 '15 at 11:39
  • 2
    Happens for me even with -u NONE -U NONE -N. I use v7.4 on arch linux – dnieder May 05 '15 at 13:39
  • 1
    @EvergreenTree I have the same problem. With vim -u NONE -U NONE -N fold-test and just setting foldmethod=marker. I'm running Vim 7.4.712 on Arch. If I try the same with Vim on Vim 7.4.160 on CentOS 7, I get exactly the opposite results (all folds open when using :m+1). And Vim 7.2.441 on CentOS 6 works fine... All fold* settings seem to be the same on these machines. – Martin Tournoij May 05 '15 at 13:41
  • 1
    That is very strange. This seems like a bug. – EvergreenTree May 05 '15 at 14:35
  • 3
    That might be caused by patch 7.4.700 which tries to prevent, that folds get invalid on :move commands – Christian Brabandt May 05 '15 at 20:36
  • Same issue here with Vim 7.4 on Debian 8. Is this worth a bugreport? – iago-lito Jul 30 '15 at 10:34
  • @ChristianBrabandt, same here. Yes it is the patch without doubt. Do you know how to resolve this problem? – Reman Nov 23 '15 at 07:32
  • 1
    @Reman try setting foldmethod=manual – Christian Brabandt Nov 24 '15 at 13:11
  • @ChristianBrabandt, It still creates problems. I removed the folds at all. I wish that the patch 7.4.700 was not included. – Reman Dec 02 '15 at 07:24

2 Answers2

2

You can disable folding before doing :m+1 by using zi. And then enable it again with zi.

You can also add a special mapping for that in your .vimrc :

""move line up/down with Shift+up/down
nnoremap <S-Up> zi:m-2<CR>zi
nnoremap <S-Down> zi:m+<CR>zi
inoremap <S-Up> <Esc>zi:m-2<CR>zia
inoremap <S-Down> <Esc>zi:m+<CR>zia
statox
  • 49,782
  • 19
  • 148
  • 225
1

Folding is a pretty complicated mechanism (see another answer I gave about folds for a nuanced discussion of folding scanning).

In order to give a complete answer we will need to see what type of plugins you are loading when booting up an instance of vim.

I'd bet that there are a few plugins that are mucking with the default vim settings (vim -u NONE should have reproduced this). Any plugin that changes the foldmethod is a likely cause (I'd try to remove them 1 by 1 and see what causes the issue)

If you don't want to muck with your plugins at all I'd recommend manually tweaking the syntax + parsing settings and then open a test buffer to confirm your functionality. It can be a frustrating journey to get it to play nicely but with some relentless digging in :help you should be able to wrangle the beast that is folding.

Dan Bradbury
  • 1,216
  • 8
  • 10