How to Execute The Vim Commands Through Shell Script

Executing Vim commands from a shell script

-c and + expect a command-line mode command (AKA "Ex command", the commands that start with :) but <C-w>o is a normal mode command so it can't be used directly, here.

One way to get around this problem would be to use :help :normal and your shell's ability to insert control characters via <C-v>:

$ vim +Man\ ls +normal\ ^Wo

with ^W being a literal <C-w> character obtained by pressing <C-v> followed by <C-w>.

But using the command-line mode equivalent of <C-w>o seems like a better idea:

$ vim +Man\ ls +wincmd\ o

See :help :wincmd.

How to run a vim command from the shell command-line?

Note, now the syntax has changed, and the line should read (As per @sheharyar):

vim +PluginInstall +qall

For posterity, previously, the correct line was:

vim +BundleInstall +qall

Should anyone other than me be looking! Note: this is in the Github README for vundle.

How to execute the vim commands through shell script

Alternatives

Unless you really need special Vim capabilities, you're probably better off using non-interactive tools like sed, awk, or Perl / Python / Ruby / your favorite scripting language here.

That said, you can use Vim non-interactively:

Silent Batch Mode

For very simple text processing (i.e. using Vim like an enhanced 'sed' or 'awk', maybe just benefitting from the enhanced regular expressions in a :substitute command), use Ex-mode.

# Unix
vim -T dumb --noplugin -n -es -S "commands.ex" "filespec"

Attention: Vim will hang waiting for input if the "commands.ex" file doesn't exist; better check beforehand for its existence! Alternatively, Vim can read the commands from stdin. You can also fill a new buffer with text read from stdin, and read commands from stderr if you use the - argument.

Full Automation

For more advanced processing involving multiple windows, and real automation of Vim (where you might interact with the user or leave Vim running to let the user take over), use:

vim -N -u NONE -n -c "set nomore" -S "commands.vim" "filespec"

Here's a summary of the used arguments:

-T dumb           Avoids errors in case the terminal detection goes wrong.
-N -u NONE Do not load vimrc and plugins, alternatively:
--noplugin Do not load plugins.
-n No swapfile.
-es Ex mode + silent batch mode -s-ex
Attention: Must be given in that order!
-S ... Source script.
-c 'set nomore' Suppress the more-prompt when the screen is filled
with messages or output to avoid blocking.

How to run a series of vim commands from command prompt

vim -c <command> Execute <command> after loading the first file

Does what you describe, but you'll have to do it one file at a time.

So, in a windows shell...

for %a in (A,B,C,D) do vim -c ":g/^\s*$/d" -c "<another command>" %a.txt

POSIX shells are similar, but I don't have a machine in front of me at the moment.

I imagine you could load all the files at once and do it, but it would require repeating the commands on the vim command line for each file, similar to

vim -c "<command>" -c "<command>" -c ":n" (repeat the previous -c commands for each file.)  <filenames go here>

EDIT: June 08 2014:
Just an FYI, I discovered this a few minutes ago.

vim has the command bufdo to do things to each buffer (file) loaded in the editor. Look at the docs for the bufdo command. In vim, :help bufdo

How to run a vim command on a file from the terminal?

Vim can run commands perfectly the way I interpret your desires.

You simply use ex +"<command>" <filename> or vim -c "<command>" <filename>, they are equivalent.

Try running the following:

$ ex +"norm gg=G" +"wq" <filename>

It will open your file, filter the whole file with the first command and save it (overwriting) with the wq command.

Can we execute vim commands from a shell script inside jenkins?

By default vim.tiny was installed in the jenkins server. vim.tiny is the compact version of vim and it did not support vimdiff as per the vim documentation. So vimdiff was not running in the shell script. As a work around, I included the following commands in the Pre-Build Steps -> Execute shell

#!/usr/bin/env bash
sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe"
sudo apt-get update
sudo apt-get -y install libncurses5-dev
sudo apt remove -y vim-tiny
sudo apt-get -y install vim
echo syntax off> ~/.vimrc

Functionality of Each command:

  • sudo add-apt-repository "deb http://archive.ubuntu.com/ubuntu $(lsb_release -sc) main universe"

    sudo apt-get update

    sudo apt-get -y install libncurses5-dev

    The above three commands installs the necessary libraries to execute sudo apt-get install command. Providing '-y' in the sudo command will automatically take yes for the terminal prompts that asks user's permission to install

  • sudo apt remove -y vim-tiny

    sudo apt-get -y install vim

    The above two commands removes the vim-tiny and then installs the full version of vim

  • echo syntax off> ~/.vimrc

    By default syntax highlighting was ON in vimdiff. So the look and feel of the vimdiff output was very worse. So the above command creates a '.vimrc' file and adds 'syntax off' to that file and saves it. This will Turn OFF the syntax highlighting in the vimdiff output and improves the look and feel

After executing all these steps in the Jenkins Pre-Build Step, vimdiff became available inside the currently running jenkins build and I was able to use the below vimdiff command in my shell script that is called from a java method.

/usr/bin/vimdiff -c 'set foldlevel=9999' $1 $2 -c TOhtml -c 'w! '"$3"'' -c 'qa!'

How to execute command inside vim?

There are multiple ways to do it. A primary question is "Do you want the output from the script in the file?"

  1. If you want the output in the file:

    :r!./shell.sh
  2. If you don't want the output in the file:

    :!./shell.sh
  3. If you have the line ./shell.sh in the file, you can include the output in the file with:

     !!sh
  4. If you've done it before, you have more options.

  5. If you save the command in a named buffer you have still more options.

  6. If you want the script to have a portion of the file (edit buffer) as its standard input, you have an enormous number of options you can use in conjunction with either of these mechanisms.

How to vim script to execute commands in function

You've expressed your actions as you'd have written a mapping.

You don't need, and must not use <CR> (and <bar> at the end of the line) -- nor :exe -- in your case. And, don't be afraid to write your commands on several lines.

And don't forget to update the variable.

nnoremap <Leader><CR> :<c-u>call <sid>Goto_definition()<CR>

let s:first_open = get(s:, 'first_open', 0) " set to 0 the first time, keep the old value when resourcing the plugin

function! s:Goto_definition() abort
if ! s:first_open
wincmd l
q
endif

" Looks like the following steps shall always be executed.
rightbelow vs " same as vs + wincmd l
normal K
" with a K command (which doesn't exist), it could have been done with: "rightbelow vs +K"

let s:first_open = 1 - s:first_open
endfunction

PS: line numbers help to understand where the problem is.



Related Topics



Leave a reply



Submit