How can I debug issues in VimL? - debugging

I'd like to know more about debugging in vim. What features does vim have that can help me to diagnose a problem I might have?
I'd basically like to know:
How can I diagnose a problem with my .vimrc and other configuration files?
What are some strategies to debugging a script in VimL?

How can I diagnose a problem with .vimrc and other configuration files?
If you're having some unexpected behavior in vim and you're not sure where the
problem is originating, there are a few approaches to honing in on the source
of the issue. One of the best first steps is to find out if your problem is
normal vim behavior, caused by a plugin or caused by your .vimrc.
If your vim instance is setting a particular 'option' and you're not sure
where it is being set. You can use the :verbose command to find out. For
instance
:verbose set nocompatible?
nocompatible
Last set from ~/.vimrc
To run an instance of vim without any plugins or configuration files run
vim -N -u NONE
I set this as to alias called cleanvim in my .bashrc file. The -u NONE is
what's doing the magic here. The -N simply puts vim into nocompatible mode,
which generally is desired. You can also use the option NORC to only exclude
your vimrc. Note that if you use something like pathogen or vundle to
instantiate your plugins from within your vimrc, then your plugins will also
not load properly.
If you are using a plugin manager like pathogen or vundle then excluding your
plugins is simple; just comment out the line in your .vimrc that calls
pathogen or vundle. However if you have other plugins loaded from your .vim
directory you can exclude them with the --noplugin flag.
If your problem is being caused by a plugin, try adding back plugins one by one
to determine which one is causing the issue. From there you can either report
the bug to the plugin's maintainer or try to diagnose the problem yourself
using the tips from the rest of this answer.
If your problem is caused by your .vimrc there are some ways to hone in on
the problem further. Once simple method is to add the finish command at some
point in your .vimrc. Once this command is encountered the script will stop
being sourced and no commands after it will be executed. In this way you can
exclude large portions of your .vimrc and try to find out the general region
where the problem is coming from.
What are some strategies to debugging a script in VimL?
Vim has a help section on this topic at :h debug-scripts. This describes
vim's debug mode in detail, which will allow you to set breakpoints and step
through a sourced file or user function. You can add a breakpoint on a specific
function or a specific line in a file. For instance...
" set a breakpoint on the function MyCoolFunc
:breakadd func MyCoolFunc
" set a breakpoint on line 43 of your .vimrc
:breakadd file 43 .vimrc
" set a breakpoint at this location
:breakadd here
After you set a breakpoint you can source the file again to begin debug mode at
that line. If you'd like to use debug mode on an entire file start vim with the
-D flag. You could also run debug mode on a particular command. For example,
say you're having trouble with a particular command :MyCommand. You can start
debugging mode on this command with :debug MyCommand.
Once debug mode has been started you can use the usual set of vim commands.
This is useful because you can now check the value of variables using the
echo command to try and diagnose an issue. You can also use the verbose
option to provide extra information about the following lines. See :h 'verbose'
for its options.

The scriptease is very helpful to debug vimL:
provide commands for easier insertion of Vim breakpoints
:Runtime allows easy reload of plugins, even unleting include guards
:Disarm {file}: attempt to disable a runtime file by removing its mappings, commands and autocmds
:Time {command}: profilling
:Verbose {command}: like :verbose, but capture the results to a file and load it in the preview window
For cases where you need to restart Vim several times (e.g.: incremental removal of plugins) the :RestartVim command of session plugin can be useful.

For vimL I find those 2 plugins awesome:
BreakPts
Set/View Vim breakpoints and browse functions visually
Decho
Better echo functionality suitable for debugging scripts
I also use this to quickly source the selected vimL lines:
fu! SourceRange() range
let tmpsofile = tempname()
call writefile(getline(a:firstline, a:lastline), l:tmpsofile)
execute "source " . l:tmpsofile
call delete(l:tmpsofile)
let n = a:lastline - a:firstline + 1
echo 'Sourced ' . n . ' line' . (n > 1 ? 's' : '')
endf
com! -range Source <line1>,<line2>call SourceRange()
nn gs m`:Source<cr>``
vn gs m`:Source<cr>``

Related

Vim Vundle broken, can't open file

My vim setup apparently has broken out of nothing. It now spills errors for every single plugin I have configured. This started happening after I have changed some appearance settings, some syntastic features (both of which I don't think are the cause) and changed my shell to fish (this MAYBE is the cause).
Using the directive set shell=/usr/bin/fish or set shell=fish does not change anything, it still fails, for which I tried after reading this question.
The errors happen for both :BundleInstall and :BundleUpdate. My full vimrc file is available here if there is need to read it, it's not long.
Yes, setting your shell to fish is likely to be the root of your problem. The fish shell doesn't support the standard UNIX syntax for file redirections, which breaks Vim's system(). Just set Vim's shell to sh:
set shell=/bin/sh
You can't use the interactive features of fish from Vim anyway.
A while back I wrote up some docs for this on the Vundle wiki that you may find useful. In short, you can either:
Run: env SHELL=(which sh) vim +BundleInstall! +BundleClean +qall
Add set shell=sh to your .vimrc
As an aside, my Tackle project has an Up plugin that includes a handy way to update your vim plugins via Vundle.

Vim is taking 700MB memory on WIndows, how can I debug it?

I have Vim on Windows server 2012, and when I start it from the start menu everything works fine. However, when I start it from the command line it take 5 seconds and uses 700MB memory. Even when its not opening a file. There is something weird going on, and I was wondering if there are any ways to debug it/figure out what is causing this? Thanks, Eric.
EDIT:
Here is the result of vim --startuptime outputfile (abbreviated):
times in msec
clock self+sourced self: sourced script
clock elapsed: other lines
000.000 000.000: --- VIM STARTING ---
...
016.000 016.000: parsing arguments
016.000 000.000: expanding arguments
4794.000 4778.000: shell init
4794.000 000.000: Termcap init
...
4825.000 000.000: setting raw mode
8768.000 3943.000: start termcap
8768.000 000.000: clearing screen
8783.000 000.000: --- VIM STARTED ---
You could use Sysinternals' Process Explorer to check if any of the processes are starting child processes or if there is any difference in the environmental variables.
Also, Sysinternals' Procmon would allow you to check what registry entries, files, etc does any application use (filter by command name includes vim), but probably you will find the differences just with Process Explorer.
Sysinternals was a company that created some nice apps for Windows and Microsoft bought it some years ago. You can access the last version of any of their apps on http://live.sysinternals.com
Excerpt from Vim FAQ 2.5:
2.5. I have a "xyz" (some) problem with Vim. How do I determine it is a
problem with my setup or with Vim? / Have I found a bug in Vim?
First, you need to find out, whether the error is in the actual runtime
files or any plugin that is distributed with Vim or whether it is a
simple side effect of any configuration option from your .vimrc or
.gvimrc. So first, start vim like this:
vim -u NONE -U NONE -N -i NONE
this starts Vim in nocompatible mode (-N), without reading your viminfo
file (-i NONE), without reading any configuration file (-u NONE for not
reading .vimrc file and -U NONE for not reading a .gvimrc file) or even
plugin.
In this invocation, try to reproduce your problem. If the error
persists, the chance is good you've found a bug in Vim (see also
Question 2.6. faq-2.6)
If the error does not occur when starting Vim this way, then the problem
is either related to some plugin of yours or some setting in one of your
local setup files. You need to find out, what triggers the error, you
try starting Vim this way:
vim -u NONE -U NONE -N
If the error occurs, the problem is your .viminfo file. Simply delete
the viminfo file then. If the error does not occur, try:
vim -u ~/.vimrc --noplugin -N -i NONE
This will simply use your .vimrc as configuration file, but not load any
plugins. If the error occurs this time, the error is possibly caused by
some configuration option inside your .vimrc file. Depending on the
length of your vimrc file, it can be quite hard to trace the origin
within that file.
The best way is to add :finish command in the middle of your .vimrc.
Then restart again using the same command line. If the error still
occurs, the bug must be caused because of a setting in the first half of
your .vimrc. If it doesn't happen, the problematic setting must be in
the second half of your .vimrc. So move the :finish command to the
middle of that half, of which you know that triggers the error and move
your way along, until you find the problematic option.
It also mentions how to create a log file:
You can also use the -V command line argument to get more debug
information to analyze the problem:
$ vim -V2logfile
You can increase the value passed to the -V argument to get more debug
information. By also specifying a logfile name, this makes sure, the
debug messages don't appear on the screen and won't disturb you when
trying to reproduce the problem.

pathogen vim indent-guides not showing

I happened to already have pathogen installed and running with JSHint and Janus, which works just fine. However, when I try to use the plug-in vim-indent-guide, it does not show at all.
According to pathogen as well as the github for the vim indent guides, it would seem that the only really necessary step would be to clone the repository into my ~/.vim/bundles. I am aware that the .vim file of the add-on needs to be in a subdirectory called plugins, which I confirmed. My indent-guides are installed into ~/.vim/bundle/vim-indent-guides/plugin/indent_guides.vim. I also am using VIM 7.3 which should mean version is not a problem.
To test my theory that pathogen is working, I made a file that would clearly raise a JSHint error
Given that assumption was proven correct, I tested to see if the files were loading, using :scriptnames in the file I was editing. Sure enough, I see two files there matching the name
104: ~/.vim/bundle/vim-indent-guides/plugin/indent_guides.vim
105: ~/.vim/bundle/vim-indent-guides/autoload/indent_guides.vim
however, I see no visible results of tabs or spacing in any files.
Perhaps it is a display issue or Janus "overwriting" this add-on's settings, but to be honest, I'm stumped. Anyone have any ideas?
I don't have a colorscheme yet and this is what i've added to the .vimrc file after installation:
...
" Indent Guides Settings
let g:indent_guides_enable_on_vim_startup = 1
let g:indent_guides_auto_colors = 0
let g:indent_guides_guide_size = 1
autocmd VimEnter,Colorscheme * :hi IndentGuidesOdd guibg=red ctermbg=3
autocmd VimEnter,Colorscheme * :hi IndentGuidesEven guibg=green ctermbg=4
colorscheme default
...
Good Luck.
Check your $TERM variable by echo $TERM in bash. If TERM=xterm instead of TERM=xterm-256color, it only handle 8 colors (instead of 256) and the colors will not be displayed properly.
You may fix to the correct value by this line in ~/.profile:
export TERM=xterm-256color
By doing so, re-enter vim and type :set t_Co should show t_Co=256, which is correct.

How to set my GVim to be feel like IDE?

Currently, I'm using GEdit as my text editor for editing Ruby and Javascript source codes. I would like to give GVim a try to be my editor choice. I have tried to follow https://github.com/akitaonrails/vimfiles and few others instructions, but I don't get any luck, when I source ~/.vimrc, then I always get:
bash: /home/samnang/.vimrc: line 5: syntax error near unexpected token ('
bash: /home/samnang/.vimrc: line 5:call pathogen#runtime_append_all_bundles()'
Could you point me somewhere to get the instruction or configuration?
Environment: Ubuntu 10.10
Edit: If I don't source it, when I type vim or gvim, then I got:
Vim: Caught deadly signal SEGV
Vim: Finished.
Segmentation fault
You need to run source ~/.vimrc as an Ex mode command. That is, inside Vim itself, hit : and enter the command:
:source ~/.vimrc
Right now, you're running Bash's source command, which is entirely not what you want to do.
~/.vimrc is the configuration file for vim, and will automatically be read by vim when it launches in order to know how you want it set up. You can add your non-graphical vim commands, such as key mappings, abbreviations to ~/.vimrc/. In ~/.gvimrc you can add commands to set your colorscheme, the default number of columns and rows displayed at startup, etc.
If you aren't familiar with vim itself type vimtutor at the command-line and go through the tutorial.
To start gvim, type gvim at the command-line. To edit a file you can either open gvim, then use :e file/to/load in comman-mode, or do gvim file/to/load at the command-line. gvim supports multiple windows and tabs as does vim, so study those things to make the most use of them.
You can also try integrating Vim with eclipse if you want to bring IDE functionality to Vim (like projects, error highlighting, code completion, etc).
If you're interested check out the eclim project. It supports most of the modern languages, including Ruby, and I highly recommend it.
Try adding a .vim or vimfiles in your $HOME directory. If it still fails, add a file to the .vim directory. I did a
cd
mkdir .vim
cd .vim
touch .netrwhist
chmod g+w .netrwhist
I discovered this while learning about building your own syntax files at vim wikia creating your own syntax files

How can I invoke VIM with C-X e for long, complex, tricky commands?

I found an awesome tip here. You can "[r]apidly invoke an editor to write a long, complex, or tricky command". However, when I press the key combination above, I get Emacs open. I would like to switch it to Vim. How can I invoke Vim with C-X e?
[1. Problem SOLVED by Brian Cambell]
export EDITOR=vim
Add to your .bashrc or appropriate shell rc file
[2. Problem SOLVED thanks to Pax]
I was unable to get the tricky command back to Bash. The errors were:
> Error detected while processing BufRead Auto commands for "*":
> E117: Unknown function: JumpToLastPosition
Quotes in .vimrc solved the second problem. I am still unsure about the part in my .vimrc:
" augroup misc
" autocmd!
" autocmd BufReadPost * call JumpToLastPosition()
" autocmd FileChangedShell * call WarningMsg("File changed outside of vim")
" augroup end
[3. Problem]
What do the above part in .vimrc do?
On most Linux installs (all the ones I tested), bash recognizes both the Emacs and Vi command history keys (or you can use "set -o vi" to force it).
So, you can just use the vi-mode "<ESC>v" to to enter visual mode, this will start editing in a Vim session.
To run the command, you just save and exit from Vim ("ZZ" or ":wq"). To cancel the command, you need to delete the contents, save and exit ("1GdGZZ").
In addition to running it by exiting, you can also save it while in the editor to another location (":w /tmp/myscript").
Keep in mind that visual mode will work with the currently selected line so you don't have to start with a blank command ("<ESC>v"). You can use the normal vi-mode tools to select a line from the history first and then enter visual mode ("<ESC>kv" for last command, "<ESC>/grep<ENTER>nnv" for third-last grep command and so on).
Using this method has the advantage of not changing the "EDITOR" variable which may be used for other things (unless you want Vim for everything, which is a very sensible position to take IMNSHO).
Update:
Regarding your error, posted after the question:
JumpToLastPosition() is the function called by Vim for all files to put the cursor where it was when you last edited the file. I'm going to assume you're actually getting this error when the editing starts, not when you exit, since this is the auto function following a buffer read.
Can you start a "normal" vim session ("vim xx.txt" and then "vim xx") without this error occurring? You may find you get the same problem (and possibly only on the last one).
If you do have the same problem, you need to look at your startup files. It's possible the autocmd for BufRead is broken somehow. Have a look inside your vimrc and you filetype.vim files to see where that function is called and/or defined (I suspect it's called but not defined and that may be a mismatch between the two files or one of them has been damaged).
export EDITOR=vim
Add to your .bashrc or appropriate shell rc file
The link that you provided contains the answer:
Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR.
You need to set the EDITOR environment variable (like #Brian Campbell)

Resources