How to search across a directory of files in vim? - vim

A common programming task for me in vim is:
:s/some pattern/
do some work
n # finds the next entry
do some work
n # finds the next entry
...
Now, s/.... only searches in the current file.
Is there a way I can do this, but search across a directory of files? Say do "s/..../" over all files in subdirectoires of pwd that ends in .hpp of .cpp ?
Thanks!

You can simply use the :grep command: or for a more complete integration of search tools, use the grep.vim extension.
Simply type :help grep to get a nice documentation of what is available out of the box in Vim.
Using :grep foo *.?pp should do what you want.
This will open the QuickFix list, just like the one you get using :make, enabling to jump to the found occurrences.

There is a fantastic plugin named ctrlsf.vim ,which makes you search in vim efficiently.
Here is a snapshot of it.Quite cool ,isn't it?
For instance , If you want search word like "hello" in current dir , after installation , all you need to do is Simply type:
:CtrlSF "hello"
then you'll get what you expect.
Have fun!

:help windo
:help bufdo
man find
man xargs
find ./start/point -iname "*.cpp" -print0 | xargs -0 vim
:bufdo s/baar/foo/gc
:wall
:qall
gc "global confirm", wall (write all), qall (quite all). To see file list :ls top jump to next file :wn (write next) or :bn (buffer next). By the way, xargs manage large chunks of file in memory to deliver to vim withou erros.

Related

Find global references to struct element using VIM

I'm working with the Linux Kernel, so I installed ctags and cscope to help me find references. However, there's no way to find references to struct->element.
Then, I installed coc.nvim and ccls to see if that would help, but I'm only getting local references to struct->element.
Is there a way to find global references to a struct element inside of VIM?
Your best bet is probably the quickfix list, which can be set using
:grep
:vimgrep (handy when you only want to search, say, files in the argument list ##)
any tool for which you can configure either the output to match the default quickfix pattern-matching, or vice-versa
In your case, I would either do
vim -q <(git grep '->element')
Or I would use the git-jump script in git’s contrib folder:
git jump grep '->element'
You could also load up certain files you care about with one of
vim my files...
or
:args my files...
And then search those with
:vimgrep /->element/ ##

Opening file present in another folder which has same relative path as current opened file in vim using vertical diffsplit?

Say I have following 2 similar files in 2 different workspace on same linux machine.
/user1/ws1/ip/src/ip_main.c
/user1/ws2/ip/src/ip_main.c
Now I did,
cd user1/ws1/
vim ip/src/ip_main.c
then I press ESC then executed
:cd ../ws2
which shows my pwd as "user1/ws2"
Now I want to open ip/src/ip_main.c in pwd i.e "user1/ws2"
using ESC
:vertical diffsplit "some logic to get to ws2/ip/src/ip_main.c"
note after :cd ../ws2
:pwd command show "user1/ws2" but :echo $PWD command still show "user1/ws1"
How can i do it,Can anyone help ?
Diffing both files is easily done via shell globbing, e.g. in Bash:
$ vimdiff -O /user1/ws{1,2}/ip/src/ip_main.c
Withing Vim, you have to use relative paths (i.e. prepend ../.., then go down into the other hierarchy). <C-R>% on the command-line (cp. :help c_CTRL-R) inserts the current path; this may avoid retyping much of the similar path, especially when used with the command-line window (:help c_CTRL-F).
With my EditSimilar plugin, you can use this:
:DiffSplitSubstitute 1=2

Indenting in VIM with all the files in Folder

I have a folder containing hundreds of TTL (TeraTermLanguage) files.
Now I wanted indent all these files.
I have created teraterm.vim for indentation and I open a file using VIM and do "gg=G" and whole file gets indented properly.
But is there any way, where I can indent all the files in folder.
I wanted to do with help of Shell. But in VIM I couldnt pass file indent command as the argument to VIM.
Please suggest which is the best way I can do indentation to all the files in VIM.
Much simpler than scripting vim from the bash command line is to use vimscript from inside of vim (or perhaps a much simpler one-liner for scripting vim from the command line). I personally prefer using the arg list for all multi-file manipulation. For example:
:args ~/src/myproject/**/*.ttl | argdo execute "normal gg=G" | update
args sets the arglist, using wildcards (** will match the current directory as well as subdirectories)
| lets us run multiple commands on one line
argdo runs the following commands on each arg (it will swallow up the second |)
execute prevents normal from swallowing up the next pipe.
normal runs the following normal mode commands (what you were working with in the first place)
update is like :w, but only saves when the buffer is modified.
This :args ... | argdo ... | update pattern is very useful for any sort of project wide file manipulation (e.g. search and replace via %s/foo/bar/ge or setting uniform fileformat or fileencoding).
(other people prefer a similar pattern using the buffer list and :bufdo, but with the arg list I don't need to worry about closing current buffers or opening up new vim session.)
Open up a terminal. Type:
$ vim -w indentme.scr foo.c
Then, type this exactly (in command mode):
gg=G:wq
This will close vim, saving the process of indenting all lines in the file to a Vim script called indentme.scr.
Note: indentme.scr will contain a record of all key commands typed, so when you are done indenting the file, don't spend a lot of time using the arrow keys to look around the file, because this will lead to a much larger script and will severely slow down batch operations.
Now, in order to indent all the lines in a file, just type the following command:
$ vim -s indentme.scr unindented-file.c
Vim will flash open-shut (if you're on a fast computer and not editing a huge file), indenting all lines, then saving the file in-place.
Unfortunately, this will only work on one file at a time, but you can scale the functionality easily using sh's for loop:
for filename in *.ttl ; do
vim -s indentme.scr "$filename"
done
Note: This will save-over any file. Unless set bk is in your ~/.vimrc, don't expect a backup to be saved.
I went off of amphetamachine's solution. However, I needed to recursively search through multiple directories. Here's the solution that I used:
$ find . -type f -name '*.ttl' -exec vim -s indentme.scr "{}" \;
Taking reference from the above answers I would like to make this complete.
I will start from the scratch so that a beginner can understand.
Step-1
Install astyle (tool used for formatting ) by using the following command
Open up a terminal. Type:
sudo apt-get install astyle
Step-2 Make sure you have vim installed on your system.Run the below commands from the directory in which your code files are.The below command will create a script that we intend to run recursively so as to beautify each and every file in our directory.(as mentioned in the above answer)
vim -w indentme.scr foo.c
Step-3 Then, type this exactly (in command mode) and press enter
:%!astyle
Step-4Then type this exactly (in command mode) and press enter
:wq
Step-5 Last run this recursively by the following command:
find . -type f -name '*.cpp' -exec vim -s indentme.scr "{}" \;
All your cpp files will be formatted properly.

How can I open several files at once in Vim?

Is there a way to open all the files in a directory from within Vim? So a :command that would say in effect "Open all the files under /some/path into buffers".
Ideally, it would be great to open all the files under a dir recursively.
The command you are looking for is args:
For example:
:args /path_to_dir/*
will open all files in the directory
Why it doesn't work if I want to open all files ending with a certain extension?
I tried
:n ./**.cs
and opens only the files in the currenty directory.
I found the answer.The correct code is :n **/*.cs
For more information :h find
Did you try
:n /some/path/*
It will open all files in /some/path
I don't think it'll open file recursively though.
EDIT
Maybe using ** will open recursively as daf mentionned
A method that doesn't require messing with args is to put the list of files in a text file, and then use the :so command to run the commands in that file.
For example, if you want to open all the files that end in .php in a given directory, first create files.txt containing the list of files, prepended with whatever command you want to use to open them.
sp alpha.php
sp bravo.php
sp charlie.php
Then, within vim:
:so files.txt
If the list of files is large, it's relatively trivial to generate the files.txt file quickly, by redirecting the output of ls to a file, and then using a vim macro to prepend sp before each filename.
This obviously isn't as elegant as using the args and argdo commands, but those commands are also a lot more complicated.
There also might be a way to do this with a single command on the command line, but even after 16 years I still find vim programming to be strange and arcane.
Another way to open files recursively
find . -type f -exec vi {} \;
If you'd like to add to the argument list;
:arga what_you-d_like_to_add
see
:he arga
from/in vim for more information.

How to open a file in a list of files in Vim?

I have a longish list of files opened in vim that looks like this:
/dir1/file1
/dir2/file2
/dir2/file3
.....
How can I open all of them one by one the easiest way possible in the same session of vim either with split or edit?
I'd say with -p for tabs
vim -p `cat yourlistoffiles`
You can use quickfix mode, as following
:set errorformat=%f
:cf myfilelist
at this point you can use the normal quickfix shortcuts to go through your files, :cn for the next file, :cp for the previous one and :cr to go to the first again.
EDIT:
oh, if you want to read the list from the current buffer, use :cb instead of :cf in in the instructions above
You can do the following
cat file | xargs vim
Where "file" contains your list of files, this will open the files in the same vim session. As usual when opening multiple buffers, you can navigate forward with :bn and backward :bp.
I'm going to assume you have the file list open inside Vim, and want to simulate the "gf" command across the whole list...
Edit your .vimrc to include this function:
function Openall()
edit <cfile>
bfirst
endfunction
You can then highlight the entire file (or the set of paths you want to open) using visual mode (1G, Shift-V, G) and typing ":call Openall()". Afterwards the command row will show this:
:'<,'>call Openall()
This will run the new Openall() function across all highlighted lines.
Press Enter and all the files will be opened in background buffers. You can then access them using the usual buffer commands. :ls will display them as a list.
I suppose you want to select and list in vim. all the files of a certain extension. From your home directory or a particular source.
find . -name "*.sh" | vim -
Then within vim, you could search and view this potentially huge list.
(Another topic)
You found your file, now you want to open it in a split?
CTRL-W F *CTRL-W_F*
Split current window in two. Edit file name under cursor and
jump to the line number following the file name. See |gF| for
details on how the line number is obtained.
{not available when the |+file_in_path| feature was disabled
at compile time}
CTRL-W gf *CTRL-W_gf*
Open a new tab page and edit the file name under the cursor.
Like "tab split" and "gf", but the new tab page isn't created
if the file does not exist.
{not available when the |+file_in_path| feature was disabled
at compile time}
For my use case, where you already have a buffer open with the list of files in it, I have found the best way to do this. You can type this in vim cmd mode:
:% normal gf<C-v><C-o>
(where <C-v> is you literally typing Ctrl and v, same for <C-o>)
and it becomes:
:% normal gf^O
How this works:
% runs the command for every line in the file
normal runs everything after it in normal mode
gf opens the file under the cursor in a new buffer
<C-v> lets you enter a control-code (<C-o> in this case)
<C-o> goes back to the last location, which will always be this buffer with the list of files
Potential usage:
# open buffer with list of files from grep
$ grep -rl "new User()" mycode/ libraries/ | vim -
# then runs this in vim:
:% normal gf^O
Bonus: Make a keyboard shortcut
Add the following to your ~/.vimrc :
" Open all files in current buffer (e.g. after piping to `vim -`)
nnoremap <leader>oa :% normal gf^O<cr>
This maps \oa to run the command (since <leader> is \ for me).
An alternate method using a macro:
qogf<C-o>j
which runs gf (go file) then uses <C-o> to get back to the initial unnamed buffer, then just goes down one line with j, ready to be repeated.
Then see how many lines in the buffer (12) and run the macro like this:
12#o
I often need to open a changing list of files that had been modified in my SVN checkout.
This one liner works to open all modified files in vim tabs.
svn st | grep ^M | awk "{print($2)}" | xargs vim -p
Try this with bash:
$ vim -S <(sed "s/^/badd /" <your file name>)
But I don't know why the first line of the file is ignored... :-O
This script works as expected:
rm -f myfile
for i in `seq 10000`
do
touch $i
echo $i >> myfile
done
vi -c "badd `head -1 myfile`" -S <(sed "s/^/badd /" myfile)
http://vimdoc.sourceforge.net/htmldoc/starting.html#-S
http://vimdoc.sourceforge.net/htmldoc/windows.html#:bad
Maybe this could help:
http://vimdoc.sourceforge.net/htmldoc/windows.html
It's as simple as typing
vim /dir1/file1 /dir2/file1 /dir2/file2 ...
Once you're in vim, you can switch betwen then with ":n" to go to the next file, ":prev" to go to the previous file.
My searchInRuntime plugin has a :Sp and a :Vsp commands that'll do the trick when called banged. However, the files have to exist.

Resources