Create a command shortcut for NERDTree in Vim editor
Asked Answered
Q

6

46

I'd like to create an abbreviation for NERDTree on the command-line. I find it annoying have to write :NERDTree every time I want to enable it. So I'd like to type :nr or something like that. Is that possible?

Qintar answered 24/6, 2011 at 12:13 Comment(1)
Note: Vim doesn't let you add custom commands that start with a lowercase. For Instance, ':ne' would not be valid, but ':Ne' would be.Photocell
F
58

In my .vimrc I have:

let mapleader = ","
nmap <leader>ne :NERDTree<cr>

So when I need NERDTree I just write ,ne in normal mode.

Fact answered 24/6, 2011 at 12:19 Comment(5)
If you want do it like in your question, then add to .vimrc: :command NE NERDTree. Than you can call it: :NEFact
Thanks! I've been using nmap <leader>ne :NERDTreeToggle<cr> to toggle NERDTree on and off.Archuleta
Why isn't this a default =(Wera
It would be nice is this focused in NERDtree if it was already open.Wera
@Fact if you have no object, you should make this :NERDTreeFocus so it doesn't open multiple copies.Wera
P
31

I find this works very nicely, better than any other suggestions I've tried:

map <silent> <C-n> :NERDTreeFocus<CR>

You just hit control-n to go back to the hierarchy. Selecting a file name in the hierarchy will switch you, of course, to that file.

  1. It works in both normal and insert mode
  2. It doesn't close the hierarchy as the accepted answer does (using the :NERDTree command starts you from scratch, closing the hierarchy, while using :NERDTreeFocus simply moves the focus, which I think is what you want)
Pirouette answered 7/5, 2013 at 3:18 Comment(1)
For me in INSERT mode Control-N was always code completion. And it stil is with this mapping.Yalonda
D
24

To toggle, use the following:

map <silent> <C-n> :NERDTreeToggle<CR>

Domineer answered 23/5, 2014 at 14:42 Comment(1)
this solution is very practicalPrimine
G
6

For ex Sublime users:

map <silent> <C-k>b :NERDTreeToggle<CR>
Galaxy answered 5/9, 2016 at 14:8 Comment(0)
S
0

add to your rc file (.bashrc / .zshrc) this shortcut

alias nerd="nvim -c \"NERDTree\""

then, reload your bash width

source ~/.zshrc # or ~/.bashrc according your case

and run

nerd

the -c flag allows you to execute a command when starting the terminal

nvim -c "command"
Spleenwort answered 17/5, 2021 at 19:29 Comment(0)
S
0

.vimrc:

let mapleader = "n" #nerdtree  
nmap <leader> :NERDTreeFocus<cr>
let mapleader = "f" #focus
nmap <leader> :NERDTreeToggle<cr>

.bash_profile:

alias n="vim -c 'NERDTree'"
Stowage answered 27/6, 2023 at 13:6 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Hua

© 2022 - 2024 — McMap. All rights reserved.