0

This is a followup (but a distinct question) to this question, which I'll reiterate here for completion.

I have a Vim mapping to start searching (ack-grep with ack.vim plugin) for a pattern from the directory that is the current directory (so the result after :pwd). This mapping works when I'm looking at a buffer.

I want to use the same mapping while I'm in netrw. But, I want to change the current directory (:pwd) to the directory netrw is showing me, so the search will be started from the directory I'm looking at. I know I can do this with the netrw c command. How do I give the c command from within a function?

I've tried:

function! StartAckSearch()
    " If we're in netrw change the current directory to the directory we're
    " viewing
    if &ft ==# 'netrw'
        echo 'in netrw'
        c                                                                                                                                                                                                        
    endif
endfunction
nnoremap <Leader>a :call StartAckSearch()<CR>

And:

I've tried:

function! StartAckSearch()
    " If we're in netrw change the current directory to the directory we're
    " viewing
    if &ft ==# 'netrw'
        echo 'in netrw'
        execute 'c'                                                                                                                                                                                                        
    endif
endfunction
nnoremap <Leader>a :call StartAckSearch()<CR>

But they both don't work.

Question How do I call a netrw command using Vimscript? (If my question can be rephrased to be clearer, please go ahead)

Community
  • 1
  • 1
Niels Bom
  • 8,728
  • 11
  • 46
  • 62

1 Answers1

2

I think you can use norm c to call it.

Another way is exe 'norm c'

kev
  • 155,172
  • 47
  • 273
  • 272
  • I've tried `norm c`, which worked. Could you give some more information on why/how this works? Is `norm foo` saying "execute normal command "foo"? – Niels Bom Aug 17 '12 at 09:57
  • `:normal gg` is the same as typing `gg` in **NORMAL** mode. – kev Aug 17 '12 at 09:59
  • I know that's the case when you're looking at a file. But why do I have to explicitly say `norm c` when I'm in Vimscript AND in netrw? If I do just `c` the error makes me think it wants to c(hange) stuff. – Niels Bom Aug 17 '12 at 10:05
  • `:c` is different from `c`. For example, `:x`(COMMAND mode) is save and quit, but `x`(NORMAL mode) is delete character under cursor. In vim script, you can omit the `:`, so `c` is actually `:c`. – kev Aug 17 '12 at 10:13