!!
(shorthand for :.!
) executes the current line as input to a command, per POSIX ex
& vi
. You may need to append sh
if it is a system command.
Executing !!
on a blank line (and omitting sh
) is a shortcut for reading a shell command straight into the buffer. By it's nature :.!
overwrites the current line while :.r!
inserts on the line below.
ls -A | head -n +4
~
~
!sh
Results:
.sh_history
.sh_logout
.kshrc
corelist.txt
~
~
4 lines added; 1 line deleted`
This means there is no need to redirect pipelines to a file and then examine the data to see if the contents are valid. Just execute commands in vi
directly and u
ndo if you make a mistake.
Alternately, yanking a line as a named buffer allows you to execute it as an ex
command, almost like a macro. You can still edit and u
ndo the line to get it correct instead of trying to edit the :
line in command mode.
The functions recommended here are all POSIX and have been supported for over 40 years, so no special vim or other enhanced features are required.
:%s/meep/pEEp/ | g/foo/ s//BAR
foo
grok
meep
~
~
Yank the ex
command (line 1, :%s...
) into a named buffer / macro.
I just use the label m
for "macro".
"myy
or
:1y m
Now execute the named buffer / macro, in command mode, using @
:
@m
Results:
:%s/pEEp/pEEp/ | g/BAR / s//BAR
BAR
grok
pEEp
~
~
4 lines changed
But remember that "multiple undo" is not POSIX. u
ndo is only a toggle between undo and redo in a "historically accurate & compliant" ex
/ vi
implementation.
The work-around is to save to a temporary (or valid) file name before executing a questionable edit:
:w $$.tmp
Then just :e!
to "reset and reload" if needed.
You can also use :pre
(preserve) to make a special temporary backup file prior to making multiple changes.
Then use :reco! %
(recover this!) to restore back to that point.
Realize that :preserve
creates a snapshot-like file which is deleted as soon as it is rolled back to. It does not matter if you save the edit(s) or not.
Therefore writing your own file (:w ...
) and restoring with :e!
may still have value because the system will not automatically delete it.
:pre
is perfect when you should have ran sudo vi ...
or otherwise do not have the necessary permissions - but you only realized the mistake after making several changes. i.e. vi /etc/sudoers
instead of sudo vi /etc/sudoers
.
^^ NEVER DO THIS! ONLY AN EXAMPLE! USE sudo visudo
INSTEAD!
You can get a list of existing recovery files with vi -r
and recover one directly with vi -r filename
as needed, optionally with something like sudo vi -r filename
.
The distinction here is that even though the ":preserve
d file" has it's own name and path internally, it will :write
to the original, intended location when ":recover
ed ==> /etc/sudoers
Just be sure to use :wq!
and not something like ZZ
when done with your "recovery" or you will still lose the edits which you tried to save.
By the way, ^R
is expected to redraw or repaint the display per POSIX; it is not "undo" in any compliant vi
implementation.
vnoremap <F2> "cy:<c-u>exe getreg("c")<cr>
– Bad_ptr