vimscript: make a key mapping works only in a specified buffer - vim

I'a writing a plugin. I opened a buffer, and drap some menu items in it. I want to trigger some special function when a enter key is pressed. I used nmap :func() , but enter key triggered the func in other buffers as well.
Can anybody tell me how to do keymapping that works only in the specified buffer?
Or any ideas about how to make a menu is welcome, I don't have any experience in writing plugins before.
Thanks in advance.
Regards,
Dean

You can restrict a keymapping to the current buffer by adding <buffer> to the mapping, like this:
nmap <buffer> <CR> :call func()<CR>
See :help map-arguments for details.
You can add menu items to the main menu via the amenu command:
amenu &MyMenu.MyMenu&Item :call MyFunc()<CR>
This command adds a new menu MyMenu and a new menu item MyMenuItem to MyMenu.
You can remove the menu item (or the whole menu) via the aunmenu command.
See :help amenu and :help aunmenu for details.

Related

Vim key mapping for emmet-vim

Ok here is what I want to accomplish:
In INSERT mode I would like emmet to autocomplete with the TAB key
Here is what I have tried
let g:user_emmet_expandabbr_key = '<tab>' (Only works in NORMAL mode)
Though the above code is useful, I need it to work in INSERT mode
I am transferring over from Sublime Text to VIM and I miss having the Emmet functionality so easily accessible. Any ideas as to how I can achieve this?
Thanks in advance.
Solved my problem by including the following lines in my .vimrc file.
let g:user_emmet_expandabbr_key='<Tab>'
imap <expr> <tab> emmet#expandAbbrIntelligent("\<tab>")
Now I can use the TAB key to both indent and activate Emmet snippets in INSERT mode :D
I'm guessing that the reason for your setting not working in INSERT mode is because <tab> is mapped to something elsewhere (most likely with some auto-completion plugins). You can try to find what's mapped by :verbose imap <tab> when editing HTML files and disable that, but I think the simpler solution is to override the mapping yourself, like this:
augroup EmmetSettings
autocmd! FileType html imap <tab> <plug>(emmet-expand-abbr)
augroup END
To know what is done above, see :h autocmd (and :h augroup). Basically it's telling vim to execute the specified command when editing html files. To know about other mappings you can use, see the doc.

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.

What mode does vim enter when completion menu is up?

I recently changed my arrow keys to map to no-op to force myself to use vim navigation properly. When using word completion in vim a word-list of possible completions pops up. I used to navigate this by using the arrow keys, but now have to use the same mapping as the completion initiation to iterate through.
When this menu pops up, does vim stay in insert mode, change to a different mode or does it enter a modified insert mode (similar to what paste-toggle does) that I can alter the j and k mappings to move through the list?
If not is there a way of calling a method on pop-up open and close?
The popup menu of insert mode completion candidates belongs to insert mode, there's no special submode for it. To differentiate, you have to use map expressions :inoremap <expr> with a condition on pumvisible(). E.g. to remap j to select the next completion candidate:
:inoremap <expr> j pumvisible() ? '<C-n>' : 'j'
Note that with this, you won't be able to type candidate names containing j to drill down and select from the list (which is especially useful with :set completeopt+=longest).
Navigate completions via <c-p> and <c-n>. See :h ins-completion for more information.

How to select SuperTab completion suggestion without creating new line?

When I hit Enter on SuperTab's popup suggestion, it automatically creates a new line at the end of the inserted keyword.
Is it possible to select an option in the SuperTab popup without creating a new line?
More specifically, is there a way for me to configure space as the method of selecting the code completion suggestion?
inoremap <expr> <Space> pumvisible() ? "\<C-y>" : " "
Note the <C-y> that accepts currently selected option, you may want to use it directly instead of remapping space. You will see more in :h popupmenu-keys.
Put this in your .vimrc
let g:SuperTabCrMapping=1

How can I map a click in Vim?

I want to be able to click (or cmd+click on my Mac) on a function name in Gvim (or vim with set mouse=a) and have it run the command :tag to follow a ctag, but I don't know how to include a mouse click in a Vim mapping.
A good place to start:
:help click
:help mouse
Perhaps something like this will work:
:map <RightMouse> :tag <CR>

Resources