Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VIM : How to map command according to buffer type?

Tags:

vim

windows

I know my title is not that descriptive/clear so let me explain.

Yesterday I can across Vimgrep and copen so I added the below line in .vimrc

nnoremap <silent> ,/ :execute 'vimgrep /'.@/.'/g %'<CR>:copen<CR>

What it does is open a quickfix list for the searched(highlighted) word in vim.

Now when you press Enter<CR> in quickfix list it takes you to a corresponding line in the main Vim Window.

Now, the problem I am facing is I have mapped my <CR like map <CR> o<Esc> which creates a new line just below the current line.

So, as you can see there is a conflict b/w the two. So, what I am trying is to come up with a vim function like

Pseudo Code

function IfNotInsideQuickFix()
    if buffer != QuickFix
        then map <CR> o<Esc>
    elif buffer == QuickFix
         Normal behaviour

Call function

Here is the output of :buffers

:buffers
  1 #a   "test.cs"                      line 0
  2 %a-  "[Quickfix List]"              line 1
Press ENTER or type command to continue

I have no experience in writing vim functions. So, can someone please guide me to it.

PS: I know that I can change my Vim mapping to create a new line but I want to learn how to do it via Vim function

like image 636
RanRag Avatar asked Aug 16 '12 08:08

RanRag


People also ask

How do I navigate between buffers in Vim?

Pressing Alt-F12 opens a window listing the buffers, and you can press Enter on a buffer name to go to that buffer. Or, press F12 (next) or Shift-F12 (previous) to cycle through the buffers.

How do I map a vim key?

Creating keymaps To map a sequence of keys to execute another sequence of keys, use the ':map' command. For example, the following command maps the <F2> key to display the current date and time. The ':map' command creates a key map that works in normal, visual, select and operator pending modes.

What is silent in vim mapping?

<silent> tells vim to show no message when this key sequence is used. <leader> means the key sequence starts with the character assigned to variable mapleader -- a backslash, if no let mapleader = statement has executed yet at the point nmap executes.


1 Answers

You can keep your global <CR> mapping as-is, but locally in quickfix windows restore the original behavior. This is done via an :autocmd (triggered when the quickfix window opens) that maps (without remapping) <CR> (locally via <buffer>) onto itself.

:autocmd BufReadPost quickfix nnoremap <buffer> <CR> <CR>
like image 102
Ingo Karkat Avatar answered Oct 13 '22 21:10

Ingo Karkat