0

I'm writing mapping, and I want to decide whether I have to do additional actions when current buffer type is a drawer called Fern, here is my command currently

nnoremap <nowait><silent> ;
            \ <CR>:Fern %:h:h -drawer -width=36 -toggle -reveal=%<CR>

What I want to do is like:

nnoremap <nowait><silent><expr> ;
        \ buffertype(current_buffer)=='Fern'? ":w" : ""
        \ ."\<CR>:Fern %:h:h -drawer -width=36 -toggle -reveal=%\<CR>"

So what should be the substitute of pseudocode buffertype(current_buffer) here? Do we really have function alike?

9
  • Buffer type? Are you talking about file type? – B Layer Apr 4 at 4:19
  • @BLayer: I probably use the wrong phrase, I want to detect my current is currently inside the drawer. So it's called file type? – job_start Apr 4 at 4:21
  • 1
    Yes. For instance, nerdtree buffers have file types like nerdtree or something like that. – B Layer Apr 4 at 4:23
  • 1
    Sure...but did you try it? Can't be 100% sure until it's tested. :) – B Layer Apr 4 at 4:24
  • 1
    @BLayer: It output: filetype=fern, I think you are correct! – job_start Apr 4 at 4:25
0

With @B Layer's kindly comments, I found that what I should detect is the file type, not buffer type, of the current window where the cursor at.

The final result is:

nnoremap <nowait><silent><expr> ;
            \ (&filetype!='fern' ? ":w\<CR>" : "")
            \ .":Fern %:h:h -drawer -width=36 -toggle -reveal=%\<CR>"

(and it should be !=fern, not ==fern, an logical error compare to my OP)

Update:

To also open the drawer on [No Name] file, i.e. filetype= nothing, we have:

nnoremap <nowait><silent><expr> ;
            \ (&filetype!='fern' && &filetype!=''? ":w\<CR>" : "")
            \ .":Fern %:h:h -drawer -width=36 -toggle -reveal=%\<CR>"

or this tricky <bar><bar> version:

nnoremap <nowait><silent><expr> ;
            \ (&filetype=="fern"<bar><bar>&filetype==""? "" : ":w\<CR>")
            \ .":Fern %:h:h -drawer -width=36 -toggle -reveal=%\<CR>"

The second one is tricky that if you use || it will show trailing characters error. In theory the second one is faster by short-circuiting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.