3

I started using Vim recently, just installed NERDTree (a plugin to navigate files). The command to access that plugin is :NERDTree so I though it's a good idea to start learning mappings by assigning one to that command.

So I added to my .vimrc file the following line: map :nt :NERDTree - but when I type :nt in a vim file (even after restarting) I receive the following error message: not an editor command: nt

I also tried to add the mapping directly while editing a file by typing :map :nt :NERDTree but it returned the same error when I tried to use the command.

I checked that answer:What is the difference between the remap, noremap, nnoremap and vnoremap mapping commands in vim?, so it seems to me that :map (opposed to noremap etc.) is the good command for that.

The plugin works fine when typing the original command.

What am I doing wrong? (sorry for the noob question)

Community
  • 1
  • 1
jim jarnac
  • 4,804
  • 11
  • 51
  • 88

3 Answers3

5
  • :NERDTree is a command, not a mapping, so there's no reason for creating a recursive mapping, here.

  • :map is too overreaching. You should use :<mode>map (for recursive mappings) or :<mode>noremap (for nn-recursive mappings).

  • You are missing a <CR> at the end of your mapping to tell Vim to actually execute the :NERDTree command.

In this specific case, the right mapping would be:

nnoremap :tn :NERDTree<CR>

But mapping something to :<anything> is not a good idea because it will introduce a timeout whenever you try to execute an Ex command. This means that you need to find another combo. Why not <Space>n?

nnoremap <Space>n :NERDTree<CR>
romainl
  • 186,200
  • 21
  • 280
  • 313
  • excellent, thx a lot! Quick question what do you mean by "because it will introduce a timeout whenever you try to execute an Ex command" ? – jim jarnac May 16 '17 at 08:09
  • `:` is the normal mode command that puts you in command-line mode. If you map something to `:` in normal mode, say `:tn`, Vim needs to decide what you actually want to do and uses a timeout for that. If you are a quick typist you probably won't notice it but if you are a normal human being you'll definitely notice it. – romainl May 16 '17 at 08:36
3

With the mapping that you have, it will be require multiple keystroke. Will it be okay for you to use a single key like F2?

 nnoremap <F2> :NERDTreeToggle<CR>

This will toggle open/close NERDTree upon pressing F2 and save you some key stroke.

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
1

Here you can figure out, how vim's mapping work and look like ;). Don't forget to source your new .vimrc before using.

John Goofy
  • 1,330
  • 1
  • 10
  • 20
  • Ok thx. What do you mean by "Don't forget to source your new .vimrc before using" ? – jim jarnac May 16 '17 at 08:10
  • He means that if you add a mapping to your .vimrc, you either need to restart vim or source your .vimrc before the mapping takes effect: `:source ~/.vimrc` – nickspoon May 16 '17 at 08:14
  • @jimbasquiat Great! If you think this answer is the right, please mark this answer as right or please upvote it. About your other question: every rc file, doesn't matter which one, needs to be sourced after updating. So, if you have had any changes in your `.vimrc` file, you need to source it. You have to type in your terminal, if your `pwd` is where your `.vimrc` exists: `source .vimrc` or the short form `. .vimrc`. Only than your update will be recognized by vim. – John Goofy May 16 '17 at 08:21