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
Related
Since I've started using Vim tabs (I'm a bit late to the game since they've been there since 2009!), I find I'd like most thing to default to tabs, so I always open files with vim -p file1 file2 etc. I would like :help / :h to always open in tabs also. I can do :tab help ctrl-w or :tab h ctrl-w but how can I use .vimrc to always default :h or :help to open in a new tab?
Additionally, is there a way to take an open file and push it into a tab, or to tell vim to take all open buffers and dynamically shift them all into tabs?
"take an open file and push it into a tab"
You can do that with this normal mode command:
<C-w>T
or this equivalent Ex command:
:wincmd T
See :help ctrl-w_t and :help :wincmd.
":h to always open in tabs"
You only need to put the aforementioned Ex command in after/ftplugin/help.vim:
wincmd T
You can use autocmd to always open help in a new tab
" This won't work if you open the same help file more than once
autocmd FileType help wincmd T
" This works
autocmd BufEnter *.txt if &filetype == 'help' | wincmd T | endif
Or you can define a new command to only open it in a new tab, when in a small screen or there are many to read, for example.
With the following, you can use :Tabhelp ....
function! Tabhelp(arg)
execute 'tab help '.a:arg
endfunction
command -nargs=? -complete=help Tabhelp call Tabhelp(<q-args>)
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.
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
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>
Like said here I can open e.g. a second file in vim using
:vsplit file2.dat
and use
:set scrollbind
to synchronize the scrolling.
Is there a possibility that this is done automatically when I view files with vsplit?
How about a custom command?!
:command! -nargs=* -complete=file VBoundSplit setlocal scrollbind | vsplit <args> | setlocal scrollbind