Search code examples
bashsvnvimdiffvimdiff

VIM - Passing colon-commands list via command-line


Good day,

I am writing a simple script within my BASHRC file to accommodate something I couldn't quite resolve in a previous question:

Side-by-side view in Vim of svn-diff for entire directory

I basically generate a list of all files which have a "Modified" SVN status. For each of these files, I want to create a side-by-side visual diff, convert it to HTML, then append it to a running HTML file.

eg:

MODIFIED_FILES="$(svn status | grep "^M" | cut -c9-)"
for i in ${MODIFIED_FILES}; do
  # Generate a side-by-side diff in vim via VIMDIFF
  # Convert via ToHTML
  # Append the HTML file to a file called "overall_diff.html"
done

I can accomplish the vimdiff easily enough by creating a clean copy of the file, and having a copy of the modified file.

vimdiff has an issue at first, ie:

2 files to edit
Error detected while processing /Users/Owner/.vimrc:
line   45:
E474: Invalid argument: listchars=tab:>-,trail:.,extends:>,precedes:«
Press ENTER or type command to continue

So, I am trying to get past this so I don't have to hit ENTER for each file in my list.

Next, I need to have vimdiff call the ToHTML command, and then issue the command to append the HTML buffer to a running file:

:'<,'>w! >>overall_diff.html

In short, how do I:

  1. Get past this issue with listchars when vimdiff is called. This issue doesn't occur when I run vim, so I don't know why it occurs when I run vimdiff.
  2. Pass a list of colon-commands to VIM to have it run them at startup without requiring a change to my .vimrc file.

Solution

  • In the end, I created a separate VIMRC file that gets passed to the vim command at run time, via:

    `vim -d file1 fil2 -u my_special_vimrc_file`
    
    function createVimDiff()
    {
       # Create some buffers
       TEMP_FILE="./tmp_file"
       VIM_TEMP="./temp.html"
       REVISION=""
       BUFFER_FILE="./overall_diff.html"
       # Get a list of the files that have changed
       MODIFIED_FILES="$(svn status | grep '^M' | cut -c9-)"
       # Remove buffers
       rm "${BUFFER_FILE}"
       for i in ${MODIFIED_FILES}; do
          # Remove intermediate buffers
          rm "${TEMP_FILE}"
          rm "${VIM_TEMP}"
          # Get the current SVN rev number for the current file
          REVISION="$(svn info ${i} | grep Revision)"
          # Echo the name of the file to the report
          echo "FILE: ${i}" >> "${BUFFER_FILE}"
          # Same with the revision number
          echo "${REVISION}" >> "${BUFFER_FILE}"
          echo "<br>" >> "${BUFFER_FILE}"
          # First print a copy of the unmodified file in a temporary buffer
          svn cat "${i}" > "${TEMP_FILE}"
          # Now print the unmodified file on the left column, and the
          # modified file in the right column, so they appear side-by-side
          vim -d "${TEMP_FILE}" "${i}" -u ~/.vimdiff_rc
          # Write the side-by-side diff to a file
          cat "${VIM_TEMP}" >> "${BUFFER_FILE}"
          echo "<br>" >> "${BUFFER_FILE}"
       done
       # Cleanup temporary buffers
       rm "${TEMP_FILE}"
       rm "${VIM_TEMP}"
    }
    

    And the following was put into my VIMRC file:

    " Convert the diff to HTML
    autocmd VimEnter * silent TOhtml
    
    " Write output to temporary buffer
    autocmd VimEnter * w! ./temp.html
    
    " Quit VIM
    autocmd VimEnter * qa!