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.)
: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 :-)