vimrc: how to auto change window focus when vim is loaded - vim

I've let tagbar and NERDtree autoload when vim is opening any file.
The window layout from left to right is:
NERDTree----My source code----TagBar
The problem is, each time vim is up, the NERDtree on the left gets the focus(keyboard). I wish to make the middle window(My source code) having the focus, so I can start coding immediately. Or else I have to C-w l to switch windows each time.
How to set this in ~/.vimrc?
Thanks a lot.

The plugins probably use :autocmd VimEnter to open their sidebars. You can define a similar autocmd, but it has to run after the plugins'.
Put the following into ~/.vim/after/plugin/middleWindow.vim:
autocmd VimEnter * 2wincmd w
This goes to the second available window.

It's a bit tricky, because I don't know when in your start-up sequence, all three buffers are loaded. If you use Vim8, you can do the wincmd on a timer. This works for me:
call timer_start(100, { -> execute( "wincmd l") })

Related

Open file in vertical split in Vim / netrw

If I open vim with vim . netrw provides me with a nice list of the files and directories in the current directory.
If I open a file using v the file opens in a very narrow split down the left hand side of the screen and the directory listing remains open in a wide split on the right hand side of the screen.
Ideally I'd like it to have the opposite effect. ie. Show the directory listing in a narrow split on the left hand side of the screen and show the file in a wide split on the right hand side of the screen.
Any help much appreciated.
Netrw v153 and later (May 28, 2014) gives you the :Lexplore command, which, by default, opens a directory listing on the left hand side and opens files to the right (by pressing <cr>).
Whilst Jonathan.Brink's answer works perfectly well, simply adding
let g:netrw_altv=1
to .vimrc also seems to do the trick...
See https://superuser.com/questions/1056929/open-file-in-vertical-split-in-vim-netrw/1062063#1062063 for more info.
I'm sure this could be improved upon you can write a custom mapping that target's the netrw filetype.
Stick this in your .vimrc:
" open file vertically to the right
augroup netrw_mappings
autocmd!
autocmd filetype netrw call Netrw_mappings()
augroup END
function! OpenToRight()
:rightbelow vnew
:wincmd p
:normal P
endfunction
function! Netrw_mappings()
noremap V :call OpenToRight()<cr>
endfunction
The only thing is that you need to use V rather than v. For some reason I was unable to override netrw's v command, but using the capital version seems better anyway since it's not overriding a default.

How to emulate key presses in vim script

my question is similar to this one https://superuser.com/questions/277051/how-can-i-emulate-key-presses-on-vim-startup
I'm using the plugin netrw in my vim instead of nerdtree in that question. I add the following line to enable netrw automatically.
autocmd BufWinEnter * :Ve
While open vim, I need to press ^W^W each time because the active buffer is the left one, where the netrw is located.
How can I make vim emulate these key presses on startup? In fact, I've got it works before, but I didn't save my .vimrc in a recently re-installation of OS.
The answer of that question doesn't work for me.
My environment:
Mac OS X 10.9.5
MacVim Snapshot 73
Any answers appreciated, thanks a lot :)
Best way is by just appending the command to the existing :autocmd (separated by |):
autocmd BufWinEnter * Vexplore | wincmd w
Notes
By using BufWinEnter, this will open a netrw split on each displaying of a buffer. If you just want to trigger this once at Vim startup, VimEnter is the right event to use.
Don't abbreviate commands inside plugins and configuration; this is just to save keys on interactive use. It's easier to understand this way, and you don't have the risk of having to adapt all instances when you install / define another command with the same initial letters (e.g. :VerticalSplit). Also, you don't need the : in autocmds.
For <C-W> window commands, there's the special :wincmd Ex command to trigger those. For all else, you would use :execute 'normal! \<C-w>\<C-w>'.

In vim, how can I automatically maximize the help window

When I display a vim helpfile by running e.g. :h au, the help is displayed in a horizontal split window:
Currently I always run Ctrl+w _ but I would prefer the help buffer to open in a maximized window automatically.
I've tried to create an autocmd to solve the issue:
"Automatically maximize help buffers
augroup filetype_help
autocmd!
autocmd BufWinEnter,FileType help wincmd _
augroup END
which only works sporadically.
EDIT:
I have done some further debugging.
Opening a certain help page the first time, e.g. :h au displays it maxmimized when having above augroup in my .vimrc.
Closing the helpfiles window via :q and then reopening the same helpfile a second time causes the help file to be displayed in a split as in the screenshot above.
Closing the helpfiles buffer window via :bd and then reopening it, causes it to being displayed maximized as desired.
How can I rewrite my augroup so that it also maximizes an already opened help buffer?
The BufWinEnter event matches the help filename, so the help pattern (which is fine for a FileType match) won't work. The 'filetype' option is only set once for a buffer, so when it is reused (after :q, but not after :bd), your maximization fails, in the way you've reported.
Instead, have the :autocmd match all buffers, and check for the 'buftype':
augroup filetype_help
autocmd!
autocmd BufWinEnter * if &l:buftype ==# 'help' | wincmd _ | endif
augroup END
I assume you'd like the help window to be horizontally maximized.
Sorry, I can't reproduce your bug (tried on MacVim 7.4-258 and vim 7.4-258), but here are some suggestions:
Try changing wincmd _ to set winheight=9999 in your augroup script.
How about opening the help in a new tab with wincmd T instead?
I think you can achieve your goal of getting your help buffers to be maximized more simply by putting the following in your .vimrc (or wherever), instead of the augroup/autocmd:
:set helpheight=9999
|CTRL-W_o| CTRL-W o close all but current window (like |:only|)

how to disable some features in some specific window in vim?

here is my problem:
i'm using taglist and nerdtree.and set quickfix window displayed no matter if it has context.
and in my vimrc, i set them toggled in a fixed order, so i can get a layout i want.
but when i use C-o, C-i, C-], it will jump to a file, and if i want the features of taglist and nerdtree i should quit it and open it again.
but it will break the layout i want. so i have to quit all and open the file again.
so, is it possible to desable some features in some specific window?
thanks for any help.:)
You can disable certain commands for buffers via :map <buffer>; for sidebar windows like from NERDTree, that's good enough, as they always display the same (scratch) buffer. For example, to disable <C-O> in NERDTree:
:autocmd FileType nerdtree nnoremap <buffer> <C-o> <Nop>
I don't fully understand your question, but another approach (as it is hard to fully control where Vim places new buffer contents) would be to extend your "build your window layout" function from your .vimrc to first clean up any existing NERDTree / TagBar windows, so that you can call it later on (e.g. via a mapping) to "fix up" your layout again.

Getting VIM Tagbar to launch when opening certain file types

I'd like to have the tagbar VIM plugin launch when I open certain filetypes, so I added the following to my .vimrc:
if has("gui_running")
autocmd BufEnter *.hs nested TagbarOpen
However, this isn't working as I expected. It opens a side window, but the side window displays nothing and my cursor is trapped within it. I cannot switch windows with a click or with the CTRL-W movement commands.
However, when I run TagbarOpen manually, it works just fine.
Anyone else tried this, or is the above the wrong command to issue?
Interesting, that's a side effect of a convenience functionality that I hadn't anticipated. What happens is this: If TagbarOpen is called while the window is already open, Tagbar makes the cursor jump to its window instead of just doing nothing (for convenience like I said). So every time you try to leave the window by switching to the Haskell window, the autocommand causes it to jump right back. I've pushed a change that removes this functionality -- it probably wasn't that useful to begin with. So if you give the development version on GitHub a try (https://github.com/majutsushi/tagbar), it should work.
That the window is empty has a different reason: Haskell is not supported by Exuberant Ctags by default. But someone wrote a nice alternative for Haskell that works with Tagbar here: https://github.com/bitc/lushtags.
I actually have this exact configuration set up my vimrc for php files. Tagbar opens with function / variables loaded, cursor stays in php source file when Vim loads:
autocmd FileType php call SetPHPOptions()
function! SetPHPOptions()
setlocal shiftwidth=4 tabstop=4 softtabstop=4 expandtab makeprg=php-xdebug\ %
:call tagbar#autoopen(0)
endfunction
substitute 'php' for 'hs' or any other file type you want. List of filetypes at:
$VIMRUNTIME/filetype.vim
Have it running on MacVim (snapshot 72, Vim 7.4), and latest build of tagbar from https://github.com/majutsushi/tagbar

Resources