Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM, Run a command on multiple files

Tags:

I have a bunch of sass files and my designer used the wrong syntax. I wanted :margin-top 1px but he did margin-top: 1px

So I easily wrote a sub command:

:rubydo sub! /([\w-]+):/,':\1' 

So that works and fixes all the colons and moves them in the place I want. I have about 50 sass files in a stylesheets directory. How can I run this command on all my files in one shot? I'm not really sure how to go about this in vim. Maybe something to do with grep? I couldn't tell ya.

like image 776
Andrew WC Brown Avatar asked Aug 19 '11 20:08

Andrew WC Brown


People also ask

How do I navigate multiple files in Vim?

Using windows. Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l ) to navigate through open windows. Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one. Starting vim with a -o or -O flag opens each file in its own split.

Can Vim open multiple files?

Opening Multiple Files in VimYou can open multiple files at the start of the Vim editing session from the command line, or at any time from inside the Vim editing session.

How do I open files side by side in Vim?

Suppose you have opened a file on Vim editor and you want to split it vertically. To achieve this: Enter command mode by pressing the ESC button. Press the keyboard combination Ctrl + w , followed by the letter 'v' .

Can you run commands from Vim?

You can run commands in Vim by entering the command mode with : . Then you can execute external shell commands by pre-pending an exclamation mark ( ! ). For example, type :! ls , and Vim will run the shell's ls command from within Vim.


2 Answers

See this: http://vimdoc.sourceforge.net/htmldoc/editing.html#:argdo

I learned this command right now, but the help is clear.

Go for:

:args *.css :argdo %s/\([[:alpha:]-]\+\):/:\1/ge | update 
like image 142
Niloct Avatar answered Sep 30 '22 19:09

Niloct


Here is an example

:bufdo %s/oldStuff/newStuff/ge | update 

just change your regex to fit your needs

like image 27
Eric Fortis Avatar answered Sep 30 '22 20:09

Eric Fortis