Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/qxxxb/vim-searchhi

Highlight the current search result differently
https://github.com/qxxxb/vim-searchhi

Last synced: 28 days ago
JSON representation

Highlight the current search result differently

Lists

README

        

# vim-searchhi

Highlight the current search result in a different style than the other search
results.

![Demo gif](https://raw.githubusercontent.com/qxxxb/vim-searchhi/assets/demo.gif)

## Credits

This plugin would not have existed without [vim-searchant]. It uses the same
basic implementation for highlighting the current search result.

## Features

- Smooth integration with standard search as well as other search-enhancing
plugins (e.g. [vim-anzu], [vim-asterisk]).

- Uses a custom highlight group for the cursor when it's inside a search result.

- Behaves appropriately in Visual mode.

- Highlighting is updated predictably when the cursor is moved, as well as
when switching buffers and windows. It can also be toggled with custom
autocommands.

- User autocommands are provided and executed when highlighting is turned on
and off.

**Note**: This plugin uses a lot of `` mappings. An alternative plugin is
[vim-searchlight], which has the same basic functionality but doesn't require
any mappings.

## Quick start
```vim
nmap n (searchhi-n)
nmap N (searchhi-N)
nmap * (searchhi-*)
nmap g* (searchhi-g*)
nmap # (searchhi-#)
nmap g# (searchhi-g#)
nmap gd (searchhi-gd)
nmap gD (searchhi-gD)

vmap n (searchhi-v-n)
vmap N (searchhi-v-N)
vmap * (searchhi-v-*)
vmap g* (searchhi-v-g*)
vmap # (searchhi-v-#)
vmap g# (searchhi-v-g#)
vmap gd (searchhi-v-gd)
vmap gD (searchhi-v-gD)

nmap (searchhi-clear-all)
vmap (searchhi-v-clear-all)
```

Integration with [vim-anzu]:
```vim
let g:searchhi_user_autocmds_enabled = 1
let g:searchhi_redraw_before_on = 1

augroup searchhi
autocmd!
autocmd User SearchHiOn AnzuUpdateSearchStatusOutput
autocmd User SearchHiOff echo g:anzu_no_match_word
augroup END
```

Example with [vim-asterisk]:
```vim
map * (asterisk-*)(searchhi-update)
map # (asterisk-#)(searchhi-update)
map g* (asterisk-g*)(searchhi-update)
map g# (asterisk-g#)(searchhi-update)

map z* (asterisk-z*)(searchhi-update)
map z# (asterisk-z#)(searchhi-update)
map gz* (asterisk-gz*)(searchhi-update)
map gz# (asterisk-gz#)(searchhi-update)
```

## Customization

### Highlight style

The current search result is highlighted with `CurrentSearch`, and the cursor
when it's inside a search result is highlighted with `SearchCursor`. Example:
```vim
highlight CurrentSearch
\ cterm=reverse,bold ctermfg=108 ctermbg=235
\ gui=reverse,bold guifg=#8ec07c guibg=#282828

highlight link SearchCursor WarningMsg
```

By default, `CurrentSearch` is linked to `Incsearch`, which works nicely if your
`Incsearch` and `Search` highlight groups are visually distinguishable.
`SearchCursor` is linked to `Normal` by default.

### Autocommands

The autocommands `SearchHiOn` and `SearchHiOff` are executed when highlighting
is turned on or off. Below is an example that blinks the cursor when search
highlighting is turned on, making the cursor easier to find. [vim-anzu] is also
used to echo the search count.
```vim
let g:searchhi_user_autocmds_enabled = 1
let g:searchhi_redraw_before_on = 1

augroup searchhi
autocmd!

autocmd User SearchHiOn
\ set guicursor=
\c-sm:block,i-ci-ve:ver25,r-cr-o:hor20,
\n-v:block-blinkwait20-blinkon20-blinkoff20 |
\ AnzuUpdateSearchStatusOutput

autocmd User SearchHiOff set guicursor& | echo g:anzu_no_match_word
augroup END
```

### Autocommands for toggling search highlighting

Highlighting for all search results can be toggled with custom autocommands.
Example:
```vim
let g:searchhi_clear_all_autocmds = 'InsertEnter'
let g:searchhi_update_all_autocmds = 'InsertLeave'
```

**Note**: Using `CursorMoved` for these will cause issues. If you want search
highlighting to be cleared as soon as the cursor moves, use this:

```vim
let g:searchhi_clear_all_asap = 1
```

Because `g:searchhi_clear_all_asap` and `g:searchhi_clear_all_autocmds` both
use a hack that directly sets `nohlsearch`, you may also need to use the
following maps to make sure that `incsearch` works properly when searching:

```vim
nmap / (searchhi-/)
nmap ? (searchhi-?)

vmap / (searchhi-v-/)
vmap ? (searchhi-v-?)
```

[vim-searchant]: https://github.com/timakro/vim-searchant
[vim-anzu]: https://github.com/osyo-manga/vim-anzu
[vim-asterisk]: https://github.com/haya14busa/vim-asterisk
[vim-searchlight]: https://github.com/PeterRincker/vim-searchlight