1

I need to refactor some paragraphs from an article.

I would like to have:

  • One split with the original version (not editable)
  • Another split with the document to edit

Is it possible?

2
  • I believe it is not possible the modifiable is a buffer characteristic. But I'm not sure I understand your workflow. Could elaborate more on how you plan to use the two splits? Do you want to be able to compare your modification with the original without saving it? Mar 2, 2023 at 14:35
  • 1
    I have a chapter from my PhD thesis and I need to rewrite some paragraphs. I want that in one window the file to be modifiable, so I can delete those paragraphs and write them for scratch, and the other one (the same file) to remain as it was like a snapshot, in order to see the ideas that were there. I could do this by writing the new paragraph above the old one, but maybe I will combine two paragraphs in one longer. So I need to see them as they were.
    – Mafsi
    Mar 2, 2023 at 14:58

2 Answers 2

1

Here is a simpler solution (inspired by the other one)

:let ft=&ft
:vertical new
:setlocal bufhidden=wipe buftype=nofile nobuflisted noswapfile
:read ++edit #
:silent 0d_
:setlocal nomodifiable
:execute "set ft=" . ft
:wincmd p

It:

  1. Copy the file type of the current buffer
  2. Create a vertical split
  3. Set the corresponding buffer a scratch buffer
  4. Read the text into the buffer
  5. Set the corresponding buffer non-modifiable
  6. Set the file type of the new buffer
  7. Delete the original blank line of the scratch buffer (optional)
  8. Move back to the original split (optional)
5
  • Amazing! This is what I want it! A lot of command, but serves the purpose! Thanks a lot!
    – Mafsi
    Mar 2, 2023 at 16:10
  • 1
    this does not prevent editing the buffer. also you may want to capture the filetype and set it after loading the file, so that you can apply correct syntax highlighting. Mar 3, 2023 at 10:29
  • Thanks for your comments :-) I have adapted the solution accordingly. Mar 3, 2023 at 10:56
  • 1
    Putting nomodifiable to :setlocal..., you can't execute read ++edit #. It throws that modifiable is off
    – Mafsi
    Mar 8, 2023 at 8:26
  • 1
    @Mafsi thanks for the remark! I have corrected the answer. Mar 8, 2023 at 9:24
0

To edit safely a file you could do:

  1. Make the file read-only [optional] (depend of your OS)
  2. Open the file in Vim
  3. Make the buffer non read-only [optional] (:set noreadonly)
  4. Use the Diff custom command to compare with the version saved on disk (see bottom of the question for its definition)
  5. Make your modifications

enter image description here

  1. Make the file non-read-only (depend of your OS)
  2. Save the version using :w! [optional]
function! Diff(spec)
    vertical new
    setlocal bufhidden=wipe buftype=nofile nobuflisted noswapfile
        let cmd = "++edit #"
    if len(a:spec)
        let cmd = "!git -C " .. shellescape(fnamemodify(finddir('.git', '.;'), ':p:h:h')) .. " show " .. a:spec .. ":#"
    endif
    execute "read " .. cmd
    silent 0d_
    diffthis
    wincmd p
    diffthis
endfunction
command! -nargs=? Diff call Diff(<q-args>)

Remark: If you don't want the two buffer to be compared we can simplify the solution (by dropping the diffthis calls)

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.