Can I run current script or a script in ViM? - vim

I am developing a php application using ViM. Is there a shortcut for me to run the currentEditing.php? Alternatively, is there a shortcut for running main.php?

It depends on your platform, certainly, but when I'm developing python I often run the current script just by executing :!%. The colon for a command (obviously), the bang for shell execute, and the percent for the current filename.

You can execute shell commands in ViM using the following syntax:
:!command -options arguments
Therefore, you need to save your file first, and then run whatever command you need for executing php.
I don't know php, so let me give you an example with compiling a C file:
... editing text
:w main.c # save to file
:!gcc -Wall main.c # compile the code
:!./a.out # execute the executable
Note that :! commands are run by shell and ViM has no understanding of it. Therefore, you can execute any command. This also means that the command cannot run on a modified, unsaved buffer.

Related

Vim makeprg make environment variable expansion

This is bothering me more than it should and has me completely stumped. I feel like like finding the answer will have some good learning opportunities so hopefully it's relevant.
I do embedded C development with Vim and have a setup for hobbyist stuff with Arduino (using Arduino Makefile). I use :make with some shortcuts with build projects.
An external define resolves the Arduino Makefile root directory in the project level Makefile: 'ARDMK_DIR=/usr/local/opt/arduino-mk'. This is define as an export in my shell (zsh). This is where it gets weird:
Using make at the shell prompt the project builds fine:
make -d
This program built for i386-apple-darwin11.3.0
Reading makefiles...
Reading makefile `Makefile'...
Reading makefile `/usr/local/opt/arduino-mk/Arduino.mk' (search path) (no ~ expansion)...
However using :make in Vim the define becomes something from an old install:
:make
This program built for i386-apple-darwin11.3.0
Reading makefiles...
Reading makefile `Makefile'...
Reading makefile `/usr/local/Cellar/arduino-mk/1.5.2/Arduino.mk' (search path) (no ~ expansion)...
Makefile:24: /usr/local/Cellar/arduino-mk/1.5.2/Arduino.mk: No such file or directory
I cannot for the life of me find where ARDMK_DIR is being re-defined to '/usr/local/Cellar/arduino-mk/1.5.2'. Things I have tried:
setlocal makeprg=echo\ $ARDMK_DIR\ &&\ make\ -d\: echo comes back with my shell define (/usr/local/opt/arduino-mk), but make fails with the error above!!
:echo $ARDMK_DIR: again returns my shell define.
ag my home directory for ARDMK_DIR, the only place it is defined is in my shell exports. Did since for my root directory to and same thing. Same thing for $VIMRUNTIME
Even vim-disptach works fine calling the same makeprg?!
Re-define ARDMK_DIR in the project Makefile. Everything builds find as expected. I don't want to do this however as I compile with different systems.
The same vim config works on other macOS and Linux systems with expected behaviour.
Some where between echo and the actual execution of make, ARDMK_DIR is being re-defined. Why and can anyone think of a way of finding out where and solving this?
Zsh has multiple init files that are sourced. The file .zshenv is always sourced, when the shell starts and the file .zshrc is only sourced when the shell is started in interactive mode.
If you define the variable ARDMK_DIR with different values in .zshenv and in .zshrc, the value from .zshrc will be used when you work interactive with the shell (entering commands, starting Vim, ...).
But when Vim starts a command it will start a non-interactive shell. In that case only the file .zshenv will be sourced, so you get the value from that file.
One question left:
Why did the following command first echo the correct value, but make uses the wrong?
:setlocal makeprg=echo\ $ARDMK_DIR\ &&\ make\ -d\
For testing, I started Vim under strace. Then :
:set makeprg=echo\ $EDITOR
:make
In the strace file I found the following line:
execve("/usr/bin/zsh", ["/usr/bin/zsh", "-c", "echo vi 2>&1| tee /tmp/vdxR5DH/"...], [/* 86 vars */]) = 0
As you can see, Vim executes echo vi, so it already expanded the environment variable $EDITOR to its value before calling the shell.
So the answer to the above question is, that the echo command echos the text, that Vim inserted into the command line while the make command gets the the variable value from the environment. As it is a non-interactive shell, it is the value from .zshenv.

How to run a list of vim commands in the current buffer from an external file

I frequently use Vim to parse and clean up data exported from other sources. Often, I find myself running a sequence of substitute commands. I'd like to save those commands to some kind of script file, then run it on the current buffer.
An example of the commands found in sample script could be:
:%s/pattern1/pattern2/g
:%s/pattern3/pattern4/g
:v/pattern5/d
... etc
If the script were in the same directory as the current buffer file (or maybe in the .vim folder), how could I load and run these commands as if I were typing them in manually?
If you have a Vim window open and want to execute VimL on a buffer, you can do:
:source /path/to/vim/file/name.vim
Put your commands in script.vim:
:%s/pattern1/pattern2/g
:%s/pattern3/pattern4/g
:v/pattern5/d
Use it like this:
$ vim filetocleanup -s script.vim
See :help -s.

Execute external command in the same screen

When executing :silent !command (followed by :redraw!) the screen goes to the terminal, then it come back to vim.
How can I make it execute in the same screen, without this "blink"?
If I understand you correctly you want to just execute some command, but don't want to see the output. If so, then just use the function system
:call system("g++ myfile.cpp -o output")
or whatever command you want to do. However, I would recommend just putting the output in a seperate window, you can use the QuickRun Plugin and the just run:
:QuickRun bash -src "g++ myfile.cpp -o output"
and the result will be in a separate window.

VIM. Sending commands to the terminal?

I'm trying to configure vim as my primary coding program. I have figured how to compile single files, but when I go to execute the program from within vim, I keep getting a 127 error code. I have a aliased on my box to ./a.out, however when I issue the command :!a from vim, it doesn't work. :!./a.out does. Does anyone know why this is?
Aliases are defined in rc files that are sourced by interactive shell only and work only in interactive mode (vim does pass everything to shell, it never executes anything except the shell directly with fork+execve).
By default shell launched from vim starts in non-interactive mode hence bashrc is not read and no aliases are defined (though even if they were defined, they won’t be used in non-interactive mode). You may set
set shellcmdflag=-ic
, then shell will be launched in interactive mode and .bashrc file with your alias will be read.
Aliases are a feature of your shell (say, bash). ! operator directly executes a file - shell never sees it, and can't do alias expansion on it. (EDIT: See ZyX)
If you want to make executing your ./a.out easy, you can do something like:
command XX !./a.out
then you can do :XX. Or,
nnoremap X :!./a.out<CR>
then a single key X will suffice.
Another option (which is frowned upon for security reasons) is to add the current directory to your PATH:
PATH="./$PATH"
which will allow you to run your program with just a.out as opposed to ./a.out. If you then compile your program with (a language-dependent isomorphism of) -o switch, you can have it named just a:
gcc -o a foo.c
Given this, !a would work. Use at your own risk.
EDIT: ZyX is correct-er. :) I'll leave the answer here for the other information.

Set vimrc makeprg to custom script if no Makefile

I want the :make command to execute normally if there is a Makefile present. Otherwise I want it to execute a custom script, say, called compile. If neither file is present, I'd like it to fail gracefully (ideally not leave the editing screen).
I'm aware of this thread, which presents a bash expression I was able to adapt to suit my needs. EXCEPT I sometimes need to run a shell other than bash. Is there a shell-independent way to do this?
makeprg can be set to the name of any executable. Make a shell script that runs make if there is a Makefile, otherwise run compile, or else return.

Resources