├── plugin └── isolate.vim ├── README.md └── autoload └── isolate.vim /plugin/isolate.vim: -------------------------------------------------------------------------------- 1 | if (exists('g:loaded_isolate') && g:loaded_isolate) || v:version < 700 || &cp 2 | finish 3 | endif 4 | let g:loaded_isolate = 1 5 | 6 | command! -nargs=0 -range Isolation ,call isolate#Isolation() 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Isolate 2 | 3 | ## Installation 4 | 5 | * Using [Pathogen](https://github.com/tpope/vim-pathogen), run the following commands: 6 | 7 | % git clone https://github.com/ferranpm/vim-isolate ~/.vim/bundle/isolate 8 | 9 | ## Usage 10 | 11 | call `:[range]Isolation` to edit part of a file on a new buffer. When finished, 12 | call `:Isolation` to merge the edited part with the file 13 | Recommended mapping 14 | 15 | nnoremap i vap \| :Isolation 16 | vnoremap i :Isolation 17 | 18 | ## Demo 19 | Not Updated to show toggling or explicit ranges (`:n,N Isolation`) 20 | [![asciicast](https://asciinema.org/a/18400.png)](https://asciinema.org/a/18400) 21 | -------------------------------------------------------------------------------- /autoload/isolate.vim: -------------------------------------------------------------------------------- 1 | function! isolate#Isolation() range 2 | if exists('b:isolate') 3 | call isolate#UnIsolate() 4 | else 5 | execute a:firstline . ',' . a:lastline . 'call isolate#Isolate()' 6 | endif 7 | endfunction 8 | 9 | function! isolate#Isolate() range 10 | let ft = &filetype 11 | let lines = getline(a:firstline, a:lastline) 12 | let bufnr = bufnr(bufname("%")) 13 | let wasmodifiable = &modifiable 14 | new 15 | set modifiable 16 | execute 'set ft='.ft 17 | let b:isolate = { 18 | \ 'firstline' : a:firstline, 19 | \ 'lastline' : a:lastline, 20 | \ 'bufnr' : bufnr, 21 | \ 'wasmodifiable' : wasmodifiable, 22 | \ } 23 | call setline(1, lines) 24 | if !b:isolate['wasmodifiable'] 25 | echoerr 'Warning source buffer not modifiable!' 26 | endif 27 | endfunction 28 | 29 | function! isolate#UnIsolate() 30 | if !exists('b:isolate') | return | endif 31 | let new_lines = getline(0, line('$')) 32 | let bufnr = b:isolate['bufnr'] 33 | let isolatedbuf = bufnr("%") 34 | let firstline = b:isolate['firstline'] 35 | let lastline = b:isolate['lastline'] 36 | execute 'buffer!'.bufnr 37 | if &modifiable 38 | execute firstline.','.lastline.'delete' 39 | call append(firstline - 1, new_lines) 40 | exe 'bwipeout!' . isolatedbuf 41 | try 42 | close 43 | catch 44 | endtry 45 | if bufwinnr(bufnr) 46 | exe bufwinnr(bufnr) . 'wincmd w' 47 | endif 48 | else 49 | exe 'buffer' . isolatedbuf 50 | echoerr 'Can''t merge with a non modifiable buffer!' 51 | " TODO: ? prompt to have the script set modifiable? 52 | endif 53 | endfunction 54 | --------------------------------------------------------------------------------