grep over ssh in emacs

M-x grep command is very useful in a number of situation, because, for example, it give you direct access to all matched file …

But there is not so intuitive how to use grep when opening file via ssh, this is the trick:

First generate a key pair and upload public part to the remote server:
(suppose remote host/user is B/b)

ssh-keygen -t rsa
ssh b@B mkdir -p .ssh
cat ~/.ssh/ | ssh b@B 'cat >> .ssh/authorized_keys'

be sure to use ssh-agent in order to use ssh-add

So you will not be asked for a password every time you ssh

Every time you want to grep in emacs:

M-x grep
Run grep (like this): ssh -t b@B 'grep -nH -e include path_to_files/*'

where path_to_file is the path relative to home in remote host

NOTE: do grep command when visiting remote home in emacs (otherwise it will not find matched files)

that’s all