azabiong / vim-highlighter

Highlight words and expressions
MIT License
237 stars 11 forks source link

Question: How to replace builtin n, N and * commands. #12

Closed aminroosta closed 2 years ago

aminroosta commented 2 years ago

How do I replace vim's builtin commands such as *, n and N with this plugin.

let HiSet = ')'
nmap * )
nn n  <Cmd>Hi><CR>
nn N  <Cmd>Hi<<CR>
nn ( :nohl<CR>:Hi clear<CR>

So far I have the above bindings and it works great, except when I search with /something<cr>.

azabiong commented 2 years ago

I think this is a very interesting idea! 😄 I tried this mapping and it works! 👍

The original design was to use both native search and highlight jumps to enrich search options.

So, how about this one.

While hlsearch is displayed, the 'n' and 'N' keys perform their original functions. When hlsearch is not displayed, for example after running the noh command, the keys would perform jump to highlight functions. For this, I expect it will only require 'n' and 'N' key mappings to a new function, like HiSearch().

What do you think?

azabiong commented 2 years ago

Now HiSearch() function supports n and N mapping.

nn n  <Cmd>call HiSearch('n')<CR>
nn N  <Cmd>call HiSearch('N')<CR>

And, if there is no key mapping for the :noh command:

nn <Esc>n  <Cmd>noh<CR>

That's it 😄

You can now easily switch between native search and jump mode. When switching to jump mode, you can simply turn off hlsearch using the :noh command.

Now you probably don't need the * key mapping.

aminroosta commented 2 years ago

@azabiong You made my day! I love the fact that HiSearch() now supports n and N mappings 🎉.

" I changed it a bit, and now it's exactly what I wanted.
let HiCursorGuide = 0
let HiSet = '*'
nn n  <Cmd>call HiSearch('n')<CR>
nn N  <Cmd>call HiSearch('N')<CR>
nn ( :nohl<cr>:Hi clear<cr>
azabiong commented 2 years ago

Great!   I updated documents and added your name to the Credits section to thank you for suggesting this very interesting and useful feature.

 :h Hi-Credits
aminroosta commented 2 years ago

For anyone else finding this issue via google. I've gone through a few iterations, and here are my latest mappings, You might find them useful.

" Disable vim's builtin hlsearch highlighting
set nohlsearch

" Disable the cursor guide
let HiCursorGuide = 0

" Highlight the word under the cursor -- `\<' and `\>` are added to match full words only.
nn [oh :Hi + \<<c-r><c-w>\><cr>

" Highlight the text last entered in the '/' register
nn [oH :Hi + <c-r>/<cr>

" We want to keep using the built-in 'n` and 'N' mappings.
" Use the built-in '*' (without affecting the jumplist) and highlight the word under the cursor.
nmap * :keepjumps normal! mi*`i<cr>[oh

" Highlight the word after the command mode search is complete.
cmap <expr> <cr> getcmdtype() == '/' ? '<cr>[oH' : '<cr>'

" Mapping to clear active highlights
nn ( :nohl<cr>:Hi clear<cr>

hi HiColor1 ctermfg=22  ctermbg=194 cterm=bold guifg=#004f00 guibg=#d7f7df gui=bold
hi HiColor2 ctermfg=52  ctermbg=229 cterm=bold guifg=#371700 guibg=#f7f7a7 gui=bold
hi HiColor3 ctermfg=17  ctermbg=195 cterm=bold guifg=#000057 guibg=#afd7fc gui=bold
hi HiColor4 ctermfg=19  ctermbg=189 cterm=bold guifg=#0000af guibg=#d7d7fc gui=bold
hi HiColor5 ctermfg=52  ctermbg=221 cterm=none guifg=#570000 guibg=#fcd757 gui=none
hi HiColor6 ctermfg=17  ctermbg=113 cterm=none guifg=#001767 guibg=#8fd757 gui=none
hi HiColor7 ctermfg=53  ctermbg=219 cterm=none guifg=#570027 guibg=#fcb7fc gui=none
hi HiColor8 ctermfg=89  ctermbg=159 cterm=bold guifg=#87005f guibg=#fcd7fc gui=bold
hi HiColor9 ctermfg=225 ctermbg=90  cterm=none guifg=#ffdff7 guibg=#8f2f8f gui=none
hi HiColor10 ctermfg=195 ctermbg=68  cterm=none guifg=#dffcfc guibg=#5783c7 gui=none
hi HiColor11 ctermfg=52  ctermbg=180 cterm=bold guifg=#570000 guibg=#dfb787 gui=bold
hi HiColor12 ctermfg=223 ctermbg=130 cterm=bold guifg=#fcd7a7 guibg=#af5f17 gui=bold
hi HiColor13 ctermfg=230 ctermbg=242 cterm=bold guifg=#f7f7d7 guibg=#676767 gui=bold
hi HiColor14 ctermfg=194 ctermbg=23  cterm=none guifg=#cff3f3 guibg=#276c37 gui=none