Vim Tips
- Find and replace in multiple files
- Save as root
- Perl compatible regular expressions
- Removing duplicate lines
- To be continued…
That will be a page for some useful Vim tips which I have searched for once and just want to have them kept somewhere.
Find and replace in multiple files
The simplest example would be the following
:args `find . -name '*rb'`
:argdo %s/pattern/replacement/ge | update
First the args
command will collect all the files found by the find
to the arglist
.
Then the argdo
command will iterate accross all the file names from the arglist
and apply the changes.
The update
is here to apply changes to all the files without asking.
If you have a larger project
You can tune the find
command a bit to skip specific directories. For example, the node_modules
and log
directory
usually contain the significant amount of files which are generated automatically and are usually not stored to the
version controll system. So we can just skip them from the arglist
and the replacement will be performed
significantly faster.
To exclude a specific file you can add this modifier to the find
command:
-path 'directory-to-exclude' -prune -false -o
.
The full example for excluding both node_modules
and log
will look like following:
:args `find . -path '**/node_modules' -prune -false -o -path '**/log' -prune -false -o -type f -name '*rb'`
:argdo %s/pattern/replacement/ge | update
We can narrow the search area even
By using the grep
together with the find
command. You will just need to duplicate you search pattern
two times.
So that you get a prefiltered set of files in arglist
which consists only of those which have the search pattern in
them. The -exec grep -l 'pattern' {} \;
modifier at the end will do the trick:
:args `find . -path '**/node_modules' -prune -false -o -path '**/log' -prune -false -o -type f -name '*rb' -exec grep -l 'pattern' {} \;`
:argdo %s/pattern/replacement/ge | update
The extra output also decreases the speed significantly
You can prefix the argdo
with a silent!
command which will gobble up the messages from stdout:
:args `find . -path '**/node_modules' -prune -false -o -path '**/log' -prune -false -o -type f -name '*rb' -exec grep -l 'pattern' {} \;`
:silent! argdo %s/pattern/replacement/ge | update
Save as root
If you have opened a root owned file for editing while using the non-root user session, this command will help you to save the file as a root user:
:w !sudo tee %
Explanation:
-
!
executes the follow-up command directrly in shell -
tee
is a shell command which redirects the write output of a stream -
%
is a vim specific variable that stores the current filename
So the command will take the output of the file save (:w
) and put it to the file which is called same as the file
you’re currently editing but as a sudo
user.
Perl compatible regular expressions
Just place the \v
before the regex and it will do the trick.
/\v(any-templat[e]*)/replace-$1/ge
Removing duplicate lines
So simple is that
:sort u
To be continued…
Will continuously populate it with more tips in future