Vim run shell and see output - vim

I'm trying to run an unsaved buffer in a shell command and have a history of the output
console.log("test")
Then, :w !node does what is expected, test is outputted, but once I click enter, the output disappears, and doesn't even show in :messages:
How can I find it?

If you open a Vim buffer and type in one or more shell commands each on "his" own line. Like:
ls /home
ls /root
Then you can type the command:
:%!bash
and not only will it run the commands on each line one after the other, it will also overwrite the buffer with the output of each command in chronological order, then you can do whatever you want with it :-)
I hope it was helpful :)
BTW: if you want to run a command in Vim and get the output in your buffer, then you can just double tap the exclamation mark in NORMAL MODE and the command line in the bottom will show
:.!
then you just type in your command and press enter. :)
easy peachy lemon squeeze :)
note: I learned that last one by mistake

What :w !cmd does it pipes your current buffer contents into cmd's stdin. When you don't need this you should simply execute :!cmd instead.
:messages serve to show an important log data printed by specially dedicated :echom[sg] command, not some random stuff from terminal windows. So the output from "node" process will not and should not ever get there.
You can put the process' stdout into the current buffer by using :r[ead] command, e.g.
:r !node

Related

Cygwin terminal input disappearing after quitting vim

Using Cygwin, I tried creating and editing a file in Vim:
touch test | vim
This is obviously a mistake; something like vim "$(touch test)" has a better chance of actually working. Nevertheless, this command throws the error:
Vim: Warning: Input is not from a terminal.
And after this, Vim opens and I exit the program with :q. Any subsequent commands I enter into the terminal are hidden from view until I restart Cygwin.
Why is this?
You don't understand what does a pipe | do in shell.
Pipe will take the pervious command's stdout as stdin to next command, in a subshell.
Your touch foo doesn't generate any output, what do you expect to happen? same for vim "$(touch test)".
If you want to create a file and open it in vim in one shot, you can try:
touch foo && vim foo
If you want to edit it with vim anyway, actually, you can simply just:
vim foo
then save the buffer after your editing.

What will this command do

I accidentally ran the following command in my console.It was a copy paste error.
vim -> /etc/apache2/sites-available/25-xyz-https.conf.
But after that my 25-xyz-https.conf got corrupted. Eventhough I recovered the file just curious to understand what has happened.
This happens:
vim -
means open stdin in vim.
> file
is an output redirection by the shell. Stdout of the (vim) process will get stored in file. file will get truncated by the shell before the (vim) process get's started.
I recommend to always put a # into the shell before pasting things into the shell. That gives you a chance to review the line before executing it, especially if you paste the line together with the line break at the end. (which would execute it right away)
The shell ran "vim -" and then redirected the output from that command to /etc/apache2/sites-available/25-xyz-htttps.conf
If you run "vim -" you'll see it do something like this:
Vim: reading from stdin...
You will have to hit ^C to break out of reading from stdin, then :q to exit vim.
This is because many utilities interpret the '-' character as stdin (or stdout, depending on the context).
If you did something like:
date | vim -
This would open 'vim' with the contents of the document showing the current date. There is no open file, you could not simply :w to save the file, but you could :w./thedate.txt to save the contents to ./thedate.txt. The important thing is that the output of the 'date' command became the input to the 'vim' command.
After that, the > character just redirects stdout from the whole "vim -" session to overwrite the file provided.

store all the data in terminal to text file by tee command or equivalent tool

I learnt that a tee command will store the STDOUT to a file as well as outputs to terminal.
But, here the problem is every time I have to give tee command, for every command I give.
Is there any way or tool in linux, so that what ever I run in terminal, it should store the command as well as output. (I used tee command in MySQL, where it will store all the commands and outputs to a file of that entire session. I am expecting a tool similar to this.)
Edit:
When I run script -a log.txt, I see ^M characters as well as ^[ and ^] characters in log.txt file. I used various dos2unix, :set ff=unix, :set ff=dos commands, but they didn't helped me in removing these ^[, ^] characters.
Is there any method, I can directly get the plain text file (with out these extra chars).
OS: RHEL 5
You can use script command which writes everything on file
script -f log.txt
you could use aliases like such alias ls="ls;echo ls >>log" so every time you run ls it runs echo ls >>log too.
But script would probably be better in this case, just dont go into vi while you are in script.

Vim Ex mode loads when opening terminal

I know little about Vim in terminal(Mac) and the other day I was working copy and pasting text and i think I accidently did it when in terminal. Now whenever I open terminal it instantly loads on Vim Ex mode. I know how to quit Ex mode once in terminal but is there any way i can get rid of Vim loading when I open Terminal?
Thanks
Edit: To explain further to what i mean when I open terminal.app from Utilities I get the following
and the only way I get back to the command prompt is by typing quit every time I open terminal and i cant understand why the Vim process is running in the first place.
I was just outside the terminal in a document copy and pasting text then accidentally did a command v to paste within terminal which resulted in this happening.
It appears that you've accidentally updated one of your shell startup scripts so it launches vim.
If your default shell is csh or tcsh, take a look at .cshrc, .tcshrc, and .login in your home directory, and look for a command like vi -e or vim -e.
If your default shell is bash, check .bashrc and .bash_profile.
It may be easier to figure out which file you messed up by checking which file in your home directory was modified most recently:
% ls -altr $HOME | tail
-a lists all files, including files whose names start with ..
-l gives you a long listing, showing timestamps.
-t sorts by modification time.
-r reverses the order, so newer files are shown last

How to simply ignore output from a program when called as an external one in vim?

I can dump the output content from my external command in the main window, I can disable "Press ENTER or type command to continue" and simply store it in a register.
But how do I call an external command in vim (it can be any program, apt-get, etc) and simply avoid it creating a buffer window if an output? Simply IGNORE the output from a external command I ran? I just want to call the command from vim. The command starts a simple webserver (listening on port 8080) and I have to press ctrl+c to stop it and move away from the external command buffer.
I tried silent before !cmd, it works, but I would like to stop the process my external command created right after it was started.
EDIT: I changed my mind about the simple webserver. I another situations, just doing like the suggestion accepted answer it works.
Pipe output to /dev/null:
:!cmd &> /dev/null
Use silent as you mention to get rid of the Press ENTER or type command to continue:
:silent !cmd &> /dev/null
Read this page for more on hiding this message.

Resources