vim - how to enable searching in vim editor and finding/highlighting pattern that is only in non-commented block/line

62 views
Skip to first unread message

Annis Monadjem

unread,
Aug 24, 2015, 3:12:30 AM8/24/15
to vim_use
Hi,

I am looking for a way to search in vim editor and to find text highlighted only for pattern found in non-commented blocks/lines.

I often use *, #, g*, g# but these find every pattern also in commented blocks/lines of code.

Also, I frequently use plugins CltrP, CTags and taglist but these also have the same problem of finding every possible found pattern also in commented blocks/lines of code. Additionally, the Ctrl-] and Ctrl-T find the original point where a variable was declared (in Java).

When writing and reading through Java source code I find it necessary to search for where in the file(s) a variable has been declared, initiated and perhaps modified. Often variables/functions are modified not once and initialization might not be done same place where it has been declared.

What current I do is to find the declaration of an existing variable by using Ctrl-] then i come back using Ctrl-T. Then to see where it has been perhaps initiated and changed I use * and #. But these last two find all patterns also in commented blocks/lines (where compiler does not read and where I am not interested to scroll/read).

Is there a more productive way of searching/highlighting patterns of text that appear in a file only in non-commented blocks/lines.

Thanks in advance,
Annis

Ben Fritz

unread,
Aug 24, 2015, 11:21:44 AM8/24/15
to vim_use

For this, I use an IDE, though there may be other ways.

Specifically, I use Eclipse, along with Eclim, to allow me to navigate the results in Vim and just keep Eclipse running in the background.

http://eclim.org

Charles Campbell

unread,
Aug 24, 2015, 4:39:41 PM8/24/15
to vim...@googlegroups.com
Nothing perfectly attuned, but some notes:

* First, just in case you're not aware, :set hls will turn highlighting
on for searched patterns
* One can avoid some comments with LogiPat: :LP "your-pattern-here" &&
!"/\*" && !"\*/" && !"//" (this is line based, however)
(http://www.drchip.org/astronaut/vim/index.html#LOGIPAT)
* Dave Fishburn's SrchRplcHiGrp.vim plugin
(http://www.vim.org/script.php?script_id=848) can search and replace for
specified highlighting groups. Perhaps you could use it to do a
decorate, match/highlight your pattern not having the decoration
(perhaps with LogiPat), then remove the decoration. The decoration
should be something unlikely to occur in your file (@@@, for example).

Regards,
Chip Campbell

David Fishburn

unread,
Aug 25, 2015, 9:46:36 AM8/25/15
to vim_use
On Sun, Aug 23, 2015 at 7:02 PM, 'Annis Monadjem' via vim_use <vim...@googlegroups.com> wrote:
Hi,

I am looking for a way to search in vim editor and to find text highlighted only for pattern found in non-commented blocks/lines.

...
 
Is there a more productive way of searching/highlighting patterns of text that appear in a file only in non-commented blocks/lines.


Annis,

It depends on what level of functionality you are looking for.

My plugin, 

SrchRplcHiGrp.vim  - Search and Replace based on a highlight group

SR = Search and/or Replace

This plugin defines some commands to allow you to: 
1.  Search for characters with a particular syntax highlight group 
2.  Search and replace characters with a particular syntax highlight group 


Allows you to choose the syntax group for comments and then perform searches that are either contained within that syntax group or outside of that syntax group.

Have a quick look at that plugin page, there is an example at the bottom, where I specifically look for things that are NOT comments.

HTH,
David

romainla...@gmail.com

unread,
Aug 25, 2015, 11:23:14 AM8/25/15
to vim_use
> Is there a more productive way of searching/highlighting patterns of text that appear in a file only in non-commented blocks/lines.

The commands at :help include-search do exactly that.

:ilist foo

will list:

bar baz foo
foo bar baz

but not:

// foo does this

The "d" variants of those commands are particularly useful for programming.

This might need a bit of setup, though, but not much.

FWIW, I wrote http://www.vim.org/scripts/script.php?script_id=5214 to be able to use the quickfix list/window with :ilist, :dlist and friends.

lith

unread,
Aug 27, 2015, 1:21:37 PM8/27/15
to vim_use
> I am looking for a way to search in vim editor and to find text highlighted only for pattern found in non-commented blocks/lines.

With the trag[1] plugin you could do:

:Trag -i PATTERN

This will search all files in the current project though and not just the current buffer.

[1] https://github.com/tomtom/trag_vim

Reply all
Reply to author
Forward
0 new messages