Skip to content

Instantly share code, notes, and snippets.

@cognitivegears
Last active October 25, 2016 02:01
Show Gist options
  • Save cognitivegears/a854e6b45a29b8fed007c3b05c757fe4 to your computer and use it in GitHub Desktop.
Save cognitivegears/a854e6b45a29b8fed007c3b05c757fe4 to your computer and use it in GitHub Desktop.
Using Search Results Window with Unite and Anzu

Synopsis

With Unite and Anzu (and a little vimscript) it is possible to create a window showing all matching search results that appears automatically when searching.

See Search Results Window with Unite and Anzu for the code or below for a demonstration.

Search Windows

Usage

Search as normal with /, *, etc. When the search is executed, a new window will appear with search results. By default this window is not focused, allowing you to continue searching as normal. If you want to go through the list items, switch to the window (I use my arrow keys for this, but you can use the default mappings as well. The execute action is to jump to that line of the file and return focus, keeping the search window available. To close the search window (and remove the current highlighting,) press Esc.

Further narrowing of the search results is possible by pressing i in the search window and typing search criteria (similar to other Unite sources.)

Motivation

When switching to using VIM for my daily code editor, I have found myself missing some IDE features. This is probably the biggest one that I was not able to find a pre-packaged solution for without tinkering.

Installation

Requirements:

Install Anzu and Unite with your package manager of choice, and source in the script or add to your .vimrc. That's it!

Alternative

As a lightweight alternative, if you do not want to use Unite and Anzu, below is a no-plugin version that uses the location list and built-in lvim to display the results.

Thanks to /u/aerobug on reddit for the idea!

autocmd VimEnter * call ClearLastSearch()
function! ClearLastSearch()
  let @/ = ''
endfunction

let g:saved_search = ''
autocmd CursorHold * call CheckSearch()
function! CheckSearch()
   if g:saved_search != @/
    let g:saved_search = @/
    lvim // %
    lop
  endif
endfunction

Technical Details

This script uses two tricks that I haven't seen before that may be of interest. First, closing Unite windows if they are the last one left. In order to take advantage of this, you must first name the buffers with the -buffer-name parameter, then can check and close it if it is the last one like this:

autocmd BufEnter * if (winnr("$") == 1 && unite#get_unite_winnr("searchList") != -1)|q|endif

Second, responding to a search command. Vim does not have a "search start" event (that I could find) so I had to emulate it, using the CursorHold event to call a function which compares the last search result with a saved result:

autocmd CursorHold * call CheckSearch()
function! CheckSearch()
  if g:saved_search != @/ && !has("vim_starting")
    " Save the current search term
    let g:saved_search = @/
    " .... Handle new search here
  endif
endfunction

License

MIT License

Copyright (c) 2016 Nathaniel Byrd

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment