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.
Related
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
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.
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
When using vim in ubuntu, I accidentally pressed ctrl-z which suspended my session of vim. I was editing a file (I'll call it test) which was not saved.
When I opened the file again in vim, I got the swap file error:
E325: ATTENTION
Found a swap file by the name ".test.swp"
Swap file ".test.swp" already exists!
According to Found a swap file by the name question, I have two options:
Find the session and finish it (preferable).
Delete the .swp file (if you're sure the other git session has gone away).
How would I do either of those things? If I perform rm test.swp it doesn't see the file:
rm: cannot remove `test.swp': No such file or directory
What am I doing wrong in the deletion of the swap file and how can I finish the session?
EDIT: I forgot the period in test.swp
So the correct way to remove the swp file is rm .test.swp.
My remaining question is how to resume/finish a suspended session of vim.
This is not related to Ubuntu alone, since what happens is a base mechanism of
nearly every Unix OS.
By pressin ^Z you ave suspended (not ended) the currently running vim session. The vim
session is still there and waiting for a signal to put it to foreground again.
To reactivate the session:
If vim was started directly from the commandline -- use the command "fg" (which is for "ForeGroung") and vim will appear again. This works on all ksh/bourne-like shells. For t/csh I dont know. This only works when the command "fg" was given on the console of the same terminal session as from which vim was started (which is the "controlling terminal" related to the vim session).
If vim was started (mostly under the name gvim) from a menu of a windowmanager you are a little bit out of luck here, since (g)vim gets detached from its controling terminal.
Your options to recover:
Use "fg" if the condition is valid described above. This is the cleanest way.
If the (g)vim session is detached from the controling terminal, which can be checked by doing a "ps -ef | grep vim". If the column for the TTY (see header of the output) shows
a "?" there is no controling terminal anymore, I would recommend to send the process
a SIGHUB (see manpage for the commmand "kill"/"killall") and then a SIGKILL if it is still there.
Killing vim (or any other task) will probably result in inconsistent data though, cause there was no "save" command to vim before it is killed.
After that, start a new vim with the same file, do a "recover" first (as offered by vim, which sees the according swp-file) , save the file, end vim and start it again with that file and do a "delete swap file". This is the savest way possible after killing vim.
To avoid accidentically putting vim into background if not wanted, map ^Z to another, more
"complicated" keysequence, which is hard to press accidentically. You can deactivate the
^Z command by adding the following line to your .vimrc:
map <C-z> ;
Addition: Your rm-command misses the dot in front of .test.swp causing rm not to
find the file...or deleting another file, which is named test.swp instead of ".test.swp". By deleting swp-files via vim, you are sure to delete the correct file.
Swp-files always start with a dot (hidden file) on UNIX like systems.
the file is going to be " .test.swp ", not " test.swp "
So you'll want to:
rm .test.swp
I want to write a Bash shell script that does the following:
Opens a file using Vim;
Writes something into the file;
Saves the file and exits.
echo 'About to open a file'
vim file.txt # I need to use vim application to open a file
# Now write something into file.txt
...
# Then close the file.
...
echo 'Done'
Is that possible? I found something called Vimscript, but not sure how to use it.
Or something like a here document can be used for this?
Update: I need to verify that Vim is working fine over our file
system. So I need to write script that invokes Vim, executes some
command, and closes it. My requirements do not fit into doing stuffs
like echo 'something' > file.txt. I got to open the file using Vim.
ex is the commandline version for vi, and much easier to use in scripts.
ex $yourfile <<EOEX
:%s/$string_to_replace/$string_to_replace_it_with/g
:x
EOEX
Vim has several options:
-c => pass ex commands. Example: vim myfile.txt -c 'wq' to force the last line of a file to be newline terminated (unless binary is set in some way by a script)
-s => play a scriptout that was recorded with -W. For example, if your file contains ZZ, then vim myfile.txt -s the_file_containing_ZZ will do the same as previously.
Also note that, invoked as ex, vim will start in ex mode ; you can try ex my_file.txt <<< wq
You asked how to write "something" into a text file via vim and no answer has necessarily covered that yet.
To insert text:
ex $yourfile <<EOEX
:i
my text to insert
.
:x
EOEX
:i enters insert mode. All following lines are inserted text until . is seen appearing by itself on its own line.
Here is how to search and insert as well. You can do something such as:
ex $yourfile <<EOEX
:/my search query\zs
:a
my text to insert
.
:x
EOEX
This will find the first selection that matches regex specified by :/, place the cursor at the location specified by \zs, and enter insert mode after the cursor.
You can move \zs to achieve different results. For example:
ex $yourfile <<EOEX
:/start of match \zs end of match
:a
my text to insert
.
:x
EOEX
This will change the first occurrence of "start of match end of match" to "start of match my text to insert end of match".
If you want to allow any amount of whitespace in your searches between keywords, use \_s*. For example, searching for a function that returns 0: :/\_s*return\_s*0}
If you are wanting to see the work being done inside vim or gvim you can use --remote-send
gvim --servername SHELL_DRIVER
bashpromt# cat mybash.sh
#!/bin/bash
echo "about to open $1"
gvim --servername SHELL_DRIVER $1 #I need to use vim application to open a file
#now write something into file.txt and close it
gvim --servername SHELL_DRIVER --remote-send '<ESC>i something to the file<ESC>:wq<CR>'
echo "done."
This will be slow but will do what you want it to.
First we open a gvim in which we can open all of our files (for efficiency)
With the first gvim line we open the file in the previously opened gvim.
On the second gvim line we send a command to the previously opened instance of gvim (with the desired file still open).
The command is as follows:
<ESC> - get out of any mode that gvim might have been in
i something to the file - go into insert mode and type " something to the file"
<ESC> - exit insert mode
:wq - write the file and quit vim
Recently, I have answered a similar question, “Automated editing
of several files in Vim”. May be the solution that I describe there
will satisfy your needs.