3

I know that I can suspend vim, and return to bash via:

Ctrlz

or access the shell via:

:sh 

or

:shell 

etc.

Then use bash commands as normal, and get back out using fg.

What I am wondering is, can I execute a line of code from a script in vim straight to Bash, without having to exit vim, or having to copy it (via highlighting in visual mode for example) from vim, then going to a terminal and pasting it and hitting enter etc.?

2 Answers 2

8

Easiest way is to put the cursor on the line and type:

!!shreturn

This will replace the line with the output of the script. If you don't want that, simply follow up with u.

Sign up to request clarification or add additional context in comments.

7 Comments

Oh that is cool! I've drilled down in the vim docs to varying levels, but have not run into this one yet.
Thanks this is a very cool trick. Not exactly what I was going for but I will upvote it and keep it in mind. Does this physically execute the command in bash also? Or just show the output?
This is actually very smart, I like it!
@StatGenGeek It runs the command (sh) with the current line as stdin and replaces the line with the stdout of the command. So, yes, it executes the command.
Note that if you really need to run it through bash, and if sh is not a link to bash, you can do !!bash
|
2

Arguably easiest way:

  1. Yank the text you want to execute.
  2. Open the cmdline (by pressing :)
  3. type ! and then press ctrl-r and ", which will paste the content of the unnamed register to the cmdline (which will contain the text you wanted to execute)
  4. press Enter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.