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)

1 Answer 1

2

I think you can use norm c to call it.

Another way is exe 'norm c'

4
  • 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, 2012 at 9:57
  • :normal gg is the same as typing gg in NORMAL mode.
    – kev
    Aug 17, 2012 at 9: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, 2012 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, 2012 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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