Saturday, December 01, 2012

Regex to match the word under cursor

The VIM-specific regex below matches the word under cursor. (Pasting unmodified as it is in my vimrc to also match German letters.)

/[a-zA-Z0-9ßÄÜÖäüö]*\%#[a-zA-Z0-9ßÄÜÖäüö]*

Documentation is under ':h /\%#'

Example usage: enclose the word under cursor in 'em' tag. Best experience if that is triggered on a keyboard shortcut.

:s![a-zA-Z0-9ßÄÜÖäüö]*\%#[a-zA-Z0-9ßÄÜÖäüö]*!<em>\0</em>!

Negative side-effect: causes fancy behavior of a seemingly random word to be highlighted when 'set hls' is in effect.

4 comments:

Nikola Valentinov Petrov said...
This comment has been removed by the author.
Nikola Valentinov Petrov said...

reason why you are not using


expand('<cword>>)


or even better


<C-R><C-W>

when doing the substitution?

Best, Nikola

Ihar Filipau said...

Nikola, welcome to the Blogspot - worst blogger platform for the technical discussions! Gotta html-quote everything here.

[ Oh, you updated your comment. ]

The expand() can give you the current word - but what can you do about it but to look at it? '\%#' allows you to actually DO something with the word. And 100% guaranteed to be the word under cursor - not any other instance of the word on the same line.

^R^W isn't particularly useful for similar reason: it is rather for the interactive use, it's hard to automate and in the end it is as unrealiable as the expand().

Additionally, it is not limited to the word. Word-under-cursor is probably the most popular application, but the regex actually can be easily extended, for example, to include punctutation or word before/after. Or match only part of the word. And so on and so forth.

Nikola Valentinov Petrov said...

Ah I see makes sense now. I just thought that for your example with those options are perfectly valid and I would use them instead.

P.S. Hate blogspot - they don't even provide an option for pre tags...

Best, Nikola