Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for all uppercase words in vim?

Tags:

regex

vim

I would like to search for all uppercase words in a file but I have no idea how to do it (or if it's possible). I found this solution here on stackoverflow, but it doesn't work on vim.

like image 643
sica07 Avatar asked Jun 04 '12 13:06

sica07


3 Answers

From command mode, assuming you do not have the option ignorecase set:

/\<[A-Z]\+\>

or

/\v<[A-Z]+>

Finds any string of capital letters greater than length one surrounded by word boundaries. The second form uses 'very-magic'. :help magic for details

like image 134
William Pursell Avatar answered Sep 28 '22 00:09

William Pursell


The shortest answer: /\<\u\+\>

like image 28
agudulin Avatar answered Sep 28 '22 01:09

agudulin


If you want a list of all the matching uppercase words (i.e. you aren't interested in jumping from one word to the other), you can use:

echo filter(split(join(getline(1, '$'), ' '), '\v(\s|[[:punct:]])'), 'v:val =~ "\\v<\\u+>"')

With:

  • getline(1, '$') that returns a list of all the lines from the current buffer
  • join(lines, ' ') that flattens this list of lines
  • split(all_text, separators_regex) that build a list of word-like elements
  • and finally filter(words, uppercase-condition) that selects only the uppercase words.
like image 39
Luc Hermitte Avatar answered Sep 27 '22 23:09

Luc Hermitte