I would like to create a vim command that is waiting for an input THEN that will execute ':cw' automatically after the first command.
Here is what I try:
noremap <C-p> :exec ":ProjectGrep /".input('Search: ')"/ src/**"<CR>:cw
But the ':cw' does not execute after the command, it complete the input().
Add the following snippet to your vimrc to make Vim open the quickfix/location window when there are valid errors/locations:
augroup qf
autocmd!
autocmd QuickFixCmdPost [^l]* cwindow
autocmd QuickFixCmdPost l* lwindow
augroup END
That snippet addresses what I believe is your underlying issue (having the quickfix window open automatically after your search command), though, not your actual question.
Anyway, cwindow should be the last command in the function called by your :ProjectGrep command; not at the mapping level.
Related
I have the following in my .vimrc to automatically open/close the QuickFix window after running :make :
augroup quickfix
autocmd!
autocmd QuickFixCmdPost [^l]* cwindow
autocmd QuickFixCmdPost l* lwindow
augroup END
It works fine, but when the autocmd opens the QuickFix window, it does not put the focus on the window. Is there any way to automatically put the focus on the QuickFix window after the autocmd opens it?
From :help :make:
7. If [!] is not given the first error is jumped to.
So…
:make jumps to the first error, with or without :cwindow,
:make! doesn't jump to the first error so the cursor stays where it is.
NOTE: the same applies to :grep.
I did not have tested the code below yet, but if you can:
augroup quickfix
autocmd!
autocmd QuickFixCmdPost [^l]* cwindow | wincmd j
autocmd QuickFixCmdPost l* lwindow | wincmd j
augroup END
For more read :h wincmd.
I am trying to execute a yapf command to format my python file when I save it, so I created a function to call this command:
function Format_python_file()
silent :!yapf --style="{based_on_style: pep8, indent_width: 4}" -i %
silent :e %
endfunction
autocmd BufWritePost *.py call Format_python_file() <afile>
The problem is with your autocmd line, you have a trailing <afile> there.
In fact, the message I see is quite explicit about that:
Error detected while processing BufWritePost Autocommands for "*.py":
E488: Trailing characters: <afile>
You should just drop the <afile>, the function itself already works on the current buffer, doesn't need any argument or other reference to the current file.
Also note that it's a good practice to put your autocmds inside an augroup which gets cleared first. That way, if you reload your source file (vimrc or otherwise), it won't create duplicated autocmds.
The cleaner way to set up this autocmd would be:
augroup python_yapf
autocmd!
autocmd BufWritePost *.py call Format_python_file()
augroup END
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
I'm using vim 7.3 and Rainbow Parentheses plugin. When opening multiple tabs with vim -p file1 file2 or with vim -S session.vim, or even with tabnew file or any other method, my parenthesis are colored in only one file.
I just put this into my .vimrc : au VimEnter * RainbowParenthesesToggle
as said here. I tried to use :RainbowParenthesesToggle on the other tabs once opened but it only toggles in the parenthesis-activated tab.
What should I do to make things work in all tabs ?
I made it work by adding the same instructions as here in my .vimrc, thanks to FDinoff. I replaced the last instruction to make it work using tab, as I intended first.
function! Config_Rainbow()
call rainbow_parentheses#load(0)
call rainbow_parentheses#load(1)
call rainbow_parentheses#load(2)
endfunction
function! Load_Rainbow()
call rainbow_parentheses#activate()
endfunction
augroup TastetheRainbow
autocmd!
autocmd Syntax * call Config_Rainbow()
autocmd VimEnter,BufRead,BufWinEnter,BufNewFile * call Load_Rainbow()
augroup END
The VimEnter flag on the autocommand tells vim to perform the command specified (in this case RainbowParenthesesToggle only when starting the editor, which is in your case when you open the first file.
If you want to extend the functionality to everytime you load a buffer you should do something like:
autocmd BufRead,BufNewFile * RainbowParenthesesToggle
I've setup my vim editor (I use MacVim) to save files automatically when the focus is lost:
autocmd FocusLost * silent! wall
I also automatically strip trailing whitespace from python files using this auto command:
autocmd BufWritePre *.py :%s/\s\+$//e
This auto command works perfectly when I save the file manually (either by typing :w or by pressing ⌘s) but it is not executed (i.e. the whitespace is not stripped) when I switch to another application and the buffer is automatically written.
How can I modify these auto commands to make them work together?
You need to change your FocusLost autocommand to:
autocmd FocusLost * nested silent! wall
See :h autocmd-nested for details.
I can't test this in a graphic Vim, but you can try some options:
Join some events in the same autocommand autocmd BufWritePre,FocusLost *.py ...
Execute an autocommand from an event, something like:
autocmd BufWritePre *.py :execute "%s/\s\+$//e" | doautocmd FocusLost %