sacha chua :: living an awesome life

2142 blog subscribers
2744 on Twitter
Subscribe!
E-mail Feed reader

Emacs: Keyboard shortcuts for navigating code

One of the lines on my list of things I can do in order to make progress on my book is to move my Drupal development environment from Eclipse to Emacs, as immersion would no doubt give me plenty of things to tweak and describe. When you use something every day, you notice the rough edges. With Emacs, you can sand those edges down. I already use Emacs every day, but I had been doing most of my development work in Eclipse because Eclipse packaged a number of useful features I wanted. If I can move my environment to Emacs, though, I’ll be able to customize it a lot more freely.

Take something as simple as navigation, for example. In Eclipse, I can hit Ctrl+K to search for the next instance of the current word, which is a handy way to look for function calls or definitions in the same file. How would you do that in Emacs? The built-in search functions allow me to take text from the buffer, but I wanted something even faster. Here are some of the ways you can tweak navigation, too.

If you haven’t tried it yet, you’ll probably like interactive search (C-s) because you can modify the search and see the results as you type. All you need to do to make it better than Eclipse’s Ctrl+K is to add a function to grab the current word, even if the point in the middle of the word. Add the following code to your ~/.emacs:

(defun sacha/isearch-yank-current-word ()
  "Pull current word from buffer into search string."
  (interactive)
  (save-excursion
    (skip-syntax-backward "w_")
    (isearch-yank-internal
     (lambda ()
       (skip-syntax-forward "w_")
       (point)))))
(define-key isearch-mode-map (kbd "C-x") 'sacha/isearch-yank-current-word)

Type C-s (isearch-forward) to start interactively searching forward, and type C-x to get the current word. Use C-s and C-r to search forward and backward. You can modify your search, too.

Want to make it even faster? Use these functions to bind similar searches to shortcut keys:

(defun sacha/search-word-backward ()
  "Find the previous occurrence of the current word."
  (interactive)
  (let ((cur (point)))
    (skip-syntax-backward "w_")
    (goto-char
     (if (re-search-backward (concat "\\_<" (current-word) "\\_>") nil t)
	 (match-beginning 0)
       cur))))

(defun sacha/search-word-forward ()
  "Find the next occurrance of the current word."
  (interactive)
  (let ((cur (point)))
    (skip-syntax-forward "w_")
    (goto-char
     (if (re-search-forward (concat "\\_<" (current-word) "\\_>") nil t)
	 (match-beginning 0)
       cur))))
(global-set-key '[M-up] 'sacha/search-word-backward)
(global-set-key '[M-down] 'sacha/search-word-forward)

Feel free to change the keybindings or otherwise improve the code. =) Good luck and have fun!

So-soHmmGoodGreatAwesome! (No Ratings Yet)
Loading ... Loading ...
Save to - del.icio.us - Digg it - reddit - StumbleUpon -

5 Responses to “Emacs: Keyboard shortcuts for navigating code”

  1. Great one. Been an (x)emacs user for years but just kind of settled into my grind quite a few years ago – your blog is making me play with emacs again.

  2. How about C-w while searching, it calls isearch-yank-word-or-char, isn’t that the same?

  3. It yanks forward, so if you’re in the middle of a word, it only grabs the part that’s in front of the point. =) Mine grabs the entire word, saving you a M-b (backward-word) before you start searching.

  4. Very nice !

    How about highlighting the search term in the last two function sacha/search-word-forward and sacha/search-word-backward.

    Cezar

  5. Or M-. (you need to generate the tags table first with etags)

Discussion Area - Leave a Comment

Please comment as you, not your organization.





On This Day...

  • 2009: Lotus Notes mail merge from a Microsoft Excel spreadsheet — Mwahahahahaha! I’ve been looking for ways to recognize people’s voluntary contributions to community discussions. E-mailed thanks are great because people can [...]
  • 2008: Eclipse to Emacs: Navigating your source tree — Two other things I like about the Eclipse development environment are the ability to jump to a function definition and [...]
  • 2008: Morning pages from Ottawa — In one of her books, Julia Cameron suggested writing morning pages as a way to get one’s creative juices flowing [...]
  • 2007: A Midsummer Night’s Dream — Old readers of this blog would know how special A Midsummer Night’s Dream is to me, of all Shakespeare’s plays. [...]
  • 2007: I’m going to run away and join the circus — When I thought about what I would do if I had all the money I wanted, I realized that trying out [...]
  • 2006: Creative Thursday: Conference commando! — On the way to jazz choir practice, I thought about what I like creating and when people have called me creative. [...]
  • 2006: Planning my week with zones — I think IBM’s Think!Fridays are a great idea. It’s like zoning a day for a particular purpose. I tend to treat [...]
  • 2006: I heart ultraportables! — The power adapter for my Fujitsu Lifebook P1110 gave up two days ago. With the funny way my life works, an [...]
  • 2006: IBM CASCON 2006: Social discovery and conferences — Another thing I want to build for IBM CASCON 2006 is an easy way to create an OPML file for conference [...]
  • 2006: IBM CASCON 2006 and conference backchannels — I got so carried away making lunch that I nearly missed the planning conference call for IBM CASCON 2006’s social [...]
  • 2006: Enterprise 2.0 definition from Andrew McAfee — Via Ross Mayfield comes Andrew McAfee’s description of Enterprise 2.0: / Optional Free of up-front workflow Egalitarian, or indifferent to formal organizational identities Accepting of [...]
  • 2006: Secret knocks — Now that’s a cool application of technology. Seen on Slashdot: “Knock” Some Sense Into Your Linux Laptop describes how to [...]
  • 2006: Speaking of costumes… — Photo taken by Matthew Burpee I have no problems turning up at a high-tech geek get-together about Enterprise 2.0 wearing a [...]
  • 2006: I like someone — I like someone. There, I’ve said it. I probably shouldn’t go into much detail over here, but you can check out [...]
  • 2005: Wahoo! Bikergal! — I’ve signed up with BikeShare, a Community Bicycle Network program that’s like a library for bikes. They plan to turn Toronto into a [...]
  • 2005: Bank account needs social insurance number needs employment contract — Sorting out my finances is harder than I thought. My funding won’t kick in until September, although they’re still paying 12 [...]
  • 2004: Cognitive overload — http://icl-server.ucsd.edu/~kirsh/Articles/Overload/published.html Discovered via http://del.icio.us . I like both much.
  • 2004: What do I really want to focus on for graduate studies? — I think there’s some promise in this planner.el thing. I’m intrigued by the fact that we can make personal information managers [...]
  • 2004: Migration guide for Microsoft Windows to Linux — http://www.linuxgazette.com/node/view/9187 http://www.kbst.bund.de/Anlage304428/Migration_Guide.pdf William Villanueva said: The German Government, specifically the “Bundesministerium des Inneren” (Ministry of Interior Affairs) has published a 444 page Migration-Guide that covers [...]
  • 2003: Configuring SSH behind a proxy — http://www.webweavertech.com/ovidiu/weblog/archives/000017.html
  • 2003: The importance of mentoring — http://www.artima.com/weblogs/viewpost.jsp?thread=7600