15

Maybe is a often repeated question here, but i can't find anything similar with the search. The point is that i like to use Emacs for my personal projects, usually very small applications using C or python, but i was wondering how to use it also for my work, in which we have project with about 10k files of source code, so is veeeery big (actually i am using source insight, that is very nice tool, but only for windows), questions are:

  • Searching: Which is the most convenient way to search a string within the whole project?
  • Navigating throught the function: I mean something like putting the cursor over a function, define, var, and going to the definition
  • Refactoring

Also if you have any experience with this and want to share your thoughts i will consider it highly interesting.

Br

Daniel M.
  • 676
  • 1
  • 9
  • 24

8 Answers8

10

The "traditional" way of navigating C source files is to use "etags" to make a file called TAGS, then use ALT-. to go to functions across files.

For searching for strings in files, I usually use "grep". You could make a shell script with all the directories you want to search or something if you get tired of typing them in each time.

  • 3
    Actually, etags stands for emacs tags, which is just one of the formats that exuberant ctags can produce. When exuberant ctags is invoked using the name etags, it produces emacs format tag files. When it is invoked using the name ctags, it produces ctags format files. Of course, you can also pass it command-line options to control which format is used regardless of the name used to invoke the tool. – jamessan Apr 02 '10 at 13:05
  • very interesting, any special hint for using grep in more convenient way, i mean i found very slow to enter every time the grep options and search path – Daniel M. Apr 02 '10 at 14:27
  • OK I removed the bit about exuberant. –  Apr 02 '10 at 16:26
9

My projects typically live in git, so I put this together to quickly search them:

;; There's something similar (but fancier) in vc-git.el: vc-git-grep
;; -I means don't search through binary files
(defcustom git-grep-switches "--extended-regexp -I -n --ignore-case"
  "Switches to pass to `git grep'."
  :type 'string)

(defun git-grep (command-args)
  (interactive
   (list (read-shell-command "Run git-grep (like this): "
                             (format "git grep %s -e "
                                     git-grep-switches)
                             'git-grep-history)))
  (let ((grep-use-null-device nil))
    (grep command-args)))
offby1
  • 6,767
  • 30
  • 45
  • If anyone's reading this and doesn't have their project in git, I find ack is nicer than grep for searching code. It doesn't search as fast (though it's not much slower on a pretty big codebase) but it is much faster to type "ack blah" than "find . -name "*.cc" -o -name "*.h" -exec grep blah {} \;" :-) – Matt Curtis Apr 04 '10 at 08:49
  • How about M-x tags-search instead of grep? – Eddy Pronk Apr 04 '10 at 11:26
  • The problem with M-x tags-search is that you must first create a TAGS file, and occasionally that's difficult. For example, I don't think etags will do anything sensible with a Visual Studio build file, but I want to search through those, too. – offby1 Apr 04 '10 at 20:58
6

There is also the Emacs Code Browser. It makes exploring projects a lot simpler. See here and here for more information.

pmr
  • 58,701
  • 10
  • 113
  • 156
4

Regarding searches in the whole project, I find extremely useful the rgrep command.

Also, imenu is quite handy to jump to a function definition in the same file.

These are my 2p.

Roberto Aloi
  • 30,570
  • 21
  • 75
  • 112
3

look to EDE from CEDET - it provide base support for projects...

Alex Ott
  • 80,552
  • 8
  • 87
  • 132
3

ECB is too heavyweight for my taste. I have had good results with xcscope. Needless to say it doesn't help too much with Python.

http://www.emacswiki.org/emacs/CScopeAndEmacs

daladd
  • 86
  • 2
2

In addition to using TAGS as others have mentioned, I find igrep and igrep-find very useful. There is also Emacs' built in grep and grep-find, but I find their interface more clumsy.

My standard search is:

M-x igrep-find some_regexp RET ~/work_area/*.cxx

Which will look for all *.cxx files under ~/work/area, and show results matching some_regexp. Like all the search utilities, it populates a compilation-like buffer you can navigate using C-x ` (aka M-x next-error).

Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
2

There are many ways that Icicles can help with projects. Likewise, Bookmark+ and even Dired+.

These libraries can help you create, organize, and manage projects, wherever their files and directories might reside. And they can help you navigate and search in various ways.

Some of the features are unique -- quite different from other approaches. I could list some of the project support here, but this is the best place to start.

Drew
  • 29,895
  • 7
  • 74
  • 104