1

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, but obviously I only want to give this command if I'm actually in netrw.

Question How do I detect whether the current window is in netrw "mode"?

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

1 Answers1

6

You can test the filetype:

if &ft ==# "netrw"
    " your code here
endif
kev
  • 155,172
  • 47
  • 273
  • 272
  • 2
    Thank you! I read [here](http://learnvimscriptthehardway.stevelosh.com/chapters/22.html) that always using `==#` instead of `==` is a good habit. If you agree you could update your answer. – Niels Bom Aug 17 '12 at 09:07
  • 2
    @NielsBom More, if you think it is possible to receive number instead of string you should use `is#`, it is like `===` in javascript (`type(a)==type(b) && a==#b`) for scalar types and `is` from python for lists and dictionaries (checks whether two objects are the same (internal: pointer equality comparison)). I personally use `is#` for any string equality comparisons, `is` when constant is numeric, but string is also an option (`a is 0`, like `a is None` in python), `==#` for complex structures (unless I need `is`), `==` for numbers if no strings are expected. – ZyX Aug 17 '12 at 09:36
  • The first rule prevents me from thinking whether or not I [will] want to be able to use numbers (only 0 almost always) there as some special value(s). Second is just less typing: `a is 0` and `a is# 0` are equivalent. `a == 0` cannot be used because `"a" == 0` is true. Third is just the only option (and nobody likes to depend on the value of `'ignorecase'`). Fourth is again less typing: `a is 0` needs at least one space after `a`. `a==0` needs no. – ZyX Aug 17 '12 at 09:43
  • Not applicable for `&filetype` though: it is purely string option which can be ever neither a number nor, specially, zero (zero is the only number which will emit true in `N ==# 'netrw'`). – ZyX Aug 17 '12 at 09:46
  • @ZyX Wow, detailed :-) thanks. If I were you I'd try and add your comments to (Steve Losh's book)[https://github.com/sjl/learnvimscriptthehardway/] – Niels Bom Aug 17 '12 at 09:55