34

I use vim for bash scripting, and sometimes I write a one-time script. Usually, I don't want to create a new file, make it executable, execute it and then remove it.

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?

2
  • 4
    You know you can edit the current command-line in Vim with <C-x><C-e> and execute it on write, right?
    – romainl
    Commented Nov 7, 2016 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
    Commented Aug 22, 2017 at 0:22

2 Answers 2

52

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.

4
  • 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
    Commented Nov 8, 2016 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
    Commented Nov 8, 2016 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
    Commented Nov 8, 2016 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
    Commented Nov 8, 2016 at 18:16
6

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
5
  • 3
    might not work if you have a new macbook without an Esc key ;)
    – roblogic
    Commented Nov 10, 2016 at 3:00
  • 1
    New MacBooks don't have Esc keys!? How can they use Vim then?
    – Hubro
    Commented May 19, 2017 at 9:02
  • 1
    Now I've tried it, the new macbook touchbar thingy does show an Esc key when running Vim in a terminal.
    – roblogic
    Commented May 23, 2017 at 3:25
  • 1
    Also, Esc is not the only way to return to normal mode. You can also use Ctrl-c or Ctrl-[. vimhelp.org/insert.txt.html#i_CTRL-%5B Commented Aug 9, 2020 at 2:33
  • No judgement but if you're using vim without remapping your keys, you're really stressing out your hands for no reason. Look into Karabiner and Ergodox (software / hardware solutions) for this problem. Commented Apr 21, 2022 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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