Steve Amerige
Steve Amerige

Reputation: 1499

In vim, how to get * to highlight and yank words including special chars (@):,^

In vim 8.0, for .sh files, I want to press * to highlight the word under the cursor, copy the word into the yank buffer, and I want words to include the characters:

( @ ) : , ^

in addition to the alphanumeric and underscore characters. I also want a second press of the * to clear the highlighting. I note that the default for iskeyword is:

iskeyword=@,48-57,_,192-255

Here's what I have in my ~/.vimrc file:

function! Highlighting()
   if g:highlighting == 1 && @/ =~ '^\\<'.expand('<cword>').'\\>$'
      let g:highlighting = 0
      return ":silent nohlsearch\<CR>"
   endif

   let @/ = '\<'.expand('<cword>').'\>'
   let g:highlighting = 1
   return ":silent set hlsearch\<CR>yiw"
endfunction

set iskeyword+=(,@,),:,,,^

let g:highlighting = 0

nnoremap <silent> <expr> <CR> Highlighting()

It works for the most part. What's not working is that words are not including the characters:

( @

but they are including the others, including the closing parenthesis. Here are some words that should get matched:

(@)_name
(@):name
(@)_name_name
(@):name^name
(@):name,name

Can anyone suggest a fix that allows this to work with words including all of the characters I want in a word? Please feel free to suggest a completely different implementation or plugins that solves my needs.

Upvotes: 1

Views: 360

Answers (1)

Steve Amerige
Steve Amerige

Reputation: 1499

The answer is to do the following in the ~/.gvimrc file:

set iskeyword+=(,@-@,),:,,,^

To get more information on why the idiom @-@ is needed, do in vim:

:help isfname

Upvotes: 2

Related Questions