Argument required error when using Ag with vim - vim

I'd like to just use the :Ag command to enter the interactive ag shell (as with :Files) and then start typing my search term.
However, vim won't allow me to open the ag interactive interface. If I type :Ag, it will complain with the following error:
E471: Argument required
If I write the search term (like :Ag something) it opens a new pane with the results. But that's not what I want: I want to use ag the same way as the :Files command.
More info
I'm using thoughtbot/dotfiles.
Definition of :Ag command
:verbose command Ag
Name Args Address Complete Definition
| Ag + file silent! grep! <args>|cwindow|redraw!
Settings
This is the portion of my .vimrc file where ag is enabled.
" Use The Silver Searcher https://github.com/ggreer/the_silver_searcher
if executable('ag')
" Use Ag over Grep
set grepprg=ag\ --nogroup\ --nocolor
" Use ag in fzf for listing files. Lightning fast and respects .gitignore
let $FZF_DEFAULT_COMMAND = 'ag --literal --files-with-matches --nocolor --hidden -g ""'
if !exists(":Ag")
command -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!
nnoremap \ :Ag<SPACE>
endif
endif

This portion of the .vimrc (by thoughtbot/dotfiles) was overriding the default behaviour set by fzf.vim:
if !exists(":Ag")
command -nargs=+ -complete=file -bar Ag silent! grep! <args>|cwindow|redraw!
nnoremap \ :Ag<SPACE>
endif
I just commented it out and things are now working as I wanted.
Thanks to #romainl for giving some useful hints.

Related

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

Vim delete a command

How do I delete a command that I created using the :command command?
I mis-typed:
:command Bigfon guifont=Monospace\ 12
So now I have a Bigfon command that doesn't work correctly; I'd like to just have a Bigfont command that does (:command Bigfont set guifont=Monospace\ 12)
Searches that involve both "Vim" and "delete" seem to only return pages that describe the various commands to delete text.
delc Bigfon or delcommand Bigfon.
Documentation :h delc:
:delc[ommand] {cmd} *:delc* *:delcommand* *E184*
Delete the user-defined command {cmd}.
Although there is a much better way to change font size quickly ( from tpope's .vimrc ):
" Quickly change gui font size with + and - in normal mode
command! -bar -nargs=0 Bigger :let &guifont = substitute(&guifont,'\d\+$','\=submatch(0)+1','')
command! -bar -nargs=0 Smaller :let &guifont = substitute(&guifont,'\d\+$','\=submatch(0)-1','')
nnoremap - :Smaller<CR>
nnoremap + :Bigger<CR>

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

:edit in function does not opens path given in argument

I have in .vimrc:
function! s:Edit(path)
vsplit a:path
endfunction
command! -nargs=1 -complete=file E call s:Edit(<q-args>)
Problem is that in new window i have file named "a:path" not file passed as argument to :E command.
Although my function starts to work when i do:
exec "vsplit".a:path
Why?
Can it be done better?
Build up your command and execute it with :execute. e.g.
function! s:Edit(path)
execute 'vsplit ' . a:path
endfunction
command! -nargs=1 -complete=file E call s:Edit(<q-args>)
For more help see :h :exe
Is there a better way?
Really depends on your goals. I would need more information about what this is supposed to do. However it seem like you want to create an alias for :vsplit. I would suggest you using cmdalias.vim or a the very least the following:
command! -nargs=? -complete=file -bang E vsplit<bang> <args>
If all you are doing is trying to optimize key strokes then the following mapping would also be sufficient:
nnoremap <leader>v :vsp<space>
Personally I would just get used to using :vsplit and stop worrying.

How to -enable- vim's "Press ENTER or type command to continue"

There are lots of questions on here about how to disable the "Press ENTER or type command to continue" text when running an external command from vim. But I want to know how to enable it. As you can see below, I'm not using silent or any other means to disable it.
Here is the relevant portion of my .vimrc:
nmap <leader>r :call NoFrills()<CR> "(r)eveal hidden chars
nmap <leader>i :set paste!<CR> "(i)ndenting on/off
nmap <leader>h :set nohlsearch!<CR> "(h)ighlighting of search terms on/off
nmap <leader>w :call SudoWrite()<CR> "(w)rite file, sudoing first
nmap <leader>a :! sudo service httpd restart<CR>"(a)pache restart
nmap <leader>p :! perl -c %<CR> "(p)erl -c
When I execute a command that should have output, like with the key sequence \ p, the command executes but any output is quickly dismissed from the screen without pausing or prompting me to continue. Why is that?
If I execute :! perl -c % from vim's command mode, it shows me the output and prompts me to continue, as expected.
:version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Feb 17 2012 10:23:31)
:set
--- Options ---
background=dark filetype=perl incsearch scrolloff=3 ttyfast
comments=:# helplang=en list shiftwidth=4 ttymouse=xterm
commentstring=#%s history=50 number syntax=perl viminfo='20,"50
define=[^A-Za-z_] hlsearch ruler tabstop=4 wildmenu
backspace=indent,eol,start
complete=.,w,b,u,t,i,k
fileencoding=utf-8
fileencodings=ucs-bom,utf-8,latin1
formatoptions=tcrq
guicursor=n-v-c:block,o:hor50,i-ci:hor15,r-cr:hor30,sm:block,a:blinkon0
include=\<\(use\|require\)\>
includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.pm','')
indentexpr=GetPerlIndent()
indentkeys=0{,0},:,0#,!^F,o,O,e,0=,0),0=or,0=and
isfname=#,48-57,/,.,-,_,+,,,#,$,%,~,=,:
keywordprg=perldoc -f
listchars=tab:>-,trail:o
path=/usr/local/lib64/perl5,/usr/local/share/perl5,/usr/lib64/perl5/vendor_perl,/usr/share/perl5/vendor_perl,/usr/lib64/perl
5,/usr/share/perl5,,
wildmode=longest,list,full
As #romainl has already indicated in the comments, the appended comments are part of the mappings, and one of those characters (e.g. the <Space>) dismisses the hit-enter prompt. You could include a trailing comment by separating with | (which must be escaped or written as <Bar>) inside a mapping):
nnoremap <leader>p :! perl -c %<CR>| "(p)erl -c
But I'd recommend to put the comments on a separate line:
" (p)erl -c
nnoremap <leader>p :! perl -c %<CR>
PS: You should use :noremap; it makes the mapping immune to remapping and recursion.

Resources