Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Astro Coke

[Vi] Huge delay when copying & pasting contents in folded texts 본문

Computer Setup

[Vi] Huge delay when copying & pasting contents in folded texts

astrodoo 2019. 9. 13. 22:23

source: https://vim.fandom.com/wiki/Keep_folds_closed_while_inserting_text

 

Setting 'foldmethod' to syntax causes two annoying problems: a general slow-down of Vim when inserting/completing text, and folds which go "SPROING!" every time you insert text which creates a new fold.

 

The first problem is caused by Vim's syntax folding being so slow compared to other methods; see :help todo.txt and search for "folding with 'foldmethod'".

 

The second problem applies to any automatic folding method—be it marker, syntax, or expression folding—inserting text which starts a fold will automatically open all folds beneath the insertion point.

 

To work around both issues, you can temporarily switch to a manual fold method when entering insert mode, and switch back when leaving it. This will save any currently open/closed folds while in insert mode without creating new folding regions, and restore your preferred settings when leaving insrt mode. Presumably you will enter both the opening and closing of a fold in one insertion; this method is not all that effective otherwise.

 

To accomplish the workaround, use the InsertEnter and InsertLeave autocmd events. Since 'foldmethod' is a window-local option, store the old value in a window-local variable:


" Don't screw up folds when inserting text that might affect them, until 
" leaving insert mode. Foldmethod is local to the window. 
autocmd InsertEnter * let w:last_fdm=&foldmethod | setlocal foldmethod=manual 
autocmd InsertLeave * let &l:foldmethod=w:last_fdm

This is probably good enough in most situations, but remember that some autocmds or mappings, or use of the mouse (gasp!) might cause Vim to switch windows without leaving insert mode. This will cause problems without a little bit more effort; we could restore the wrong fold method, leave foldmethod at manual permanently in another window, or even get an error when leaving insert mode. The following should work better:


" Don't screw up folds when inserting text that might affect them, until 
" leaving insert mode. Foldmethod is local to the window. Protect against 
" screwing up folding when switching between windows. 
autocmd InsertEnter * if !exists('w:last_fdm') | let w:last_fdm=&foldmethod | setlocal foldmethod=manual | endif 
autocmd InsertLeave,WinLeave * if exists('w:last_fdm') | let &l:foldmethod=w:last_fdm | unlet w:last_fdm | endif

Note that secondary windows open on the same buffer in the same tab page will not be protected, since foldmethod is a window-local option. Probably a :windo command could also solve the issue in this case if desired.