Position a Quickfix window to the bottom down globally - vim

I setup my vim with amix/vimrc. The Quickfix window is below NERDTree, as shown below.
How do I move it to the bottom down? I followed this thread, and added the following codes to my_configs.vim,
augroup DragQuickfixWindowDown
autocmd!
autocmd FileType qf wincmd J
augroup end
However, the Quickfix window is still on the bottom right.
My desired behaviour like this,
Open Quickfix window automatically after running :make
The Quickfix window is on the bottom with full width
My related settings are,
"a global quickfix window on the bottom down
augroup DragQuickfixWindowDown
autocmd!
autocmd FileType qf wincmd J
augroup end
" Open Quickfix window automatically after running :make
augroup OpenQuickfixWindowAfterMake
autocmd QuickFixCmdPost [^1]* nested cwindow
autocmd QuickFixCmdPost 1* nested lwindow
augroup END
"compile and run
" Quick compile and run kinds of files via ,p
nmap <F10> :call <SID>compile_and_run()<CR>
function! s:compile_and_run()
exec 'w'
exec 'vertical rightbelow copen 80'
exec 'wincmd w'
if &filetype ==# 'c'
exec 'AsyncRun! gcc % -o %<; time ./%<'
elseif &filetype ==# 'cpp'
exec 'AsyncRun! g++ -std=c++11 % -o %<; time ./%<'
elseif &filetype ==# 'rust'
exec 'AsyncRun! rustc %; time ./%<'
elseif &filetype ==# 'java'
exec 'AsyncRun! javac %; time java %<; rm -f *.class'
elseif &filetype ==# 'sh'
exec 'AsyncRun! time bash %'
elseif &filetype ==# 'python'
exec 'AsyncRun! time python3 "%"'

Put this command in a file stored in this path ~/.vim/after/ftplugin/qf.vim :
wincmd J " Forces QFW to go to bottom of screen across all windows
Reload vim and the quickfix window should be at the bottom.

Related

How do I put the focus on the QuickFix window after automatically opening the QuickFix list?

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.

"mo" to open a file with default application,

When I happened to issue "mo" in NERDTree, it open a file in default application
which is awesome, but, I cannot not refer to a documentation for such an operation.
Where could I find where the "mo" is defined?
It is not in "help".
Here is my vimrc
> execute pathogen#infect()
nnoremap <silent> <F5> :NERDTree<CR>
"syntax enable
set background=dark
filetype plugin indent on
syntax on
"NERDTree Config
autocmd vimenter * NERDTree
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 0 && !exists("s:std_in") | NERDTree | endif
autocmd StdinReadPre * let s:std_in=1
autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists("s:std_in") | exe 'NERDTree' argv()[0] | wincmd p | ene | endif
map <leader>r :NERDTreeFind<cr>
"Edit process
set number
"set ignorecase
"set smartcase
set spell spelllang=en_us
"Switch between the tabs
map <C-l> :tabn<CR>
map <C-h> :tabp<CR>
map <C-n> :tabnew<CR>
It seems that m is mapped to the menu:
call s:initVariable("g:NERDTreeMapMenu", "m")
It doesn't seem to be a default menu-item, since the NERDTree documentation states the following:
A programmable menu system is provided (simulates right clicking on a
node)
one default menu plugin is provided to perform basic filesystem
operations (create/delete/move/copy files/directories)
There's an API for adding your own keymappings
The last item is your key here, there is an API which allows you and other plugins to expand the menu:
call NERDTreeAddMenuItem({
\ 'text': 'e(x)ecute',
\ 'shortcut': 'x',
\ 'callback': 'NERDTreeExecute' })
This is an example from this plugin:
https://github.com/ivalkeen/nerdtree-execute/blob/master/nerdtree_plugin/execute_menuitem.vim
So I bet you have another Plugin installed which maps o as shortcut in the NERDTreeMenu.

How do I run an autocommand on every filetype but one?

I have my vim set up to save files whenever I change buffers and on checktime. The problem is that I use Netrw and end up saving Netrw buffers. Can I run an autocommand on every type of file except netrw?
You can use an :if in your autocmd to guard against netrw files. e.g.
autocmd FileType * if &ft != 'netrw' | echo "do something" | endif
However this still isn't quite right. You have stopped from saving netrw buffers, but there are other buffers that shouldn't be saved. I would suggest checking 'buftype' and looking for files that start with a protocol e.g. foo://.
Here is an example of auto creating intermediary directories using such an approach:
" create parent directories
" https://stackoverflow.com/questions/4292733/vim-creating-parent-directories-on-save
function! s:MkNonExDir(file, buf)
if empty(getbufvar(a:buf, '&buftype')) && a:file!~#'\v^\w+\:\/'
let dir=fnamemodify(a:file, ':h')
if !isdirectory(dir)
call mkdir(dir, 'p')
endif
endif
endfunction
augroup BWCCreateDir
autocmd!
autocmd BufWritePre * :call s:MkNonExDir(expand('<afile>'), +expand('<abuf>'))
augroup END

Set BufDelete autocmd for particular filetype

I'm writing a small snippet for quickfix buffers. I need to add an autocmd for quickfix buffers for BufDelete event.
I have following in ~/.vim/ftplugin/qf.vim
augroup quickr_preview
autocmd!
autocmd BufDelete <buffer> echom "Hey"
augroup END
The autocmd is getting executed before the quickfix buffer is loaded. So the BufDelete autocmd gets set for the calling buffer and not the quickfix buffer.
I've also tried putting autocmd FileType qf autocmd BufDelete <buffer> echom "Hey" directly in my ~/.vimrc, but that has same effect.
How to go about this?
For now I'm going with following.
function! QuickFixBufDelete()
if &buftype == 'quickfix'
autocmd BufDelete <buffer> echom "Hey"
endif
endfunction
autocmd BufCreate * call QuickFixBufDelete()
This is not good as the autocmd gets invoked for all filetypes and then I check for the filetype in my function.
The current answer is no longer working for me. I am now trying the BufReadPost auto command, and it seems to be playing nicely ...
augroup quickr_preview_auto_cmds
autocmd!
autocmd BufReadPost quickfix
\ if !exists('b:quickr_preview_auto_cmds')
\ | exe 'autocmd BufDelete <buffer> pclose! | sign unplace 26'
\ | let b:quickr_preview_auto_cmds = 1
\ | endif
augroup END
This matches what is described in the vim help for qiuckfix-window ...
When the quickfix window has been filled, two autocommand events are
triggered. First the 'filetype' option is set to "qf", which triggers the
FileType event. Then the BufReadPost event is triggered, using "quickfix" for
the buffer name. This can be used to perform some action on the listed
errors.

autocmd function always executed twice

I put together the following pile of awesomeness:
if !exists("g:AsciidocAutoSave")
let g:AsciidocAutoSave = 0
endif
fun! AsciidocRefresh()
if g:AsciidocAutoSave == 1
execute 'write'
execute 'silent !asciidoc -b html5 -a icons "%"'
endif
endf
fun! AsciidocFocusLost()
augroup asciidocFocusLost
autocmd FocusLost <buffer> call AsciidocRefresh()
augroup END
endfun
augroup asciidocFileType
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
The only problem is: It saves twice every time vim loses focus when in an asciidoc file.
When I put :autocmd asciidocFileType into the command line, it shows:
---Auto-Commands---
asciidocFileType FileType
asciidoc :call AsciidocFocusLost()
:call AsciidocFocusLost()
The same with :autocmd asciidocFocusLost and AsciidocRefresh().
Why this duplication?
I can't tell you for sure why you got those duplicates (multiple sourcing?), but the canonical way to prevent this is clearing the augroup first:
augroup asciidocFileType
autocmd!
autocmd FileType asciidoc :call AsciidocFocusLost()
augroup END
Since you only have one definition, you can also do this in one command:
augroup asciidocFileType
autocmd! FileType asciidoc :call AsciidocFocusLost()
augroup END
#ingo-karkat's answer is great, but I think I know why you get twice as many autocmds and how to prevent it naturally:
Some distributions of vim / Neovim make sure by default that filetype plugin indent is on. Test it for neovim with env XDG_CONFIG_HOME=/dev/null nvim -c 'filetype' or for vim with env HOME=/dev/null vim -c filetype
If the output is
filetype detection:ON plugin:ON indent:ON
Then You don't need to add filetype * on to your vimrc.

Resources