Giter Club home page Giter Club logo

vim-cool's Introduction

vim-cool

Vim-cool disables search highlighting when you are done searching and re-enables it when you search again. That's it. No more :noh<CR>, no more /sytdstdrsid<CR>, and no more dodgy <C-l> mappings.

Vim-cool is cool.

Vim-cool is experimental.

cool

Requirements

Vim-cool is intended to be used with Vim, and only Vim, 7.4.2008 or later. It may or may not work in other editors but they are not and will not be officially supported.

Installation

Follow your favorite plugin/runtimepath manager's instructions.

If you choose manual installation, just put plugin/cool.vim where it belongs:

$HOME/.vim/plugin/cool.vim        on Unix-like systems
$HOME\vimfiles\plugin\cool.vim    on Windows

In Vim 8.0 and above, see :help package.

Setup

The whole assumption behind Vim-cool is that the user enabled search highlighting but they don't want the highlighting to linger on when they are done searching. This implies that the user has the following line in their vimrc:

set hlsearch

That's it. Nothing else to do.

Experimental features

  • Show number of matches in the command-line:

    let g:cool_total_matches = 1
    

    demo

  • Do something when we are doing nnnNNnn, do something else or do nothing when we are not:

    set statusline+=%{get(g:,'cool_is_searching',0)?'Yep':''}
    

Background

I wrote the first iteration of vim-cool in about twenty minutes, mostly to test a few ideas I had after a short discussion about 'hlsearch' and :nohlsearch on #vim.

Because it relied almost exclusively on mappings, that first iteration was way too brittle to be of any use and actually messed with a bunch of my own mappings.

Then came @purpleP and the game-changing approach he put together with the help of @chrisbra, @justinmk, @jamessan, and @ZyX-I.

The current version, essentially a weaponized version of @purpleP's code, doesn't rely on mappings anymore and thus should be devoid of nasty side-effects.

Many thanks to @bounceme for his help.

What they say about vim-cool

  • puremourning, in #vim:

    vim-cool is by far my favourite plugin

    it's just so... cool.

vim-cool's People

Contributors

a5ob7r avatar benhormann avatar bounceme avatar romainchapou avatar romainl avatar vzytoi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vim-cool's Issues

Make highlighting on a word optional

I have been looking for ages for vim-cool! However, I would like the highlighting when the cursor is on the search term to be optional; I often use search for just two character searches (when there is no good landing spot for F/f/T/t), and in those cases it gets annoying.

foldopen

set foldopen+=search is being ignored because the search commands are remapped. maybe something like &foldopen =~ search to add the zv mapping?

vim-cool doesn't seem to work in my setup

I'm doing searches using /{characters} and while I do see the current match highlighted, I don't see the other ones (i.e. hlsearch is not turned on automatically).

I've tried disabling all plugins (besides vim-cool and Vundle, of course) but it didn't solve it. I also tried reducing the vimrc to simply vundle+vim-cool and nothing else. Still no luck.

Any idea how I can debug this? Thanks in advance.

Turn off highlight on CursorMoved and InsertEnter without remapping (doesn't break search operator pending mode)

Thanks to guys from neovim I've found a way to automatically turn off search highlight when it's no longer needed (for me).

Notice how I check if the cursor is on something that have been searched previously. In your plugin you're using if expand("<cword>") =~ @/ which wouldn't work if someone searched for more than one word.

So in case someone is wondering this would turn off highlight if you've moved cursor with anything except next/previous match motion or if you've entered insert mode (in operator pending mode also, yey!)

noremap <expr> <Plug>(StopHL) execute('nohlsearch')[-1]
noremap! <expr> <Plug>(StopHL) execute('nohlsearch')[-1]

fu! HlSearch()
    let s:pos = match(getline('.'), @/, col('.') - 1) + 1
    if s:pos != col('.')
        call StopHL()
    endif
endfu

fu! StopHL()
    if !v:hlsearch || mode() isnot 'n'
        return
    else
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

augroup SearchHighlight
au!
    au CursorMoved * call HlSearch()
    au InsertEnter * call StopHL()
augroup end

Conflict with vim-floaterm

Using vim-floaterm in combination with vim-cool breaks vim-floaterm's :FloatermToggle by insert some characters (at least in neovim):

  • Linux
  • Mac OS X
  • Windows

Steps to reproduce (using vim plug):

  • create new init.vim like this:
call plug#begin('~/.local/plugged')
    Plug 'voldikss/vim-floaterm'
    Plug 'romainl/vim-cool',
call plug#end()
  • start neovim with this config nvim -u init.vim
  • enter command :FloatermToggle

Actual Behaviour

The floating terminal has some characters before and after the shell prompt, as well as the string (StopHL)

Expected Behaviour

The floating terminal only shows prompt without these characters and the string (StopHL)

Screenshot

1659359045

Equivalent Issue on vim-floaterm

voldikss/vim-floaterm#367

workaround to keep highlighting on after cgn, dgn

I thought about it for a while and I think I have a pretty good workaround for two issues.

First issue is to disable highlighting when using search motion in operator pending mode automatically. For example you may want to delete everything until last parenthesis in

some()
some(so|me()) # | is the cursor

You press d/)$ and remove the text, but now all parenthesis at last column are highlighted on every line.

The second one is I wanted to keep highlighting when doing replacements with *cgnnew... which is move to the next occurrence of current word, change it to new and repeat this three times effectively replacing next three matches. Previously I've been disabling highlighting in insert mode (I see that you restore it now upon returning to normal mode) and this removed the highlighting and I couldn't see where would the next replacement be, which made skipping replacements difficult.

So I've stopped disabling highlighting in insert mode and instead used YankTextPost Event to check if removed text matches search pattern (most probably mean that I've removed it with dgn) if so don't remove highlighting upon next CursorMove (which would happen right after the change). If it doesn't I'm checking if the text after the cursor matches the search pattern (means I'm probably deleted it with d/searchstring). If it is I'm disabling highlight.

let s:disable = 0

fu! HlSearch()
    if s:disable
        let s:disable = 0
        return
    endif
    silent! if !search('\%#\zs'.@/,'cnW')
        call StopHL()
    endif
endfu

fu! StopHL()
    if v:hlsearch && mode() is 'n'
        sil call feedkeys("\<Plug>(StopHL)", 'm')
    endif
endfu

fu! Temp()
    if matchstr(@", @/) == @"
        let s:disable = 1
    elseif search('\%#\zs'.@/,'cnW')
        call StopHL() 
    endif
endfu

augroup SearchHighlight
    au!
    au CursorMoved * if v:hlsearch | call HlSearch() | endif
    au TextYankPost * if v:hlsearch | call Temp() | endif
augroup END

Mismatch with vnoremap snippet

I use the below snippet to use a visual selection to search for a string, however it seems to conflict with vim-cool and ends up disabling the hlsearch on usage.

I tried appending :set hls<CR> at the end of the snippet but I am guessing there must be some sort of timeout thing that this plugin is using to set hlsearch after? However I think it also struggles with state, because I have to press n twice to see the highlighted results. Is there a way I can work around this?

" Search for selected text
vnoremap <silent> <c-s> :<C-U>
  \ let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \ gvy/<C-R><C-R>=substitute(escape(@", '/\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \ :call setreg('"', old_reg, old_regtype)<CR>

vnoremap <silent> # :<C-U>
  \ let old_reg=getreg('"')<Bar>let old_regtype=getregtype('"')<CR>
  \ gvy?<C-R><C-R>=substitute(
  \ escape(@", '?\.*$^~['), '\_s\+', '\\_s\\+', 'g')<CR><CR>
  \ gV:call setreg('"', old_reg, old_regtype)<CR>

Bug: highlighting disappears with */#/gd and smartcase

How to reproduce:

  • enable 'ignorecase' and 'smartcase'
  • open a document with upper and lowercase versions of the same word (e.g. "Plug" and "plug")
  • search /<\Plug\> and see how only uppercase Plugs are highlighted
  • now try to */# search Plug, until you reach a lowercase "plug" and see how the highlighting incorrectly disappears even though "plug" is a valid target

This happens due to the strange quirk with */#/gd where 'smartcase' doesn't apply to them. Plus search() always uses smartcase, so there's the inconsistency.

Perhaps I'm missing a bug or flaw with this fix, but you can simply change !search('\%#\zs'.@/,'cnW') to:

!search((&ignorecase ? '\c' : '').'\%#\zs'.@/,'cnW')

This causes search() to ignore 'smartcase' during "star searches".

Extra input when going from normal/hlsearch to insert mode

I love this plugin, but I'm having issues with it dumping a bit of text <Plug>(StopHL) every time I use c, a, or i to enter insert mode from a highlighted search state - it also doesn't seem to run StopHL, which I'm assuming would cut the highlights. Full reproduction:

vim myfile
/a # Search for anything, letter a works
(n*|N*) # Look around a bit, find the a to edit
...from here, any of the following:
ce # Replace word inserts the above Plug text
i # Drops into insert mode and inserts Plug
a # Same as i really

Just wanted to report it in case it hasn't been noticed and if you're still accepting bug reports.

Technical bits
Using cool.vim from master branch of repo as of yesterdayish
OS: Ubuntu 16.04
vim --version header:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48)
Included patches: 1-1689
Extra patches: 8.0.0056

Toggling on/off

Sometimes, I want just want to <C-d> through a document without losing the highlights.

It would be useful to implement a command or option for toggling vim-cool on/off.

:VimCoolToggle

Error for custom Esc from search commandline

I want to disable hlsearch when leaving the search commandline when pressing <Esc>.
Therefore I have following search related setup in my vimrc:

if has('patch-8.0.1206') || has('nvim-0.2.3')
  augroup Hlsearch
    autocmd!
    autocmd CmdlineEnter /,\? :set hlsearch
  augroup END
endif
cnoremap <silent><expr> <Esc> getcmdtype() == "/" \|\| getcmdtype() == "?" ? "<Esc>:set nohlsearch<CR>" : "<Esc>"

It works, but vim-cool throws an error:

Error detected while processing function <SNR>47_PlayItCool:
line   16:
E31: No such mapping
line   17:
E31: No such mapping  

Do you have a clue what is going wrong?

Highlighting toggles on when sourcing vimrc containing `set hlsearch`

When I have set hlsearch set in my vimrc file and I source it, vim-cool highlights the last search. From my quick look, it seems to be triggered by the OptionSet autocmd. If I commented out set hlsearch in my vimrc and source it, no highlighting appears. Is there a way to have set hlsearch in your vimrc without having vim-cool re-highlight last search when I source the file?

cursor forgets which column it was in

if you are just scrolling with j/k and you go on a empty line, after moving onto a line with text again the cursor won't return to the column it was on previously

Problem with <C-o> in insert mode

If I would press <C-o> in insert mode and then press search related key like n vim would insert some of your mappings into buffer which of course isn't what I wanted and not what vim should do.
I don't know if it's possible or not in vim, but you should do this actions that happens after pressing n atomical, meaning that they should be the whole command.

Conflict with vim-doge

Hi,

I just received this issue. Do you have any possible solutions from your side? I think this fix should be fixed within your plugin.

Problem starting vim-cool

I have installed vim-cool using Vundle by adding the line:

Plugin 'romainl/vim-cool'

to my .vimrc and running from command line:

$ vim +PluginInstall +qall

My problem is that when I start Vim, I get:

$ vim .vimrc 
Error detected while processing /home/essett/.vim/bundle/vim-cool/plugin/cool.vim:
line   19:
E216: No such group or event: OptionSet hlsearch call <SID>PlayItCool(v:option_old, v:option_new)
line   24:
E216: No such event: OptionSet highlight let s:saveh = &highlight
Press ENTER or type command to continue

My vim version is:

$ vim --version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:42:57)
Included patches: 1-52
Extra patches: 8.0.0056
Modified by [email protected]
Compiled by buildd@
Huge version with GTK2-GNOME GUI.  Features included (+) or not (-):
+acl             +farsi           +mouse_netterm   +syntax
+arabic          +file_in_path    +mouse_sgr       +tag_binary
+autocmd         +find_in_path    -mouse_sysmouse  +tag_old_static
+balloon_eval    +float           +mouse_urxvt     -tag_any_white
+browse          +folding         +mouse_xterm     +tcl
++builtin_terms  -footer          +multi_byte      +terminfo
+byte_offset     +fork()          +multi_lang      +termresponse
+cindent         +gettext         -mzscheme        +textobjects
+clientserver    -hangul_input    +netbeans_intg   +title
+clipboard       +iconv           +path_extra      +toolbar
+cmdline_compl   +insert_expand   +perl            +user_commands
+cmdline_hist    +jumplist        +persistent_undo +vertsplit
+cmdline_info    +keymap          +postscript      +virtualedit
+comments        +langmap         +printer         +visual
+conceal         +libcall         +profile         +visualextra
+cryptv          +linebreak       +python          +viminfo
+cscope          +lispindent      -python3         +vreplace
+cursorbind      +listcmds        +quickfix        +wildignore
+cursorshape     +localmap        +reltime         +wildmenu
+dialog_con_gui  +lua             +rightleft       +windows
+diff            +menu            +ruby            +writebackup
+digraphs        +mksession       +scrollbind      +X11
+dnd             +modify_fname    +signs           -xfontset
-ebcdic          +mouse           +smartindent     +xim
+emacs_tags      +mouseshape      -sniff           +xsmp_interact
+eval            +mouse_dec       +startuptime     +xterm_clipboard
+ex_extra        +mouse_gpm       +statusline      -xterm_save
+extra_search    -mouse_jsbterm   -sun_workshop    +xpm
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_GTK  -pthread -I/usr/include/gtk-2.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/pango-1.0 -I/usr/include/gio-unix-2.0/ -I/usr/include/freetype2 -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/libpng12 -I/usr/include/harfbuzz   -pthread -DORBIT2=1 -D_REENTRANT -I/usr/include/libgnomeui-2.0 -I/usr/include/libart-2.0 -I/usr/include/gconf/2 -I/usr/include/gnome-keyring-1 -I/usr/include/libgnome-2.0 -I/usr/include/libbonoboui-2.0 -I/usr/include/libgnomecanvas-2.0 -I/usr/include/gtk-2.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/gnome-vfs-2.0 -I/usr/lib/x86_64-linux-gnu/gnome-vfs-2.0/include -I/usr/include/dbus-1.0 -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/orbit-2.0 -I/usr/include/libbonobo-2.0 -I/usr/include/bonobo-activation-2.0 -I/usr/include/libxml2 -I/usr/include/pango-1.0 -I/usr/include/gail-1.0 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/atk-1.0 -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include -I/usr/include/cairo -I/usr/include/gio-unix-2.0/ -I/usr/include/pixman-1 -I/usr/include/libpng12     -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1     -I/usr/include/tcl8.6  -D_REENTRANT=1  -D_THREAD_SAFE=1  -D_LARGEFILE64_SOURCE=1  
Linking: gcc   -L. -Wl,-Bsymbolic-functions -Wl,-z,relro -L/build/buildd/ruby1.9.1-1.9.3.484/debian/lib -rdynamic -Wl,-export-dynamic -Wl,-E  -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,--as-needed -o vim   -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lglib-2.0 -lfreetype     -lgnomeui-2 -lSM -lICE -lbonoboui-2 -lgnomevfs-2 -lgnomecanvas-2 -lgnome-2 -lpopt -lbonobo-2 -lbonobo-activation -lORBit-2 -lart_lgpl_2 -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lfreetype -lgconf-2 -lgthread-2.0 -lgmodule-2.0 -lgobject-2.0 -lglib-2.0   -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE  -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl  -L/usr/lib -llua5.2 -Wl,-E  -fstack-protector -L/usr/local/lib  -L/usr/lib/perl/5.18/CORE -lperl -ldl -lm -lpthread -lcrypt -L/usr/lib/python2.7/config-x86_64-linux-gnu -lpython2.7 -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions  -L/usr/lib/x86_64-linux-gnu -ltcl8.6 -ldl -lz -lpthread -lieee -lm -lruby-1.9.1 -lpthread -lrt -ldl -lcrypt -lm  -L/usr/lib   

Anyone have an idea what is going on?

setpos in CursorMove breaks visual-block append

Hello,

I noticed that I always get padded append when doing a visual-block append (:he v_b_A) and managed to track it down to the setpos call in the Cooler() function in this plugin.

I'm guessing that calling setpos is seen as movement which forces the appended text to be padded to the longest line. This may be a bug in Vim.

One workaround is to check if the cursor has actually moved before calling setpos. e.g.

let cur_cursor = exists("*getcurpos") ? getcurpos() : getpos(".")
if cur_cursor != save_cursor
    call setpos('.', save_cursor)
endif

But I'm not sure if you actually need to save and restore the cursor position. I don't see anything in that function that might move the cursor. So perhaps it could just be removed. I have removed the save/restore of cursor position in my local copy and it seems to be working fine.

Searching a magic * followed by some text incorrectly clears highlighting

Kind of a weird edge case, but this occurs when I search * (or \v* or \m*) by itself, or followed by any text.

I think the source of this bug can be found in :help /star, which says:

... Exception: When "*" is used at the start of the pattern or just after "^" it matches the star character. ...

Odd interaction with fzf

Here is a minimal init.vim file that installs fzf.vim and vim-cool - I could not reproduce in my experimentation with creating a floating window manually.

cal plug#begin(stdpath('data') . '/plugged')
Plug 'junegunn/fzf.vim'
Plug 'romainl/vim-cool'
let g:fzf_layout = { 'window': { 'height': 0.9, 'width': 0.9 } }
cal plug#end()

To trigger the bug,

  • Create a file and highlight some word by searching for it
  • While the highlight is still visible, run some fzf plugin command that will open a floating window, such as :Files

The floating window flickers, then closes.

Check if highlight is enabled?

Hey!
How could I check if a search is in progress, e.g. echo g:is_searching which would return 1 when colorization is on and 0 otherwise.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.