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

The problem I am facing is that I displayed some keys and mouse events for serialization in vim while editing the file. But these mappings affect functionality if a quick fix buffer.

I was wondering if keys can be displayed depending on the buffer in which they are used.

EDIT - I am adding additional information on this subject. Consider a scenario. I want to display <C-F4> to close the buffer / window. Now this behavior may depend on several things.

If I edit the buffer, it should just close this buffer without changing the layout of the windows. For this I use buffkil .

It does not depend on the file extension, but on the type of buffer. I saw in the vim documentation that there is a list and a list in a list. Therefore, if it is specified in the buffer, it should close using bufkill commands.

If it is not a specified buffer, it should use the <cw>c command to close the buffer and change the window layout.

I am new to writing vim functions / scripts, can someone help me get started on this

+4
source share
4 answers
 function KillBuffer() if &buflisted " bufkill command here else execute "normal! \<Cw>c" endif endfunction noremap <C-F4> :call KillBuffer()<CR> 

Put this in your .vimrc Or, if you want to treat the quickfix window as unregistered buffers (it is specified in my Vim):

 function KillBuffer() if &buflisted && !&filetype=="qf" " bufkill command here else execute "normal! \<Cw>c" endif endfunction noremap <C-F4> :call KillBuffer()<CR> 

According to the manual, you can replace execute "normal! \<Cw>c" with a simpler close! in the above scenarios.

+4
source

You can intercept the loading of certain types of files and assign buffer mappings.

 au! BufRead *.ext call <SID>init_hotkeys() function s:init_hotkeys() nnoremap <buffer> <CR> :Action<CR> endfunction 

To display complex logic in a hotkey, you can use something like this in your vimrc or even better - put the following in 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 <cw>c endif endfunction nnoremap <C-F4> :call <SID>close_buffer()<CR> 
+1
source

You can create specific file type parameters. First, in the vimrc file, make sure the filetype plugins are enabled by adding

filet plugin on

Then create a special file of type filetype. On Unix, create a file called ~ / .vim / after / ftplugin / [file-type-name] .vim and place your mapping there. On Windows, the directory is $ HOME / vimfiles / after / ftplugin. [FileName] is the type discovered by Vim, sometimes the same as the file name extension, for example c.vim, python.vim, etc. Vim can tell you what type after opening the file, if you enter

: echo & ft

+1
source

I use this in my vimrc to insert an empty line above or below the current line, using only return and shift-return (unlike o<Esc> or o<Esc> ), without interfering with the behavior of the open file you want in quick delete 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> 

Hope someone finds this helpful.

0
source

Source: https://habr.com/ru/post/1303952/


All Articles