Automate synchronized scrolling - vim

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

Related

Default Vim to always open help in a new tab

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>)

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

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

Vim folding get expanded with new marker

If I have a block of css or js that is folded in vim, when ever add a new block above it, the block gets unfolded.
Currently, I just have this in my vimrc
autocmd FileType css,scss setlocal foldmethod=marker
autocmd FileType css,scss setlocal foldmarker={,}
Is there a way to disable this?
Will :h fold and check, but wanted to ask here as well.
Add this to your vimrc, straight from the vim wiki: http://vim.wikia.com/wiki/Keep_folds_closed_while_inserting_text
" Don't screw up folds when inserting text that might affect them, until
" leaving insert mode. Foldmethod is local to the window. Protect against
" screwing up folding when switching between windows.
autocmd InsertEnter * if !exists('w:last_fdm') | let w:last_fdm=&foldmethod | setlocal foldmethod=manual | endif
autocmd InsertLeave,WinLeave * if exists('w:last_fdm') | let &l:foldmethod=w:last_fdm | unlet w:last_fdm | endif

How can I set certain variables depending on the permissions of an opened file?

I use Vim to open a bibtex file, but I export another bib-file and replace the original one often.
I'd like to reload the Vim buffer automatically to view the new bibtex file.
As far as I know, :view file-name will open a file with read-only mode.
And set autoload makes it load automatically.
How can I set autoload only when a file is opened with read-only permission?
Do I need to use certain au Buffer.. variables?
I would write a custom :ViewAutoRead command:
:command! -nargs=1 -complete=file ViewAutoRead view <args> | setlocal autoread
You can also combine the setting with the command:
:view +setl\ autoread filename
An :autocmd would be a general solution for any readonly file:
:autocmd BufRead * if &readonly | setlocal autoread | endif

Resources