8

Referring to clean-up tasks like (but not limited to) the ones detailed in these questions:

is there a way to apply those commands only the lines of the files which were actually modified? This is for keeping the diff output focused on the actual change after editing source code (a potential code-reviewer would not be confused by the changes involved in cleaning the whole file), while not introducing new formatting problems.

6
  • vim.wikia.com/wiki/… ... Might be a good place to start ... No time to write a full answer now ;-) Feb 6, 2015 at 0:11
  • @Carpetsmoker thx for the link
    – guido
    Feb 6, 2015 at 0:26
  • I did some more digging, and I don't think this is possible. :changes doesn't work without a lot of hackery; as it seems this information is stored in the viminfo file, and there's no way to see which changes you made this session, and which the previous one ... Unless you disable the viminfo file of course, but that's rather heavy handed ... would like to be proven wrong here, though :-) Feb 6, 2015 at 14:50
  • @Carpetsmoker I don't think the change list contains enough information to implement this feature, even if you could isolate the changes made in each session. It contains a single cursor position for each change, so you can't tell the extent of the edit that was made.
    – Rich
    Feb 9, 2015 at 13:59
  • @Rich Yes, I came to the same conclusion... Feb 9, 2015 at 14:01

1 Answer 1

4

You can use the command folddoopen to execute a command only on the lines that are not within a closed fold.

For example, you can remove whitespace from the end of lines with the command:

:%s/\s\+$//

To apply this only to the lines that are currently not within a closed fold, add folddopen, like this:

:%folddoopen s/\s\+$//

folddoopen can also be abbreviated to foldd.

If you're using Vim as your source control's diff tool, then you can use this to apply edits only to changed lines whilst viewing the diff.

Note that by default, Vim's diff mode displays a few lines before and after each changed section. So in order not to affect lines that are immediately before or after changed lines you'll need to first set context in diffopt to 0 e.g.:

:set diffopt=filler,context:0

For manual edits, you might also find the foldopen option to be useful. It defines which types of commands will cause folds to open. Although note that it does not prevent editing from taking place within the folds in the way that the folddoopen command does.

(If you mean something else by "the lines of the files which were actually modified" then you're going to have to find a way of folding away the unchanged lines before you run the command: the easiest way is to keep an unchanged copy of the file and use vimdiff manually.)

2
  • 2
    This is a very creative idea! I didn't know about :folddoopen, seems like a very useful command. I also wanted to offer that if using fugitive.vim, you can more easily see the diff of your current buffer with the :Gdiff command.
    – tommcdo
    Feb 8, 2015 at 12:19
  • This work nicely; I can open a scratch buffer with the previous revision diffed to the current with :vsp new, then :read !git show HEAD~1:path and finally :windo diffthis (and then using your solution).
    – guido
    Feb 9, 2015 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.