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>'.
Related
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") })
I have previously asked this in comp.editors, but without getting any replies.
This ought to be simple: I want to configure vim to set number whenever the editor is in ex mode (to get visible line numbers), but never have that option set in visual mode. How?
If the solution involves having to start ex instead of vim, that's fine, but the solution should ideally also cover the case of entering ex mode from visual mode (using Q in vim visual mode, for example).
A solution that also works in nvi would be nice, but not necessary.
You can try something like this:
let &number = mode(1) ==# 'ce'
nnoremap <silent> Q :set number<CR>Q
This will set number when you run vim -e and when you enter ex mode with Q, but it won't clear it when you go back to visual mode. As far as I can tell there is no way to detect the actual event of switching modes. shrug
Vim's Autocmd seemed like your best bet, since it has event listeners. An example would be
:autocmd InsertLeave * :set nonumber
:autocmd InsertEnter * :set number
which shows/hides line numbers
However, I couldn't find any events for Ex mode when I looked.
I'm using iTerm2 and Vim 7.4 on top of OS X 10.9.
In my bash shell, my cursor is a blinking line. I've installed Vitality (https://github.com/sjl/vitality.vim/) in order to get the Vim cursor to be a block in normal mode and a line in insert mode. Then, in order to get my cursor to revert to a line on exiting vim, I've added the following autocmd to my .vimrc (sourced from this stack overflow question):
autocmd VimLeave * let &t_me="\<Esc>]50;CursorShape=1\x7"
This is all working great; the one problem is that when I suspend Vim via Ctrl-Z (which I do frequently), my cursor remains a block. Is there some way to detect that Vim is being suspended (maybe via an autocmd) and set the cursor to a line? Also, presumably I would then have to reset the cursor to a block on resuming Vim.
There's no :autocmd event for suspending, but you can solve this part by hooking into the <C-z> command:
:nnoremap <silent> <C-z> :let &t_me=...<CR><C-z>
Restoring the cursor on restore is more difficult. It looks like the Vitality plugin already uses autocmd events to change the shape, so one mode change (into / out of insert mode) would be required to correct things.
If that's not enough, you'd have to install a separate fire-once autocmd (e.g. on CursorMoved,CursorHold) in the above mapping. Or you could try sending the :let command via feedkeys(), in the hope that it would only be executed after Vim awakes (not tested that).
I use Ingo's suggestion along with what I found here:
Update Vim after it suspended?
... To toggle a different terminal setting (called "Bracketed Paste Mode") when
I suspend Vim. You can tweak this for any other escape sequence pairs you
need, as the general concept is not BPM specific. This trick solves the 'fg'
problem. Here it is:
" (Re)Set Bracketed Paste Mode
function SetBPM(mode)
execute "silent !echo -ne '\033[?2004" . a:mode . "'"
endfunction
" toggle BPM when suspending (hook ctrl-z)...
nnoremap <silent> <C-z> :call SetBPM("l")<CR>:suspend<bar>:call SetBPM("h")<CR>
I've tried the following in my .vimrc:
:au FocusLost * silent! wa
autocmd BufLeave,FocusLost silent! wall
And also tried:
How can I make Vim autosave files when it loses focus?
but can't get it to work, when I do a Ctrl+Z or switch to another tab in Terminal (mac) it still doesn't auto save.
BufLeave is triggered when you go to another buffer. Neither <C-z> nor switching to another Terminal.app tab will trigger this because you are using CLI Vim which doesn't care at all about the terminal emulator's GUI or environment and… you are not leaving your buffer.
The same is true for FocusLost (more or less, the doc says that it's GUI only but can work in some terminals without telling which one).
So, these setting will probably work in MacVim but definetly not in CLI Vim.
Actually, since Vim is not aware of your terminal emulator's tabs or about it being put in the background, I doubt you can achieve your goal in CLI Vim.
I happen to have autocmd FocusLost * :wa in my ~/.vimrc but I've put it in an if has("gui_running") conditional and also inoremap <Esc> <Esc>:w<CR> to save on ESC. Hope it helps.
On OS X and Vim CLI, I use this plugin http://www.vim.org/scripts/script.php?script_id=4521
AutoSave - automatically save changes to disk without having to use :w
(or any binding to it) every time a buffer has been modified.
AutoSave is disabled by default, run :AutoSaveToggle to enable/disable
AutoSave. If you want plugin to be always enabled it can be done with
g:auto_save option (place 'let g:auto_save = 1' in your .vimrc).
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