Unknown function: NERDTreeAddKeyMap - vim

I'm trying to add a keymap to my NERDTree configuration which executes the current FileNode and displays the output. I put this script file in my ~/.vim/plugin directory.
The keymap actually works when I source the script file from within Vim, but displays the error "Unknown function: NERDTreeAddKeyMap" when starting Vim.
I used Pathogen for my plugins, and in my ~/.vimrc I have all the necessary :
" Enable filetype plugins
filetype plugin indent on
" Start Pathogen plugin to load bundle
call pathogen#infect()
call pathogen#helptags()
As well as NERDTree in my ~/.vim/bundle directory.
Where should I move the script so that it is automatically loaded on Vim startup, without this ugly error ?

The filetype plugin indent on line is supposed to come after the two Pathogen lines.
The relevant documentation says:
This code should sit in a file like ~/.vim/nerdtree_plugin/mymapping.vim.
So, since you use Pathogen, the right place is probably:
~/.vim/bundle/[nerdtree directory]/nerdtree_plugin/mymapping.vim

Related

Are plugins loaded by vimrc or afterwards?

I'm confused about the order in which Vim loads plugin files, and seem to be finding mixed answers online. Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on. Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded, what causes each to load, and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?
Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on.
All plugins are sourced (the correct term) after your vimrc unless you source them manually. The filetype plugin indent on line doesn't change anything to that order.
Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded,
Assuming you have filetype plugin indent on in your vimrc:
The system vimrc if there is one.
Your vimrc.
Built-in plugins.
Your plugins.
Built-in filetype-specific plugins.
Stuff in the after/ directory.
The whole thing is explained in :help startup and can be seen very clearly with :scriptnames.
what causes each to load,
The value of &runtimepath in general and the :filetype command for filetype-specific stuff.
and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?
:source $MYVIMRC re-executes each command in your vimrc.
Most plugins are written in a way that prevent them from being sourced twice. Read their documentation/code if you want to reset them.
:help :filetype.
.vimrc is executed before loading plugins:
At startup, Vim checks environment variables and files and sets values
accordingly. Vim proceeds in this order:
(...)
Execute Ex commands, from environment variables and/or files
An environment variable is read as one Ex command line, where multiple
commands must be separated with '|' or "".
vimrc exrc
A file that contains initialization commands is called a "vimrc" file.
Each line in a vimrc file is executed as an Ex command line.
(...)
Load the plugin scripts.
Just use :scriptnames to see all the sources files and their order in which they are loaded during startup.

GoLang Highlighting in vim not working

I've seen multiple places(including here) that to add syntax highlighting you have to add certain lines to the .vimrc:
"Stuff for GoLang"
filetype off
filetype plugin indent off
set runtimepath+=$GOROOT/misc/vim
filetype plugin indent on
syntax on
That is what's currently in my .vimrc
Restarted vim, terminal, system, and still no highlighting. Any suggestions?
Okay guys, I go the answer:
$GOROOT needs to be defined or you can simply put the location of your go installation.
Ensure that the corresponding runtime files are actually there.
$GOROOT must be defined; check with :echo $GOROOT
There must a syntax plugin (syntax/go.vim) below $GOROOT/misc/vim. Check with :echo filereadable($GOROOT . '/misc/vim/syntax/go.vim').
After opening a Go file, you can check again via :scriptnames and :syntax list.

Why won't vim recognise a plugin command in the vimrc, but it will recognise it when running?

I've installed the vim-gitgutter plugin with pathogen.
I can type :GitGutterLineHighlightsEnable from inside vim and line highlights are turned on, great.
But I want line highlights to be automatically enabled at startup, so I added the command to my ~/.vimrc. However when I start vim, I get "E492: Not an editor command: GitGutterLineHighlightsEnable". Once vim has started up, I can run the command.
My vimrc looks like this:
execute pathogen#infect()
colorscheme railscasts
.. snip tabs and colors etc ..
GitGutterLineHighlightsEnable
hi GitGutterAddLine guibg=#222F22
hi GitGutterChangeLine guibg=#222239
hi GitGutterDeleteLine guibg=#2F2222
Figured it out.
.vimrc is executed before plugins are loaded. From this related question, I changed the commands to:
autocmd VimEnter * GitGutterLineHighlightsEnable
This executes the command after vim has started up.
Use
let g:gitgutter_highlight_lines = 1
instead of
GitGutterLineHighlightsEnable
As you determined yourself, plugins are processed after the .vimrc.
What you can do if you don't like using a VimEnter autocmd, is put a file in your ~/.vim/after/plugin directory for any commands that should run after plugins are loaded.

Pathogen ignoring ftplugin scripts

I'm trying to use Pathogen to manage Vim plugins. I had a couple of scripts I made in .vim/ftplugins.
I installed Pathogen but now none of the scripts in ftplugins runs.
I tried adding a directory inside .vim/bundle with the scripts but it didn't work (it was .vim/bundle/local/ftplugin/python.vim)
Any idea how can I make Pathogen load the scripts in ftplugin directory?
First lines of my .vimrc:
set nocompatible
syntax on
filetype plugin indent on
"execute pathogen#infect()
Only works with that line commented out.
I am running gvim from a Bash prompt with the filename as first parameter like this:
$ gvim some/path/somefile.py
I expect to see the file with my predefined colorscheme for Python files defined in ~/.vim/ftplugin/python.vim and all the other settings defined in that script.
The ~/.vim/bundle directory is empty.
Pathogen is in ~/.vim/autoload and there is nothing more there.
$ ls ~/.vim/ftplugin/
css.vim html.vim javascript.vim python_pep8.vim python_pyflakes.vim python.vim rst.vim xml.vim
$ ls ~/.vim
autoload bundle colors doc ftdetect ftplugin plugins ScrollColor.vim spell syntax
It was a problem with filetype detection, this is the Pathogen issue.
The work around in my case was simple, use this to enable Pathogen:
set nocompatible
"execute pathogen#infect() " breaks filetype detection
call pathogen#runtime_append_all_bundles()
filetype plugin indent on
syntax on
What I did to find out was to remove my ~/.vim directory and start with a clean one. Adding things one by one and checking the results. I realized it was not detecting the correct filetype (when I opened an empty file detection was ok, but it was not when opening an existing file).
Putting my comment here:
Wondering whether it works if you put :filetype and :syntax calls after :execute? Official README suggest doing just this in the second section: first :execute, second :syntax, third :filetype. Note: DO NOT disable filetype prior to :execute like #Eduan suggested, just don’t enable it until :execute is called:
set nocompatible
execute pathogen#infect()
syntax on
filetype plugin indent on
And, by the way, never use *map.
I think I can see your problem, putting this in an answer instead of a comment for the sake of the example code's clarity.
Try this:
" Set the filetype stuff to off, required for Pathogen
filetype off
filetype plugin indent off
execute pathogen#infect()
" You can now turn it on again
filetype on
filetype plugin indent on
Instead of your current setup.

How to set a different colorscheme for each file type in Vim?

In Vim, I want to use a different colorscheme for each file type.
e.g. I want to use desert256 colorscheme for Python & JavaScript files, and use jellybeans colorscheme for HTML & CSS files.
I've tried putting the following code in my .vimrc, but the colorscheme change happens only when changing buffers for the first time.
i.e. If I open a new Python file, Python's colorscheme is used, and when I open a new CSS *buffer*, indeed the colorscheme changes to CSS's colorscheme. However, Changing back to Python's buffer does not change the colorscheme back.
I've used autocmd WinEnter to try and make this rule happen when changing windows (and buffers), but it doesn't help:
autocmd WinEnter,FileType python,javascript colorscheme desert256
autocmd WinEnter,FileType *,html,css colorscheme jellybeans " This includes default filetype colorscheme.
How can I fix this? In addition, a bonus would be to not change a colorscheme when not needed - i.e. Changing from a Python to a JavaScript buffer won't change the colorscheme to "itself".
EDIT:
If anyone's interested, here is my .vimrc repo in github.com. I'll update it with the solution I find here once given.
I've been looking for the same thing. This inside your .vimrc works reasonably well although not perfect.
autocmd BufEnter * colorscheme default
autocmd BufEnter *.php colorscheme Tomorrow-Night
autocmd BufEnter *.py colorscheme Tomorrow
(Note if you're looking for a good dark color theme Tomorrow-Night looks pretty good. Very similar to theme used on Code Academy.)
What you want are filetype plugins, rather than the autocmds. Run help: ftplugin in vim for more info.
From the vim help page:
A filetype plugin is like a global plugin, except that it sets options and
defines mappings for the current buffer only.
In order to use filetype plugins, first put the line filetype plugin on in your vimrc. Then create the folder ftplugin in your vim folder (on unix it's ~/.vim/, I'm not familiar with windows). Then create a script file for each file type you want to customize. These files must be named a specific way. From the vim help page:
The generic names for the filetype plugins are:
ftplugin/filetype.vim
ftplugin/filetype_name.vim
ftplugin/filetype/name.vim
So, for example, if I wanted to create a script for a python file, I would have three options:
Create a file named python.vim in ftplugin
Create a file named python_whatever.vim in ftplugin
Create a file named whatever.vim in ftplugin/python
This script will then be loaded anytime I open a file that vim recognizes as a python file.
So, in order to accomplish what you want:
Create a file named filetype.vim in the ftplugin directory for every filetype you want.
In each of these files, add the line colorscheme name_of_colorscheme
Add filetype plugin on to your vimrc.
In order to set a default colorscheme, just set it in your vimrc file. If I remember correctly, filetype plugins are loaded after your vimrc.
Edit:
The OP indicated that he had a good reason to avoid using the ftplugin directory. After a bit more diggin, I found this script. It can be placed in the global vimrc and seems intended to solve the same problem as the OP.
I have a hack you may like. It is far from perfect, and it doesn't use a .vimrc, but it works for me. It requires you to type a different command to edit different files. It works using the -c parameter when you call gvim. This argument allows you to run vim commands after loading the file. Add this to your ~/.bashrc ( I guess you are using bash ) :
alias gpy="gvim -c 'colorscheme desert'"
alias gcs="gvim -c 'colorscheme jellybeans'"
Hope this helps
Use BufWinEnter instead of WinEnter, like this:
autocmd BufWinEnter,FileType javascript colorscheme jellybeans

Resources