2

I'm new to Vim, and for the paste couple of weeks, I've been using NERDTree with no problems. However, I recently added a few key mappings and am now unable to open NERDTree with the <C-n> command. Nothing happens when I run the command.

Here's the part of my .vimrc that I found to be causing the problem.

" Map semicolon to colon and vice versa
nnoremap ; :
nnoremap : ;

" Map shortcuts to align = for Tabularize
vmap <Leader>a= :Tabularize /=<CR>
" The following line doesn't work as well.
nmap <Leader>a= :Tabularize /=<CR>

Once I remove the nnoremap : ; command, NERDTree begins working again. Why is this happening, and how can I fix it?

Additionally, the shortcut to call Tabularize works in the visual mode but not in insert mode. I don't know if this and the NERDTree problems are related.

Background information: I'm running Vim 7.4.52 on bash on Windows (Windows Subsystem for Linux).

2
  • Welcome to our site. This question How do I debug my vimrc should allow you to isolate the issue. Once you find what code causes your issue, you can edit your question to be more precise about your problem and we can reopen the question.
    – statox
    Apr 23, 2018 at 9:03
  • 1
    @statox, I've isolated the issue and updated my question. Apr 23, 2018 at 9:16

3 Answers 3

3

The problem is that when you invoke your Tabularize mapping, the : in the right-hand-side of the mapping is remapped to a ; by your colon map, and the keystrokes that are then invoked are:

;Tabularize /=<CR>

In order to fix this, you need to tell your Tabularize mapping to ignore existing mappings, and instead use the default functionality. You do this by using the non-recursive form of the map command:

nnoremap <Leader>a= :Tabularize /=<CR>

This tells Vim to use an actual colon, and to ignore anything that colon (and everything else in the rhs) may be mapped to.

Note that your vmap command is subject to similar issues: it's just that you don't currently have colon mapped to anything in visual mode, so it works by happenstance. To make your mapping robust, you should use vnoremap here, similarly.

I imagine this is also causing your NERDTree issue: I don't believe Ctrl-N is a standard keystroke for opening NERDTree, so you've probably got that set up as a mapping in your .vimrc somewhere, and it's probably not using the non-recursive form of the map command.

As a general rule of thumb, you should always use the nore forms of the various map commands, only using the bare forms if you have a specific reason to do so, which will happen infrequently (or never!)

1
  • Yup, once I replaced nmap with nnoremap for NERDTree, the problem was solved. Thanks for the help! Apr 30, 2018 at 7:08
1

I had the same problem, what solved it for me was to put my line for nerd tree mapping towards the bottom of the .vimrc, AFTER where you remap the 'leader key'. I used nerd tree toggle so it can open and close easily. Here's what worked for me (using 'leader ctrl+b' like bookmark in a browser):

:map <leader><C-b> :NERDTreeToggle<CR>
1
  • Welcome to Vi and Vim! Note that this still won't work for the OP if you use map instead of noremap—the : will get re-mapped to ;.
    – D. Ben Knoble
    Sep 6, 2021 at 19:33
0

I also set 3 versions of the command starting with nnoremap, inoremap, and vnoremap. These correspond to normal mode, insert mode, and visual mode. That explains why it wouldn't work half the time for me!

1
  • Welcome to Vi and Vim! I'm afraid it having multiple version specific mappings doesn't really explain anything: when writing an answer, your goal should be to provide as much detail as necessary to resolve the querent's problem.
    – D. Ben Knoble
    Aug 28, 2021 at 17:38

Your Answer

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

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