20

I use vim for bash scripting. the thing is sometimes I don't want to create a new file, make it executable, execute it and then remove it. I mean the scripts which are going to be used only once.

For instance I open vim and type this:

#!/bin/bash

ll | while read line
do
    echo $line
done

I did not save this file, I just use vim to simplify bash-scripting. I know I can execute a bash command via ! in vim. But what about executing everything that is in the buffer?

  • 4
    You know you can edit the current command-line in Vim with <C-x><C-e> and execute it on write, right? – romainl Nov 7 '16 at 20:02
  • If you run bash with this option set -o vi, you can hit Esc-v to edit the command line in vi. Then Esc-:wq to execute. – roblogic Aug 22 '17 at 0:22
33

I wrote a pretty extensive answer about this over on stack-overflow. The basic idea is that since the write command is about writing not saving, you can write the text in your buffer into an external program, such as python or bash. In your case, you would want to do something like:

:w !bash

or

:w !sh

These commands literally just write the text in your buffer directly into the program you specify with !, which is convenient because then you do not need to save a file. Of course, you may also do something like:

:w | !bash %

which saves your file and then runs it as a bash script. You can choose whichever one makes more sense for you.

  • Is there a way to provide options an argument this way? I tried :w !bash -a test but got "cannot execute binary file" – St.Antario Nov 8 '16 at 17:23
  • @St.Antario Hmm. I'm sure there must be some way, but my bash is pretty rusty (I mostly use windows). When I try that in cygwin with an empty test file, it runs fine for me. What is the command supposed to do, and what should test contain? – DJMcMayhem Nov 8 '16 at 17:27
  • My actual case is pretty complicated, so let it just print the argument passed in. BTW, what do you mean what should test contain? test is just an argument to -a. – St.Antario Nov 8 '16 at 17:48
  • 1
    @St.Antario Oh, OK I see. I thought test was supposed to be a file. I'm not really sure what to do. If you're trying something complex it might be better to just save it in a file. It might be worth asking as a separate question so you can provide more detail. It would probably also be a good question for Unix and Linux – DJMcMayhem Nov 8 '16 at 18:16
4

Scenario: you are working in bash shell. First, make sure the local edit mode is vi:

$ set -o vi

Now, when you find yourself working on a long command like the following, hit Esc+v.

$ fjhwfq this is my long command

This will load your command into a temporary vi session. Now you can edit it as you wish, and even add another command...

fjhwfq some long command
ls -l

When you have finished editing, type Esc :wq and the command(s) will be executed, for example:

$ fjhwfq some long command
fjhwfq some long command
bash: fjhwfq: command not found
ls -l
total 44
-rw-r--r-- 1 ropata Domain Users 24073 Nov 10 14:17 _viminfo
drwxr-xr-x 1 ropata Domain Users     0 Jul 15 13:57 bin/
-rw-r--r-- 1 ropata Domain Users   956 Nov  7 10:43 custom_env
  • 2
    might not work if you have a new macbook without an Esc key ;) – roblogic Nov 10 '16 at 3:00
  • New MacBooks don't have Esc keys!? How can they use Vim then? – Hubro May 19 '17 at 9:02
  • Now I've tried it, the new macbook touchbar thingy does show an Esc key when running Vim in a terminal. – roblogic May 23 '17 at 3:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.