Only load a plugin if it 'works' - vim

I have vim installed on two machines -- one with python3.7 and one with python3.4 (on an old server that won't have 3.7 without some work). One of the plugins I have:
call plug#begin('~/.vim/plugged')
"Plug 'SirVer/ultisnips'
" ... other plugins
call plug#end()
Literally causes an error on every single keypress on insert mode (and the stacktrace looks like a python error). The plugin itself seems to load fine, but in Insert mode it makes vim unusable, here is a ten second video to show: https://gyazo.com/fafb22850f4b1c8c142c0a71a40d698a.
I'm not sure if I'll be able to figure out how to fix this or if perhaps it just won't work with python3.4. Either way, is there a way where I can only load this plugin if there are no issues? For example, something like, in pseudocode:
if "plugin works in insert mode"
Plug 'SirVer/ultisnips'
endif
How could this be done?
Update: my current solution is as follows, but it does depend on the existence of python (though, to be noted, I haven't been able to get Plug to work at all without python, so I think this is a safe assumption) --
" UtiliSnips to add code-snippets: https://github.com/SirVer/ultisnips
" For whatever reason, not working/compiling with python version 3.4
if has('python3')
pyx vim.command('let python_version="%s"' % sys.version[:3])
if python_version == '3.7'
Plug 'SirVer/ultisnips'
Plug 'honza/vim-snippets'
endif
endif

There is something strange here. You mix python3 with pyx. pyx is meant to be portable to Python2 & 3. python3 says: "if true, from now on, python2 is no longer available!"
Which means, you have the choice here. If you absolutely wish to bind your current Vim to a python3 interpreter, well... assume it: pythonx related features no longer make any sense. You're in your .vimrc, not in a plugin somebody else may use with a Python2 interpreter. Keep it simple.
if has('python3') && (py3eval('sys.version_info[1]') >= 7)
" no need for the convoluted <<python3 vim.command>>, pyXeval FTW!
Plug ......
endif

Related

Vim Latex Suite can't find latex command using Pathogen

Background
I was having dual boot issues with Windows 10 and Ubuntu 17.10, which eventually resulted in a boot loop and since I need Ubuntu 16.04.3 for an Operating Systems class I decided to load up an ISO into one of my USBs with Rufus, reformat my Windows partitions from GPT to NTFS to get around some nasty bootmg/efi issues and here we are, good as new, except for one thing...
Problem
The only thing I haven't been able to successfully reinstall has been Latex Suite for Vim. Installation last time had been an easy extraction into .vim, but after some deliberation I elected to use Pathogen.
I've installed Pathogen using
mkdir -p ~/.vim/autoload ~/.vim/bundle && \
curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim
Which works for color schemes, etc., and every other aspect of latex-suite save for compiling, generating this every time I try to compile (using \ll):
/bin/bash: latex: command not found
I installed texlive which does then allow me to compile using vim, but I'd prefer not to use this solution if I don't have to (in fact I've already removed it, its more of a plan b for now given it's obstructive and non-intuitive compilation errors). I looked at the other questions asking how to install Latex Suite through pathogen and didn't see anything else similar to what I'm working on; below is self-explanatory in regards to installation structure.
~/.vim/bundle$ ls
vim-archery vim-latex-1.10.0
Obvious running pdflatex and latex can't be found because they are not in my $PATH but I was under the impression that with a good, recommended package manager like Pathogen I wouldn't need to which is what originally raised the red flag for me as its whole job is to modify the runtime path. I thought maybe trying sudo vim text.tex might work in case there hadn't been adequate permissions for Pathogen to modify runtime path but no dice.
.vimrc
execute pathogen#infect()
syntax on
filetype plugin indent on
colorscheme archery
let g:tex_flavor='latex'
set sw=2

How to ensure plugin manager is installed

I would like to check at the top of my .vimrc whether vim-plug is installed and act accordingly so whenever I install my dotfiles, I don't get errors after running vim for the first time.
If there is no vim-plug, then wget should download it from github and install plugins.
So far I have this code snippet
if empty(glob("~/.vim/autoload/plug.vim"))
execute '!mkdir ~/.vim/autoload && wget -O ~/.vim/autoload/plug.vim https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
endif
call plug#begin('~/.vim/plugged')
" plugins...
call plug#end()
But this just makes sure vim-plug is present in the plugin directory.
How can I load this plugin after download and issue PlugInstall after call plug#end()?
Thank you for help!
Edit
I partially managed to solve the rpoblem by adding
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
Unfortunately some errors still occur.
To check whether plugin loaded or not you should deal with its implemented features. vim-plug provides several commands and functions so you can do exists("*plug#begin") or exists(":PlugInstall"). Since you call PlugInstall I would recommend the last one. For more information type help exists(.
Friendly speaking, the goal is very weird since there can be no internet connection or plugin destination can be changed by the maintainer. Nevertheless, if you prefer this behavior here are some tips:
Use -p flag for mkdir. Command will not fail if there is already autoload directory.
Use vim system() function to call the shell command; then check v:shell_error for issues I mentioned above. It should be 0 if no errors happen.

Is BundleInstall (for Vundle) required every time Vim is started?

I'm using Vim/Cream on Win7 with the Vim binaries provided with Cream (v7.3.107), and have installed Vundle, and the plugins work fine after I run :BundleInstall. But after exiting and restarting, the plugins don't work until I run BundleInstall again. Is this normal? I thought BundleInstall was a one-time command (excepting when used for updates). Here's an excerpt of what I have in my vimrc (actually cream-user.vim, which is what Cream prefers):
set nocompatible
filetype off
set runtimepath+=$HOME/vimfiles/bundle/vundle/
call vundle#rc()
Bundle 'gmarik/vundle'
" Your bundles go here:
"
" ORIGINAL REPOS ON GITHUB
Bundle '907th/vim-auto-save'
.
However, when I run :AutoSaveToggle (a vim-auto-save command), I get the following error:
E492: Not an editor command: AutoSaveToggle
.
The cream-user.vim file is being invoked (it's listed in scriptnames):
83: C:\Users\<MyUserName>\.cream\cream-user.vim
[...]
85: C:\Users\<MyUserName>\.vim\bundle\vundle\autoload\vundle.vim
86: C:\Users\<MyUserName>\.vim\bundle\vundle\autoload\vundle\config.vim
.
...and Vundle itself is installed, as its commands work, notably :BundleList, which lists vim-auto-save among the installed bundles:
" My Bundles
Bundle 'gmarik/vundle'
Bundle '907th/vim-auto-save'
After I re-run BundleInstall, the plugins start working, and I do notice scriptnames now includes the extra files:
125: C:\Users\<MyUserName>\.vim\bundle\vundle\autoload\vundle\installer.vim
126: C:\Users\<MyUserName>\.vim\bundle\vundle\autoload\vundle\scripts.vim
127: C:\Users\<MyUserName>\.vim\bundle\vim-auto-save\plugin\AutoSave.vim
Putting :BundleInstall in the .vimrc doesn't look like the right move either, as it opens a buffer (can be worked around, but still...). I'm a brand new to Vim so I'm not sure what's wrong, probably something simple on my end, like my not being clear on how to appropriately invoke vundle. Any ideas on how to fix this?
First: You're right, :BundleInstall is not required on every launch. I guess the behavior you're seeing has to do with the Cream customizations; it probably messes with 'runtimepath' itself, and therefore interferes with Vundle.
If you just chose Cream for an easy install of Vim (though your 7.3.107 is quite dated already), there's also a "Vim-only" installer. Also, a Vim 7.4 installer is available from http://www.vim.org/download.php.
In case you do want Cream (wouldn't recommend that; especially if you're into programming / customizing Vim with plugins), I'd open an issue with the Vundle project, asking for help / support of Cream.
I had a similar problem with MacVim. The problem for me was that I didn't follow the instructions closely enough. I just assumed that I could just put the Vundle lines in my .gvimrc. This was WRONG! The solution, as per the instructions, was to place the lines in the .vimrc file. The order of operations matters, and it's possible this matters for Cream.
I found a solution to this problem in this bug report: https://github.com/gmarik/Vundle.vim/issues/430
At the end of your cream-user.vim file, add:
call vundle#config#require(g:bundles)
This causes the plugins to be loaded without showing the Vundle\Installer buffer at startup.

Sparkup doesn't work in vim for me

I installed sparkup vim plugin, i'm sure it's in the right place. I use archlinux.
And my vimrc: http://wklej.org/id/504484/
Sparkup just doesn't work at all.
I don't know what to do.
Ok, i found out what was the problem.
First it was python version which needed to be change
#!/usr/bin/env python to python2
in sparkup.py file
Second, I needed to add
filetype plugin on
To .vimrc file.
Sparkup needs vim to be compiled with Python 2 -- you can check using :python print 42 whether it is.
Isn't Arch shipped with Python 3 by default anyway? I suspect that's the problem.

How can I format JS code in Vim?

I have this bit of JavaScript...
15 $('.ajax_edit_address').each(function() {
16 $(this).ajaxForm({
17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
18 success: function(response) {
19 $('input, select, textarea', '.ajax_edit_address').removeClass('updating');
20 }
21 });
22 });
That's formatted the way I like it. But let's say I had just finished typing something and I wanted to tidy it up. So I run the Vim code formatter on it...
=7j
The result is...
15 $('.ajax_edit_address').each(function() {
16 $(this).ajaxForm({
17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
18 success: function(response) {
19 $('input, select, textarea', '.ajax_edit_address').removeClass('updating');
20 }
21 });
22 });
Vim seems to have trouble with functions as method arguments.
Here is what I think is the relevant part of my .vimrc...
:set cindent shiftwidth=2
" indent depends on filetype
:filetype indent on
:filetype plugin on
Is there something else that needs to be installed or configured to format JS code?
VIM plugin Jsbeautify could handle jQuery correctly. It's the vim plugin version of the online Jsbeautify.
There is a far simpler solution that requires no vim plugins.
Install js-beautify to your system python:
pip install jsbeautifier
Then add this to your .vimrc:
autocmd FileType javascript setlocal equalprg=js-beautify\ --stdin
That's it.
Run :help equalprg to see why this works.
If you've got js-beautify installed (it's available for Python: pip install jsbeautifier, or Node: npm -g install js-beautify) then you can just run it directly from vim - to reformat the current file:
:%!js-beautify
I'd recommend the CLI version of einars/jsbeautify, which you can find here: https://github.com/einars/js-beautify.
It's the offline version of www.jsbeautifier.org.
Use this plugin https://github.com/Chiel92/vim-autoformat to run the formatter on your current buffer with one button press.
The biggest issue seems to be the cindent doesn't recognize this type of syntax:
test({
var b = 2;
});
It will turn it into this:
test({
var b = 2;
});
If you handle that case I'd imagine the indent wouldn't be so awful for the jQuery syntax. But this would require you writing a custom javascript indent file. Also, you'd have to edit the html indent file to not use cindent for script tags with javascript content.
I don't think anyone has successfully created a jquery/prototype compatible indent file for javascript. The existing javascript indent scripts are all flawed.
Unfortunately, 'cindent' just isn't going to do the job since it's is very much tied to C syntax. Since all the default indent script for javascript does is turn on 'cindent', that's not much help. It even says so in the script!
" Maintainer: None! Wanna improve this?
I don't do anything other than really basic javascript so I've never bothered trying to find anything better. From a quick look on vim.org, this script looks like it may be worth a shot. It's newer, so it probably takes into account the more complex javascript that's used now days.
Another alternative that do not need to configure anything inside vim is to run the format command manually at save like:
:w !js-beautify --stdin >%
After saving in this way the vim editor will ask you to reload the current file content:
W12: Warning: File "src/static/js/main.js" has changed and the buffer was changed in Vim as well
See ":help W12" for more info.
[O]K, (L)oad File:
This works like the :w sudo tee % command used to save a file that you modified without privilege.
The command uses the standard input(STDIN) and write it to a variable file descriptor % used as source of current file.
PS: of course you need to install the js-beautify.
pip install jsbeautifier

Resources