I've recently discovered I can fold blocks of code with Vim by using zf.Then I can open and close them with zo and zc... I've also seen this solution on StackOverflow that has a vimrc configuration that allows me to automatically save my folds when I close and when I open my files. However, if I copy my folded files with a cp command like:

cp myFoldedFile.js myNewFile.js

I end up losing all my folds in myNewFile.js. I've also tried saving it from inside Vim with :w! /home/user/myNewFile.js but it didn't work as well. In my case, it'd work well if there was a Vim command that I could use from inside it instead of using the command line as my original sample. Is it possible? Is there any way of copying my files and keeping their folds?

  • If you are ok with having fold-markers inside the file, you can use foldmethod=marker
    – D. Ben Knoble
    May 6, 2022 at 20:10
  • @D.BenKnoble Perfect, just using set foldmethod=marker in my vimrc file was enough for what I needed. Thanks!
    – raylight
    May 7, 2022 at 1:34

1 Answer 1


Probably :set[local] foldmethod=marker is the most portable in that it actually denotes folds in the file using special (and customizable) markers. Even programs other than vim can be told (or taught) to recognize these markers. Because the content of the file contains the fold information, copying the file copies the folds.

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.