Vim ":source %" command results in Error (E499) - vim

Hello fellow Stack Overflow vim users,
I have recently found myself spending a considerable amount of time in my school's Computer Science lab writing code. I've configured vim on countless linux systems and have never encountered this error:
E499: Empty file name for '%' or '#', only works with ":p:h"
This error happens anytime I use % in a command in vim. I've never seen this before, but I will give as much info as I can. Here is my .vimrc:
set nocompatible
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'VundleVim/Vundle.vim'
"Plugin 'scrooloose/nerdtree'
"Plugin 'bling/vim-airline'
"Plugin 'altercation/vim-colors-solarized'
call vundle#end()
filetype plugin indent on
I've commented out those last few plugins to see if they had any correlation with the error, but that didn't seem to help. I used Git to install vim as usual:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
All I did after that was run :PluginInstall to install the plugins, and then tried to use :source %, resulting in the error.
This has really stumped me, and I hope to solve it, as I'd like to use my customized vim setup for this CS class, but I can deal with stock ViM or even Vi, or if it comes to it... nano.
EDIT: Using Ubuntu 15.04

This seems to be related to empty %. You can do two things: pass the filename when invoking vim: vim myscript.vim and then calling :source %, or passing the filename to :source (:source myscript.vim).
EDIT
If you issue :help registers, there is listed that % is a read-only register containing the current filename. So, if you are editing a buffer that doesn't have a filename (not saved), this register is empty and so you get error message.

Related

Vim: command of a plugin are not recognized with minimal_vimrc

I am debugging a plugin and start vim with vim --servername VIM -u minimal.vim minimal.tex. The plugin is loaded automatically and the plugin's name is in the output of :scriptnames, :h local-additions and :set runtimepath? commands, but no command defined by the plugin works: E492: Not an editor command:. Issuing a :packadd <packagename> does not change the situation.
What am I doing wrong? What do I need to do to load that plugin? How to use a plugin with as pure vim config as possible?
The minimal vimrc:
set nocompatible
let &runtimepath = '~/.vim/bundle/vimtex,' . &runtimepath
let &runtimepath .= ',~/.vim/bundle/vimtex/after'
filetype plugin indent on
syntax enable
Update: When I looked a little deeper into it next day, I realized that other plugins are working. Since it reduces the issue from a general vim debug question to a plugin specific one, I would contact the creator of the plugin. I will post the solution when I will have one. (The plugin in question is vimtex)

How to turn on syntax highlighting in VIM 7.3 OSX

System = OSX 10.9.4
I am trying to turn on syntax highlighting in vim while using the terminal. However, I am unable to get it to work properly.
Things I've tried:
located the vimrc file and added the following code:
set ai " auto indenting
set history=100 " keep 100 lines of history
set ruler " show the cursor position
syntax on " syntax highlighting
set hlsearch " highlight the last searched term
filetype plugin on " use the file type plugins
Located vimrc under directory:
cd /usr/share/vim/
The interesting thing is that once I add the code to the vimrc using vim, followed by exiting (x), and re-opening the file again, syntax is correctly highlighted in the vimrc.
However, when I try to make a new vim file called "test", copy the same code, save and exit. Re-open it, the syntax is not highlighted at all.
It appears that syntax highlighting only works when I open the actually vimrc file---and not when I try to create a new one or open another file that should have syntax highlighting.
I've also tried to create a .vimrc (exact copy) under the ~/ (directory). No success.
Made a new file called "test" and tried turning it on while active as well:
vim test
"then within vim"
:syntax on
I am really confused as to why this partially works.
Any help is much appreciated.
Cheers!
p.s. I have followed these instructions as well from: http://geekology.co.za/article/2009/03/how-to-enable-syntax-highlighting-and-other-options-in-vim
*I am aware of macvim, but would like a solution for the native vim in terminal. Thanks.
NEVER do anything in $VIM as it will work only by luck, cause unexpected behaviors and likely be overwritten next time Vim is updated.
What you have put in /usr/share/vim/vimrc should be in ~/.vimrc.
filetype on and syntax on are the bare minimum you need in your ~/.vimrc for syntax highlighting to work.
$ vim ~/.vimrc gives you syntax highlighting because the file is recognized by Vim as a vim file. Filetype detection is mostly dependent on file extensions so you can't expect any syntax highlighting in a file called test.
$ vim test.py, on the other hand, should give you syntax highlighting.
If the filetype is not detected, you can force it with :set filetype=python.
You most probably want to enable indentation along with syntax highlighting, so add these to lines to ~/.vimrc
filetype plugin indent on
syntax on
Steps with screenshots can be found here
http://osxandiosdaily.com/how-to-enable-vim-syntax-highlighting-on-mac-osx/
Inside of your file, enter command mode and enter the command
:syntax on

vimrc: checking &ft doesn't work unless "filetype detect" ran 1st

I'm running OS X 10.9. I downloaded the Python 3.3.3 .dmg file and installed it. I download the vim code (not macvim) via google code (mercurial) and built it with the following commands:
./configure --enable-python3interp --with-python3-config-dir=/Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3
make VIMRUNTIMEDIR=/usr/local/share/vim/vim74
sudo make install
Everything seems to work normally. I mention all this to try to cover my basis in case I've caused my own issue.
What I'm trying to figure out is why when I check the file type with "if &ft == 'python'" it only returns true if I run "filetype detect" 1st. I am not running "filetype on" or any other filetype command. I know Vim correctly recognizes my .py files without the "filetype detect" because I can run ":echo &ft" and it returns "python".
Is this normal behavior or did I screw something up in the compile? What makes me think this might not be normal is that every example I can find from example .vimrc files I don't see anyone else running "filetype detect".
Thanks.
Edit: I've tried this with "filetype on" before the if statement, without the "filetype detect", but this doesn't seem to work either.
From :help filetype-detect:
...
When filetype detection was off, it will be enabled first, like the "on"
argument was used.
So either of these sequences should have the effect of setting 'filetype' to "python":
:filetype on
:e foo.py
:e foo.py
:filetype detect
My preference is to add :filetype on to my vimrc file, either directly or with something like
:runtime vimrc_example.vim
Beyond the initial conditional detection of the filetype issue, I also ran into the condition failing when opening a new tab or split. Whatever is within the condition is applied to new buffers regardless of file type when I check with the following:
filetype detect
if &ft == 'python'
....
endif
The following however is checked correctly with each new buffer load:
au BufRead *.py [arguments]
as in
au BufRead *.py let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
I hope this helps others.

ctags not working as expected with Vim plus general setup problems (C programming)

I have installed cvim and NodeTree plugins and generated an exuberant ctags file for my build tree.
This is what my ~/.vim/.vimrc file looks like:
:noremap :TlistToggle
:let Tlist_Show_One_File = 1
:let Tlist_Exit_OnlyWindow = 1
:let Tlist_Use_Right_Window = 1
set tags=./tags;/
set number
set tabstop=4
set incsearch
When I start editing a file, I notice that Ctrl ] does not work and I have to resort to typing ta: funcname - which gets tiring after a while. Interestingly enough, Ctrl T pops me off the tag stack as expected - I don't understand whats going on - how do I fix this?
Incidentally, vim (appears to) completely ignores the contents of my .vimrc file and I always have to type the same commands in the editor, so as to get the settings I want - very annoying.
Last but not the least, I used to be able to type :make in the editor window, drop to the console and then have the build results displayed in a little window which I can then go to and select a line (with an error or warning say), and then have the editor automagically take me to the offending line - unfortunately, I don't remember the plugin (or commands) I used to allow me to build from within vim.
So, how do I:
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
Fix my .vimrc file so that contents are actually applied to my vim session.
Find the appropriate plugin to install to allow builds (using make) from within vim
You're asking about a weird mix of problems.
Fix my vim setup so that I can move to definitions/declarations using Ctrl-]
The tags functionality is working; I suspect that you have a mapping blocking Ctrl-]. Try
:verbose nmap <C-]>
and
:nunmap <C-]>
Fix my .vimrc file so that contents are actually applied to my vim session.
:echo $MYVIMRC
will tell you the location of the .vimrc that Vim uses. Also, check the output of :scriptnames which scripts get loaded, and read :help vimrc to understand the logic Vim applies.
Find the appropriate plugin to install to allow builds (using make) from within vim
That's built into Vim. With the appropriate 'makeprg' set (it defaults to make), you can run :make. Vim parses the output (through the 'errorformat' option), and you can open the quickfix list via :copen.
Your vimrc is:
~/.vim/.vimrc
If you run Vim 7.4, it should be:
~/.vim/vimrc
or
~/.vimrc
If you run Vim 7.3 or older, it should be:
~/.vimrc
And... what Ingo said.

MacVim: slimv does not start

I'm using MacVim (kind of gvim for OSX) and try to get the slimv plugin running.
Sadly it's not working out of the box. In fact, it does not start up at all.
My setup:
MacVim (32bit cause of this) (vim 7.3)
:scriptnames does not list ftplugin/slimv.vim while plugin/paredit.vim is listed
:set ft? shows filetype=lisp for .lisp files
:messages shows no errors
:filetype filetype detection:ON plugin:ON indent:ON
:echo g:paredit_loaded 1
:echo g:slimv_loaded E121: Undefined variable: g:slimv_loaded \ E15: Invalid expression: g:slimv_loaded
compiled with +python (2.7)
SBCL and slime are installed - works flawless with emacs.
I tried it with and without let g:slimv_swank_cmd = ... in .vimrc and changed the line recommended on the plugin page from
let g:slimv_swank_cmd = '!osascript -e "tell application \"Terminal\" to do script \"sbcl --load ~/.vim/slime/start-swank.lisp\""'
to
let g:slimv_swank_cmd = '!sh -c "sbcl --load /Applications/MacVim.app/Contents/Resources/vim/runtime/slime/start-swank.lisp" &'
since the osascript was not working and I don't know how to fix it. But a similar call to xterm is sufficient for Linux so my sh call should be fine.
Well, I got no idea what to try next. :/
The problem got solved by installing slimv to ~/.vim instead of the vim ebedded in MacVim. Maybe some kind of bug? However, Common Lisp + vim - I just love it.
Because moving the slimv plugin to ~/.vim fixed it, I suspect the problem is that MacVim's default /Applications/MacVim.app/Contents/Resources/vim/runtime/ftplugin/lisp.vim is being sourced before the ftplugin/lisp/slimv-lisp.vim file provided with slimv.
Both of those files (lisp.vim and slimv-lisp.vim) start with code like this:
" Only do this when not done yet for this buffer
if exists("b:did_ftplugin")
finish
endif
" Don't load another plugin for this buffer
let b:did_ftplugin = 1
and so whichever file gets sourced first will prevent the other file from running since vim does finish (exits the script) if it detects that another *lisp*.vim script was run previously and therefore set the b:did_ftplugin buffer-local variable.
You can tell this is happening by running MacVim from the command line with the arguments:
-V20macvim-log.txt hello.lisp
Then quit the MacVim session that starts up, and look at the macvim-log.txt file it created.
Search for b:did_ftplugin and you'll see it referenced each time lisp.vim or slimv-lisp.vim runs, and you can see that lisp.vim runs first, which prevents slimv-lisp.vim from working.
Moving your slimv install from the /Applications/MacVim.app/ dir to your ~/.vim dir will change the order so that slimv-lisp.vim is sourced before lisp.vim, and then slimv will work.
If slimv.vim is not listed in :scriptnames and g:slimv_loaded is undefined then you don't have the plugin loaded at all. I guess you don't have filetype plugins enabled. Paredit is a general plugin but slimv.vim is a filetype plugin and filetype/indent plugins must explicitly be enabled. Try to add these lines to your .vimrc:
filetype plugin on
filetype indent on
The problem got solved by installing slimv to ~/.vim instead of the vim ebedded in MacVim. Maybe some kind of bug?
However, Common Lisp + vim - I just love it.

Resources