tags:

views:

118

answers:

3

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

+1  A: 

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>
Mykola Golubyev
That was helpful but i could not achieve what i intended to. Can you help me with the updated description i have posted
Yogesh Arora
thanks for the answer
Yogesh Arora
+1  A: 

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

Steve K
That was helpful but i could not achieve what i intended to. Can you help me with the updated description i have posted
Yogesh Arora
+1  A: 
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.

ZyX
Yogesh Arora
Vim version? How do you get quickfix window? I tested this by running `:helpg smth`, than `cw` and it had "qf" filetype. Quickfix window obtained by `make` than `cw` commands also has this filetype.
ZyX
Yogesh Arora
ZyX