lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Searching a symbol in code


From: Vadim Zeitlin
Subject: Re: [lmi] Searching a symbol in code
Date: Mon, 30 Apr 2018 13:30:42 +0200

On Sun, 29 Apr 2018 22:16:44 +0000 Greg Chicares <address@hidden> wrote:

GC> Then let me describe exactly how I'm exploring this code, and maybe you
GC> can show me a better way. In vim, I just move the cursor to 'column' and
GC> press '*', then use 'n' and 'N'. I don't really want to type a complex
GC> regex to indicate 'except after //'. What more powerful method am I
GC> missing?

 You're not really missing anything, it's just that grep is insufficient
for convenient exploration of C++ code and some dedicated program (dare I
whisper the word "IDE"?) is needed for this. In Vim the best way I know of
is to use a custom function to search outside of comments (and strings,
while we're at it) as explained here:

https://stackoverflow.com/questions/2683521/vim-search-in-c-c-code-lines/2696143

But, in practice, I have to admit that even though I added this function
(with slight modifications) to my .vimrc a few years ago already, I don't
use it that often, probably because I didn't define any convenient mappings
for it, in particular for going to the next match.

 A more convenient method might actually be to use the normal Vim search
commands, without remapping anything, but to fold the comments and remove
"search" from 'foldopen' option to prevent them from being opened when
searching. I've just tried this and it seems to work really well, I think
I'm going to use it more in the future. Of course, you don't see the
comments when doing it like this, but it could be actually viewed as an
advantage because the structure of the code is more clear (closer to the
synopsis-like view you'd like to see) and, of course, you can open any
comment you're interested in using 'zo' command (and close it back using
'zc', or just use 'za' to toggle it). Also, matches in single-line comments
are still found and highlighted (if you have 'hlsearch' on) by default, but
this can be avoided by explicitly setting foldminlines=0: you can then
still see the first (and only) line of single line comment, but it's shown
as a fold, not as a comment, and is not searched. If you'd like to try
this, here are the commands I've just used interactively in my Vim (this is
important as you need to reduce quoting, i.e. remove one backslash from
foldexpr, if you put this into your .vimrc):

        setl foldmethod=expr foldexpr=getline(v:lnum)=~'^\\s*//'
        setl foldopen-=search
        setl foldminlines=0

 But, again, while this can be relatively nice, even a die-hard Vim user
that I am has to admit that IDEs do the job better than Vim here. And while
I never use IDE editor for writing anything, I do use IDE as a code
navigation tool sometimes, especially when working with unfamiliar code
bases as commands such as "Go to definition/declaration" are more powerful
than using ctags when they're powered by the database produced by compiler
itself and "Find all references" provides a convenient way to do what
you're looking to do here.

 Of course, if you don't use IDEs at all (I do use it to build and debug
in any case), it might be a bit of an overkill to start using one just for
this. But there are dedicated code navigation tools as well, e.g.
https://www.sourcetrail.com/ (I've never used this one, but heard positive
things about it; there are undoubtedly others).

 Or maybe one of the Vim-based alternatives above can be good enough. I'm
going to try using the folds-based one for longer, but after playing with
it for 5 minutes, I rather like how it works.

 Regards,
VZ


reply via email to

[Prev in Thread] Current Thread [Next in Thread]