简体   繁体   中英

How to search for a single word in multiple gvim files from the command line?

I want to open up multiple files in different tabs and search for the same word in all of them. Then I want to jump to the first occurrence of the found word in each file.

Doing this works, but it doesn't jump to the first occurrence:

gvim -p -cmd "/word" file1 file2 file3 file4 file5 file6 file7

I need to manually press 'n' to go to the next match.

Vim doesn't provide a way to execute a command for every file on the command line. The -c option causes the command to be executed after the first file is read, and --cmd happens before any files are opened.

If you want to do this, you'd probably need to define a script with a function that did that (say, Search ), load the script with -S , and then execute it with --remote-send option. On many systems, gvim starts up with a default server name by default, but if it doesn't, you'd need to use --servername with your initial process.

Alternatively, you could do this from the command line with grep , which would be more flexible, but of course wouldn't appear in an editor.

Since you're opening the files in separate tab pages, you can use :help:tabdo to execute the search in every page.

If it's okay to just go to the line of the first match, you can directly do the search via :/ :

vim -p -c 'tabdo /word/' file1 file2 file3 file4 file5 file6 file7

To also go to the beginning of the first match within the line, we need something like this, using normal mode n :

vim -p -c "/word" -c 'tabdo 1normal! n' file1 file2 file3 file4 file5 file6 file7

You might be interested in :grep and :vimgrep , which populate the quickfix list:

$ gvim file*
:vimgrep /pattern/ ##

The ## means search the argument list, which you can view with :args . It's what :next and :prev use.

Now, you can navigate the searches with :cnext and :cprev ; or, you can open the quickfix window :copen and hit enter on any line.

You could still pop open all the files in tabs with :cfdo tabedit but at this point it might not be necessary!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM