Why does opening the NERDTree in VIM need about 1~2 seconds? - vim

I installed the NERDTree plugin in the vimrc
" Define map <Leader>
let mapleader = " "
let maplocalleader = " "
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree'
call plug#end()
nmap <Leader>nt :NERDTreeToggle<cr>
when I input Spacent, it needs about 2.x seconds to open the NERDTree no matter how many files/dirs in the current directory.
I don't know why. anything I can do to improve the performance to open it e.g.(in 1 second)
thx.

The only way you can answer this, barring finding a bug, is to profile Nerdtree using vim's built-in tools. Take a look at :help profile to see how to use them.

Related

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>'.

Is it possible to configure behaviour of NERDTreeToggle to use NERDTreeFind when opening?

Is it possible to configure NERDTree so that :NERDTreeToggle acts like :NERDTreeFind if a buffer is not open (instead of the default :NERDTree) ?
Failing that, would it be possible to create a mapping/small script that could check the visibility of the NERDTree window and if open close it, if closed invoke NERDTreeFind ?
I looked at the NERDTree documentation to find if the visibility of the NERDTree window was open, but could not find it.
You can set let NERDTreeQuitOnOpen=1 to close the tree when you select a file, and create a mapping for find:
nmap <leader>p :NERDTreeFind<CR>
there is a function, which may help you to distinguish if the NERDTree is opened.
nerdtree#isTreeOpen()
you could test a little bit with :echom nerdtree#isTreeOpen() when you opened/closed the Nerdtree.
I'm now learning to use Vim too and had the same desire to make NERDTreeToggle to use NERDTreeFind when opening. After some digging/googling, I had a go at writing a simple Vim script below, and it seems to work for me! :]
function! ToggleNERDTreeFind()
if g:NERDTree.IsOpen()
execute ':NERDTreeClose'
else
execute ':NERDTreeFind'
endif
endfunction
And I just bound the above function to a shortcut key to use for both finding/closing NERDTree. Hope this helps.
nnoremap <leader>f :call ToggleNERDTreeFind()<CR>
small improvment of the function provided by thomaswhyyou which also works
if current buffer is empty:
function! ToggleNERDTreeFind()
if g:NERDTree.IsOpen()
execute ':NERDTreeClose'
else
if bufname('%') == ''
execute ':NERDTree'
else
execute ':NERDTreeFind'
endif
endif
endfunction

Buffer Explorer for vim

What do people recommend for easier manipulation of buffers in vim?
Using ls and b1, bn and bp commands is good but maybe there is a better way.
Is lusty explorer the best option?
I am using vim 7.3.
You should test all of them and see which one is the best according to your tastes and requirements.
I've used LustyExplorer for a while and loved it until I tried CtrlP which I find faster and more intuitive. I have :CtrlPBuffer mapped to ,b and see no reason to complain: it's both elegant, fast and intuitive.
You don't have to rely on plugins, though: :b <tab> allows you to tab through a list of all available buffers. You can do :b pattern<Tab> to get a smaller list.
Unite.vim is a new plugin and is what I switched to from CtrlP.
This is a good starting point if you want to explore what it can do.
I use minibufexpl.vim. I guess its main advantage is that it takes up very little space.
FuzzyFinder is another excellent add-on for buffer/file navigation:
http://www.vim.org/scripts/script.php?script_id=1984
Whichever plugin you choose for this, it's worth investing some time to find out all the ways it can help you.
If you are fine with having vim compiled with ruby support and have dev toolchain installed on the system (make, gcc, maybe something else — Gentoo users like me already have all of this) then Command-T is a good choice. To use it for switching buffers you should map something to :CommandTBuffer, I have
nnoremap ,b :CommandTBuffer<CR>
I used many plugins before, including minibufexpl and Bufexplorer, but there was something in all of them that used to annoy me.
Now I use young plugin Buffet, and I would recommend it because it seems to be really the best one for me: it is really fast and easy to use.
Personally i would like to switch my buffers by Ctrl+Tab and Shift+Ctrl+Tab, and buffers should be ordered in most-recently-used order.
Here is my buffet's config to achieve <C-Tab> and <S-C-Tab> switching:
noremap <silent> <C-Tab> :Bufferlistsw<CR>
noremap <silent> <C-S-Tab> :Bufferlistsw<CR>kk
if !has('gui')
map <S-q> :Bufferlistsw<CR>
endif
augroup BuffetAdd
if !exists("g:BuffetAdded")
let g:BuffetAdded = 1
au BufWinEnter buflisttempbuffer* map <buffer> <Tab> <CR>
au BufWinEnter buflisttempbuffer* map <buffer> <C-Tab> j
au BufWinEnter buflisttempbuffer* map <buffer> <C-S-Tab> k
" in console Vim we can't use <C-Tab> mappings (almost always),
" so this is temporary solution: <S-q>
if !has('gui')
au BufWinEnter buflisttempbuffer* map <buffer> <S-q> j
au BufWinEnter buflisttempbuffer* map <buffer> q <CR>
endif
" workaround Surround plugin issue in Buffet's window:
" disable "ds" mapping in the Buffet window (to make "d" work fast)
au BufEnter buflisttempbuffer* nunmap ds
au BufLeave buflisttempbuffer* nmap ds <Plug>Dsurround
endif
augroup END
Just one issue: Vim does not allow you to map release of some key, so, you need to press Tab again to really switch to buffer.
Anyway, if you don't need <C-Tab> switching, Buffet plugin works nice without it.
Update June 2019
BufExplorer is my unequivocal first-choice for buffer management.
" Buffer explorer
" ,be to open, q to close, d to delete buffer
Plug 'jlanzarotta/bufexplorer'
Highly rate the above plugin. It's simple and effective. Further details in the readme.
If you're looking for some "extras" in addition to the above (optional), I do also use:
" Close buffers but keep splits
Plug 'moll/vim-bbye'
and:
Plug '/usr/local/opt/fzf'
Plug 'junegunn/fzf.vim'
" this setting for quick search across buffers
nmap <silent> <leader>b :Buffers<cr>

How change short keys in Vim?

I am a new user in Vim. How change these keys in Zen Coding,
ctr+y+,
To
ctr+e
And also change in omni,
ctr+x ctr+o
To
ctr+j
How can I do that?
I suggest you to type:
:help map.txt
inside vim, you'll find all the explanation to understand how to do it.
You can't use the same shortcut for 'zencoding' plugin and for an omnicomplete function; anyway you could add to your .vimrc:
imap <C-j> <C-y>
But I suggest not to use 'C-j' as 'j' is always related to movement in vim; use 'leader' (:help leader) which is targeted to user shortcuts, instead.
You may follow the answer provided by #eolo999, but I suggest you to read zencoding documentation and add the following to the vimrc:
" Note the `nore'. You must use it where possible "
" in order not to get remapping problems when your vimrc grows up "
inoremap <C-j> <C-x><C-o>
" from :h zencoding-customize-keymappings "
let g:user_zen_expandabbr_key='<C-e>'

How to map keys in vim differently for different kinds of buffers

The problem i am facing is that i have mapped some keys and mouse events for seraching in vim while editing a file. But those mappings impact the functionality if the quickfix buffer.
I was wondering if it is possible to map keys depending on the buffer in which they are used.
EDIT - I am adding more info for this question
Let us consider a scenario. I want to map <C-F4> to close a buffer/window. Now this behavior could depend on a number of things.
If i am editing a buffer it should just close that buffer without changing the layout of the windows. I am using buffkil plugin for this.
It does not depend on extension of file but on the type of buffer. I saw in vim documentation that there are unlisted and listed buffer. So if it is listed buffer it should close using bufkill commands.
If it is not a listed buffer it should use <c-w>c command to close buffer and changing the window layout.
I am new at writing vim functions/scripts, can someone help me getting started on this
function KillBuffer()
if &buflisted
" bufkill command here
else
execute "normal! \<C-w>c"
endif
endfunction
noremap <C-F4> :call KillBuffer()<CR>
Put this in your .vimrc
Or, if you want to handle quickfix window as unlisted buffers (in my Vim it is listed):
function KillBuffer()
if &buflisted && !&filetype=="qf"
" bufkill command here
else
execute "normal! \<C-w>c"
endif
endfunction
noremap <C-F4> :call KillBuffer()<CR>
According to the manual, you could replace execute "normal! \<C-w>c" with simpler close! in the above scripts.
You can create filetype specific settings. First, in your vimrc file, make sure filetype plugins are enabled by adding
filet plugin on
Then make a filetype specific plugin. Under Unix create a file called ~/.vim/after/ftplugin/[file-type-name].vim and put your mapping in there. In Windows the directory is $HOME/vimfiles/after/ftplugin. The [file-type-name] is the type detected by Vim, sometimes the same as the filename extension, e.g c.vim, python.vim, etc. Vim can tell you what the type is after you open the file if you enter
:echo &ft
You can intercept certain types of files loading and assign buffer specific mappings.
au! BufRead *.ext call <SID>init_hotkeys()
function s:init_hotkeys()
nnoremap <buffer> <CR> :Action<CR>
endfunction
To map complex logic on the hotkey you can use write something like this in your vimrc, or even better - put the following to the closebuffer.vim file inside your vim plugin directory
function s:close_buffer()
if &buflisted
" your command here from the killbuf plugin
echo "Listed Buffer"
else
wincmd c
" or
" normal <c-w>c
endif
endfunction
nnoremap <C-F4> :call <SID>close_buffer()<CR>
I use this in my vimrc to insert an empty line above or below the current line using only return and shift-return (as opposed to o<Esc> or O<Esc>) without interfering with the open file behaviour you want in the quickfix list.
" Use enter to insert newlines in normal mode, but not in quickfix
function! s:insert_line(direction)
if &buftype == "quickfix"
execute "normal! \<Enter>"
else
if a:direction == 'below'
execute "normal! o\<Esc>"
else
execute "normal! O\<Esc>"
endif
endif
endfunction
nmap <Enter> :call <SID>insert_line('below')<CR>
nmap <S-Enter> :call <SID>insert_line('above')<CR>
Hopefully someone else will find this useful.

Resources