func! s:get_visual_selection_list() abort let [lnum1, col1] = getpos("'<")[1:2] let [lnum2, col2] = getpos("'>")[1:2] let lines = getline(lnum1, lnum2) let lines[-1] = lines[-1][: col2 - (&selection ==? 'inclusive' ? 1 : 2)] let lines[0] = lines[0][col1 - 1:] return lines endf func! s:get_visual_selection_searchpattern() abort let lines = s:get_visual_selection_list() let lines = map(lines, 'escape(v:val, ''/\'')') " Join with a _literal_ \n to make a valid search pattern. return join(lines, '\n') endf " Read last visual-selection into command line cnoremap =join(get_visual_selection_list(), " ") inoremap =join(get_visual_selection_list(), " ") xnoremap * ms/\V=get_visual_selection_searchpattern() nnoremap * ms:let @/='\V\<'.escape(expand(''), '/\').'\>'call histadd('/',@/) nnoremap g* ms:let @/='\V' . escape(expand(''), '/\') call histadd('/',@/) " Use the setting of mark trick for backward search as well. nnoremap # ms# nnoremap g# msg#