Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I see changes before I save my file in Vim?

Tags:

vim

diff

preview

People also ask

Where is Vim history stored?

It's in the file . viminfo (or _viminfo if you are on Windows). It should be in whatever passes for your home directory.

How do I jump to the beginning of a file in Vim?

Type "gg" in command mode. This brings the cursor to the first line.

How do I save after making changes in Vim?

The command to save a file in Vim and quit the editor is :wq . To save the file and exit the editor simultaneously, press Esc to switch to normal mode, type :wq and hit Enter .


:w !diff % -

Because some people asked about an explanation for the command

:w !diff % -

Here is my attempt on writing a more detailed answer:

I am assuming that you are working on a system with cat and echo installed (e.g. almost any GNU/Linux, Mac OS, BSD and other UNIX-like systems).

The above command works as follows:

  1. The syntax for saving a file in vim is:

    :w <filename>
    
  2. The syntax for executing a shell command in vim is:

    :!<command>
    
  3. Inside the shell environment issued by vim % happens to point to the current filename. You can verify this by executing the following:

    :!echo %
    

    This should output the filename (or an error, if vim was run without a filename).

    Using cat we can also output the content of the file:

    :!cat %
    

    This should return the files content in its last saved state or an error if it has never been saved.

  4. The program diff is able to read from standard input (stdin). Its man page states the following:

    [...] If a FILE is '-', read standard input. [...]

  5. Executing the save command without a filename but rather a shell command behind it causes vim to write the files content to stdin of the shell instead of saving it in a physical file. You can verify this by executing

    :w !cat
    

    This should always print the files current content (which would have been written to a file instead).

Putting it together (or tl;dr): The file is "saved" to stdin, diff is run with the filename and stdin as input.

Knowing this one could also compare files with vimdiff doing something like this - this is just an idea you do not want to do this:

:w !cat > /tmp/tempFile && vimdiff /tmp/tempFile % && rm /tmp/tempFile

(Then open readonly and close vimdiff using :qall)


http://vim.wikia.com/wiki/Diff_current_buffer_and_the_original_file

Here is a function and command to see a diff between the currently edited file and its unmodified version in the filesystem. Just put this in your vimrc or in the plugin directory, open a file, make some modifications without saving them, and do :DiffSaved.

function! s:DiffWithSaved()
  let filetype=&ft
  diffthis
  vnew | r # | normal! 1Gdd
  diffthis
  exe "setlocal bt=nofile bh=wipe nobl noswf ro ft=" . filetype
endfunction
com! DiffSaved call s:DiffWithSaved()

To get out of diff view you can use the :diffoff command.

Below is a similar function, adapted to mimic the 'cvs diff' command...


I've always likes diffchanges - nice, simple, works.


from vimrc_example.vim:

" Convenient command to see the difference between the current buffer and the
" file it was loaded from, thus the changes you made.
if !exists(":DiffOrig")
  command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis
          \ | wincmd p | diffthis
endif

git supports the following command

:w !git diff --no-index -- % -

map it to a command by adding the following to your ~/.vimrc

command GitDiff execute "w !git diff --no-index -- % -"

Now executing :GitDiff becomes a handy little command to quickly show the diff before each save.


Source the following and use :DIFF command

function! s:diff()
    let tmpa = tempname()
    let tmpb = tempname()
    earlier 100h
    exec 'w '.tmpa
    later 100h
    exec 'w '.tmpb
    update
    exec 'tabnew '.tmpa
    diffthis
    vert split
    exec 'edit '.tmpb
    diffthis
endfunction
command! -nargs=0 DIFF call <SID>diff()