The quickfix buffer (opened with :cope after a :make for instance) is included in the :bnext :bprevious navigation which I use very frequently (having mapped it to the arrow keys). I never want to navigate to the quickfix buffer that way however. Is there a good way to exclude it?
Here is a simple solution:
augroup qf
autocmd!
autocmd FileType qf set nobuflisted
augroup END
See :help 'buflisted'.
Related
I want to create a plugin which maps a certain localleader mapping to a function call.
I have a ftplugin/javascript.vim file with the following content:
augroup javascript_pluginName
au!
echom "The plugin is loaded for JS"
au FileType javascript nnoremap <buffer> <localleader>j :call pluginName#pluginName#funName()<cr>
augroup END
When I open a javascript file, the message is printed, but then checking the mapped combinations shows that there is no such mapping.
I understand, that nnoremap is simply not going to map on top of other mappings. However, if I only leave the nnoremap command, without putting it in an autocmd, it would map properly.
Simply executing the call in command mode also works fine.
I looked into the vim help to see how to use autocmd, and I can't see any difference between the way I use it and what is explained there. The "Learn Vim the Hard Way" book also didn't help.
Is there something I'm missing? Should I frame the autocmd somehow differently?
I finally found out what the problem is!
Apparently, according to this article: https://vimways.org/2018/from-vimrc-to-vim/ if you put a filetype specific code into a filetype plugin, there is no need to create an autocmd for it.
The boilerplate is all made redundant by the general behaviour of vim setting the filetype on open and then running the ftplugin scripts, which are relevant for the file.
This means that in my ftplugin/javascript.vim file I only need the mapping:
nnoremap <buffer> <localleader>j :call pluginName#pluginName#funName()<cr>
Currently, I'm trying to clean the quickfix window while using cscope and to do that I'm using these commands:
augroup quickfix
autocmd!
autocmd BufRead qf set modifiable
autocmd FileType qf
\ set modifiable |
\ set hidden |
\ execute ":%s/<<.*>>//g"
augroup END
This way whenever a file is opened the <<>> tags are removed. However, since by default the quickfix window is nomodifiable I'm having to force it.
During testing I found that the replacement is executed but the quickfix window is still nomodifiable afterward; therefore I cannot edit its content or use it as notes. Modifiable can be set by hand once the window is opened, but this is not convenient. Maybe I`m missing something or using the wrong event. Can someone explain what is wrong?
It's not urgent to deal with autogroup before you are familiar to them enough.
Just try or do it in your .vimrc with a line like below:
au BufRead errors.err set ma hidden | execute ":%s/<<.*>>//g"
then the quickfix file (usually named as 'errors.err') should be modifiable.
(If you use a different name, substitute it for the 'errors.err', or use a pattern like '*.err'.)
See :h au for further info.
I suspect there is something inherently inefficient about your process of using the quickfix list, but if you wanted to abuse it this way and make it modifiable the following works for me:
au! FileType qf set ma hidden | execute ":%s/<<.*>>//g"
au! BufReadPost quickfix set ma
See :h CTRL-W-<Enter> for some further information.
The vim plugin vdebug opens several splits when it starts the debugger. One of those is DebuggerWatch at the top right. I read somewhere I can set an autocmd to listen for that buffer opening and take action. In this case I want to run :resize 100 to maximize the height. I know how to set an autocmd by filetype or by file glob, but not by a buffer name that is not a file. How do I write an autocmd for this?
I was able to resize on entering the buffer like this:
augroup vdebugwatchpanellarger
autocmd!
autocmd BufEnter DebuggerWatch resize 999
autocmd BufEnter DebuggerStack resize 999
augroup END
This is helpful because at least if I navigate to one of these buffers it will resize them. But I haven't been able to get it resizing on loading the buffer yet. I tried BufRead, BufNew, BufAdd instead of BufEnter. No effect.
The plugin sets the :help 'syntax' option of the "DebuggerWatch" window to debugger_watch so using the :help Syntax event should work:
augroup vdebugwatchpanellarger
autocmd!
autocmd Syntax debugger_watch resize 999
augroup END
Is it possible to open files that are indented with 2 spaces, but show me 4 space indentation, and when I make 4 spaces, it saves in a 2 space format?
Edit
It turns out I also need to be able to ensure that it works if the file has a mix of tabs, 2 spaces, and 4 spaces.
Edit 2
So, here is my current solution. I'm having to remap my (originally mapped to :w) so that I can place my cursor back where it was (and give me one "history back" as far as cursor positions when I do a save. Is there a way to do this without affecting cursor position (and not adding the substitution to the history, either)?
function! s:ShimSpaces()
nunmap <C-S>
nmap <C-S> ms``mt:w<Cr>`t`s
augroup SeoTabs
autocmd!
autocmd BufReadPost,BufWritePost * set tabstop=4
autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e
autocmd BufReadPost * %substitute/ \+$//e
autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e
autocmd BufWritePre * set tabstop=2
autocmd BufWritePre * retab
augroup END
endfunction
command! -n=0 -bar ShimSpaces :call s:ShimSpaces()
This is the opposite of what was asked here.
The help has an example for a similar use case of different tab widths, see :help retab-example.
Adapting that to doubling / halving spaces:
:augroup AdaptIndent
:autocmd!
:autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e
:autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e
:augroup END
With *, this will affect all opened files. To restrict this to certain files, see :help autocmd-patterns.
Edit: With the :augroup wrapping, this can be turned off again via :autocmd! AdaptIndent. This way, you can easily toggle this on / off. For ease of use, I'd put this in a function and define a custom command calling it.
I edit the same files often, so I have assigned some global marks to make it easier to load them up. For example, I jump to my resume (a TeX file) by typing 'R in normal mode.
I have also set up autocommands for TeX files, triggered by the BufWinEnter event. If I type :e /path/to/resume.tex in normal mode, everything works great. Yay!
However, the BufWinEnter event does not seem to be triggered when I jump to the file using the global mark 'R. Is there some other event I should use instead? What is the right way to set this up?
Here is the relevant line of ~/.vim/ftplugin/tex.vim
au BufWinEnter <buffer> silent loadview
Consider using :au Syntax tex silent loadview in .vimrc, instead.