in a typical c++ file i have many functions and many a times i needed to search a keyword/variable or carry out a search and replace operation … however it becomes very confusing if multiple functions exist that use the same variable names.
often times i keep hitting C-s and i never can know if i m still within the function i started in or ventured in to a different function altogether … this is more confusing since i use long functions spanning many screenfuls of code 😦
a good thing that i discovered is which-function-mode that i talked about here … which can be helpful but one has to always keep a watch on the modeline …
but i wanted to confine the search or search and replace within the scope of a function definition … turns out the all powerful emacs can do that and it does it well i think 🙂
first …
emacs has the ability to make every thing else invisible … everything else but your region of interest … the stuff won’t disappear it will simply be not visible… of course u can make it visible again at the click of a few key chords … here is how you do it
first select the region of interest if you simply want to select the entire function simply hit the following keychord within the body of function definition
C-M-h
then to make everything else disappear hit the following
C-x n n
you might have to first all the usage of this if you haven’t used this before.. apparently it confuses some beginners so they disable it by default and warn u on its first use. onwards now …
now that everything else has become invisible… the search and replace commands miraculously behave as if the other stuff does not exist … buffer movement keys also behave as if the selected region alone exists in the buffer … lucky for us this is precisely what we wanted … after you are done with search and or replace simply hit the following key chord
C-x n w
and everything else will come back !!!
of course its not limited to c++ files alone
http://www.gnu.org/software/emacs/manual/html_node/emacs/Narrowing.html
Leave a Reply