VIm Grep on raspberry pi - vim

i want to grep search with vim editor on raspberry pi, Rasbian.
in windows, i usually use findstr and command like this
grep -s xxxx *.c
how do i grep search with vim editor on os Rasbian?

If you wanna grep, just without leaving Vim, you can use :!cmd to run grep as an external command: :!grep xxx *.c
Vim has :grep; it also uses the external grep (or whatever is configured in 'grepprg'), but parses the results and presents them in the quickfix list, which allows you to quickly navigate results inside Vim.
If you don't have / don't want to use external grep, there's also the :vimgrep variant. You have to use Vim's regular expression syntax, and it'll be slower because files are loaded into Vim.
Within a buffer, there are many alternatives, like :global or :ilist. With :bufdo, you can run them on all opened buffers.

Related

How to add user command to vi/vim to have multiple conditions to search the content?

Our servers only have vi/vim to check the log files and I feel searching in vi is painful and limited.
I wonder if it's possible to provide an executable file in the path and then use the vi user command to call it and the user command is able to accept multiple conditions like A=value1 && B=value2, A=value1 || B=value2, etc and then it searches the content in the open vi editor and places the cursor in matching text.
If it's possible please describe a bit the steps.
If what you have on your machines is actually Vim, you should be able to leverage the "quickfix" feature. In a nutshell, you can:
feed a list of locations to Vim,
optionally tell Vim how to parse that list,
go through that list with commands like :cnext or :lprevious,
display that list in a special window,
batch operate on every line in the list.
In the simplest scenario, your hypothetical external program would output a list formatted in a way Vim already understand, like:
filename.txt:3067:12:some text
filename.txt:4321:7:some text
which could be fed "directly" to Vim:
$ vim -q <(yourcommand)
# also open the quickfix window
$ vim -q <(yourcommand) +cwindow
or via some file:
$ vim -q yourfile
It can all be done from within Vim by telling it to use your program instead of grep for the :grep command:
set grepprg=yourcommand
and doing:
:grep <your arguments>
References:
:help quickfix
:help :cnext
:help :lprevious
:help -q
:help -+c
:help :cwindow
:help :grep
:help 'grepprg'
:help 'grepformat'

Is there a way to perform a cli command while editing a file in NERDTree(vim)?

I'd like to do a grep -r -l "index.php" while inside vim. I am using the NERDTree plugin too. I've tried the command :! but after this it states
Press ENTER or type command to continue
I try to type a command in and it immediately reverts back to vim.
Inorder to run an external command, you can prefix it with :!
In your case,
:!grep -r -l "index.php"
ie, your supposed to type the command(s) after :! while you're in vim itself.
Using vimgrep, you can do
:vimgrep /\cindex\.php/ **
Do check the examples given in vim-tips-working-with-external-commands
but after this it states "Press ENTER or type command to continue"
This is the expected result. For grep command, you can see the results on top of this line.
Do checkout vimgrep, Silver Searcher etc for a better workflow. Some links to familiarize them
Faster Grepping in Vim
Search multiple files with :vimgrep
Without plugins:
:vimgrep \cindex.php **
(\c forces ignorecase; ** is "all files in this directory and below")
With ack and Ack.vim:
:Ack -i index.php
With the_silver_searcher and Ag.vim:
:Ag -i index.php
You can also add in NERDTree integration using NERDTree-Ack and NERDTree-Ag.

Format code with VIM using external commands

I know that using VIM I can format C++ code just using
gg=G
Now I have to format 30 files, so doing it by hand becomes tedious. I had a look how to do it passing external commands to VIM, so I tried
vim -c gg=G -c wq file.cpp
but it does not work.
Can you give me a hint?
Thanks
Why not load all the files up in buffers and use bufdo to execute the command on all of them at one time?
:bufdo "execute normal gg=G"
Change -c gg=G to -c 'normal! gg=G'. -c switch accepts only ex mode commands, gg=G are two normal mode commands.
I prefer a slight change on the :bufdo answer. I prefer the arg list instead of the buffer list, so I don't need to worry about closing current buffers or opening up new vim session. For example:
:args ~/src/myproject/**/*.cpp | 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).

Open all files that match a certain pattern in Vim

I’m in ~/src. I can do git grep _pattern_ and get a list of all *.cpp or *.hpp files that match this pattern.
Now I would like to go through all the files that match the pattern and make edits on them. How do I do this in Vim? (Basically, I want Vim to go through my directory like git grep does, and jump me to the right files.)
You can use the single inverted commas (also a unix shell feature), something like:
vim `git grep --name-only <your expression>`
In bash, you could do
vim $(grep -l _pattern_ *.cpp *.hpp)
but that's a bash feature, not a vim feature.
you can use the args ex command:
:args *.cpp *.hpp
This will open all cpp and hpp files in the current directory.
You can use any file path expansions available to :grep as well.
You could possibly set the grepprg and grepformat options to run git grep... and interpret the result. This would then let you run the command :grep and read the results into the quickfix buffer - see :h quickfix for more information. You can then step through them with :cnext and :cprev, or :copen to open a separate window with the list of files - putting the cursor on a filename and pressing return will open that file for editing.
The advantage of this over Zoran's and ammoQ's suggestions is that it will not read the files into memory until you want to edit them. Their suggestion will load possibly hundreds of files into memory at once, and can be a nightmare to manage. It is also cross platform so should work on Windows without having to use a third-party shell such as cygwin bash.
By properly using the quickfix list, you can even go immediately to the the right line (using the :help quickfix commands, eg. :cn or :cw). So, if you are using bash or zsh:
vim -q &lt(git grep foo)

redirection to a file of a search in Vim

My problem is simple. I search a specific pattern in a file (let's say label in a Tex file)
:g/label/#
but there are lots of occurrences. So I'd like to redirect this output to another file to be able to work easily with it.
Do you have a trick or a command that I don't know?
it's not clear from the original post what you mean by "work easily with it" but it's often useful to see and quickly jump between all of the matches in a buffer without "extracting" the matches to a separate buffer.
vim has an internal grep built in. your example would be something like this (in vim, % denotes the current file)
:vimgrep /label/ %
This will take you to the first occurrence and report how many matches there were. What's cool is that you can look at all of the matches listed by opening up the quickfix error list using
:cope
Now you can just scroll around and press enter on a line to jump to the exact position of the match.
The quickfix error list is exactly the same buffer you use if you run make from inside vim and your compiler throws errors: it gives you a list of what and where the errors are.
After you've jumped to one location pointed by quickfix, you can go to forwards and backwards in the list via :cn and :cp. :ccl closes the error list.
You can also expand your "error" list via :vimgrepa /newpattern/ % or :vimgrepadd
The (documented) caveat is that vim's internal grep is slower than most native grep implementations (but you do get it "for free" in windows, for example). If you do have a grep installed, you can use :grep instead of :vimgrep for similar results.
quoting :help grep
Vim has two ways to find matches for a
pattern: Internal and external. The
advantage of the internal grep is that
it works on all systems and uses the
powerful Vim search patterns. An
external grep program can be used when
the Vim grep does not do what you
want.
The internal method will be slower,
because files are read into memory.
The advantages are:
- Line separators and encoding are automatically recognized, as if a file
is being edited.
- Uses Vim search patterns. Multi-line patterns can be used.
- When plugins are enabled: compressed and remote files can be searched.
You can also use the location list if you're already using the error list for dealing with compilation errors. just add l (for location) to the beginning of the grep command (:lvimgrep,:lvimgrepa :lgrep, :lgrepa) and use :lopen :ln :lp :lcl instead of the :c* ones.
For more commands consult
:help grep
:help quickfix-window
:help quickfix
:help quickfix-error-lists
:redir > matches.txt|execute 'g/foo/#'|redir END
See :h :redir, you can also redirect to registers, variables, the clipboard etc.
What you're doing is essentially 'grep -n label file' from command line. So you can run that command and > it into a file easily enough.
The derivation of 'grep' is even from basically the same source.
I've gotten this of the net at some point:
function GoToLine(mainbuffer)
let linenumber = expand("<cword>")
silent bd!
silent execute "buffer" a:mainbuffer
silent execute ":"linenumber
silent nunmap <Enter>
endfunction
command -nargs=1 GoToLine :call GoToLine(<f-args>)
function GrepToBuffer(pattern)
let mainbuffer = bufnr("%")
silent %yank g
enew
silent put! g
execute "%!egrep -n" a:pattern "| cut -b1-80 | sed 's/:/ /'"
silent 1s/^/\="# Press Enter on a line to view it\n"/
silent :2
silent execute "nmap <Enter> 0:silent GoToLine" mainbuffer "<Enter>"
" silent nmap <C-G> <C-O>:bd!<Enter>
endfunction
command -nargs=+ Grep :call GrepToBuffer(<q-args>)
Put it in your .vimrc, then :Grep Foo
Requires external grep program to work properly.
(Just an idea -- untested.)
You can delete all the lines with your pattern in it, write to another file, and undo the delete.
:g/label/d
:w matches
u

Resources