How to show lines matching search in open folds in vim? - search

How can I show all lines in only open folds that match "foo"?
I tried this per "search only in folded text":
:folddoopen g/foo
but got this error: E147: Cannot do :global recursive
vim version: 7.3 (2010 Aug 15)

:folddoopen and :g cannot be used together in a meaningful way.
Instead, use :g with foldclosed():
:g/foo/if foldclosed('.') == -1 | # | endif
For more help see:
:help :g
:help /
:help :if
:help foldclosed()
:help :#
:help :foldopen

Related

How to Override/Redefine Vim Search Command

I need to override/redefine Vim's search operator "/" to also execute "zszH" after the search to center the search results on the screen horizontally.
For example, I want to enter: /varchar and have the search results (i.e., the string "varchar") displayed in the middle of the scren horizontally.
I can do that now by manually entering "zszH" after each search, but that is very tedious.
You can use the CmdlineLeave event. Add the following to your vimrc
augroup RecenterSearch
autocmd!
autocmd CmdlineLeave [/?] call feedkeys('zszH', 't')
augroup END
Note: CmdlineLeave requires Vim 8.1
Or you can map <cr>:
cnoremap <expr> <cr> "\<cr>" . (getcmdtype() =~ '[?/]' ? "zszH" : '')
Some mappings which might be helpful:
nnoremap n nzszH
nnoremap N NzszH
If you do not have a new enough version on Vim then maybe look into 'wrap' or create a mapping
For more help see:
:h CmdlineLeave
:h :autocmd
:h feedkeys()
:h expression-mapping
:h getcmdtype()

How to autoupdate vimgrep search result in quickfix window?

How to autoupdate search vimgrep results from quickfix window after file is autoudpated due to change?
I use :vim/pattern/% to search and put result in quickfix window.
You can always do <up> on the command line with a prefix of :vimgrep to make searching history easier. You may also want to check out q: and the use ?// to search for the command to re-execute.
Assuming the quickfix title is set correctly, you can use following the command:
:execute get(getqflist({'title':1}), 'title')
This however I am not certain this will work with all :grep/:make commands. You also need a newer Vim version to get the Quckfix list title (Vim 8+ I think).
If you find yourself doing this often you may want to bind a mapping or command.
command! -nargs=0 -bar QFRefresh execute get(getqflist({'title':1}), 'title')
Now how to add do this automatically? We can use FileChangedShellPost autocmd to run our QFRefresh command once a file change has been detected. Add the following to you vimrc file:
augroup QFRefresh
autocmd!
autocmd FileChangedShellPost * if get(b:, 'qfrefresh_auto', 0) | QFRefresh | endif
augroup END
command! -nargs=0 -bar QFAutoRefreshToggle let b:qfrefresh_auto = !get(b:, 'qfrefresh_auto', 0) | echo b:qfrefresh_auto ? 'Auto Refresh' : 'No Auto Refresh'
Now you can use :QFAutoRefreshToggle to toggle refreshing a file.
Note: As stated before QFRefresh uses the quickfix's title to get the quickfix command. If the title is not set correctly a refresh may not work correctly. Also I am not sure what guarantees Vim has on triggering FileChangedShellPost. You can force a check via :checktime command.
For more help see:
:h getqflist()
:h :execute
:h :get
:h q:
:h cmdwin
:h c_Up
:h FileChangedShellPost
:h :checktime

Is there a way to do reverse search of vim mappings?

Is it possible to search mappings by the target command?
E.g: I would like to search which are the key sequences mapped to StripWhitespace.
I know you can search what is mapped to a key sequence using map {lhs} command. E.g:
map ,s
will show mappings to key sequences starting with ,s:
x ,s :StripWhitespace<CR>
n ,sb * :SortBlock<CR>
What I want to do is do the reverse of that.
You can use :filter sort of like using grep in the shell.
:filter /StripWhitespace/ map
:filter is new in Vim 8 so you have to have a relatively up-to-date Vim. You could also in theory use execute() however that is relatively new as well.
Older Vim version you will need to use :redir
:redir #"> | silent map | redir END | new | put | v/StripWhitespace/d_
For more help see:
:h :filter
:h execute()
:h :redir
:h :silent
:h :map
:h :new
:h :put
:h :v
:h :d
:h "_

Tab completion for my vim command don't work

I have created this vim command:
command! -complete=file E vsplit | wincmd l | e
When I use it by this
:E <Tab>
It works as expected. But when i want to complete path relatively to home directory, it shows ^I for each TAB hit:
:E ~/<TAB>
# shows as:
:E ~/^I
For :e command tab-completion works good.
You need to specify how many arguments:
command! -complete=file -nargs=? E vsplit | wincmd l | e <args>
See :h :command-nargs
However you are re-creating a command that already exists :vsplit and using 'splitright' setting. Put set splitright in your vimrc and now you can use :vsplit directly.
:set splitright
:vsp foo.txt
If you do not feel like setting 'splitright' then you can use :rightbelow. Example:
:rightb vsp foo.txt
For more help see:
:h 'splitright'
:h :vsp
:h :command-nargs
:h :rightbelow

Is there a way to search inside the digraphs in Vim

Is there a way to search inside the :digraph table in VIM ? the \is not working and my eyes are getting apart searching the glyphs I desire..
try :h digraph-table there you can do search with / or ?
You can capture the actual :digraph output in a scratch buffer via:
:redir #a | silent digraph | redir END | new +setl\ buftype=nofile\ bufhidden=wipe | put! a
Besides the answers given, you can try my unicode-plugin
You can then use the :Digraphs foobar to search for digraphs whose name match foobar
Combining Ingo’s accepted answer, Bill Odom’s answer on directing ex commands to a buffer, and Capturing ex command output from the Vim Tips Wiki produces the following snippet for your .vimrc.
function! TabDigraphs()
redir => message
silent digraphs
redir END
tabnew
setlocal buftype=nofile bufhidden=wipe
silent put =message
setlocal nomodified
endfunction
command! TabDigraphs call TabDigraphs()

Resources