Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to let vi run editing commands in a file?

Tags:

vim

vi

Let's say I have a file "edit_commands" with some editing commands, like:

:1,$s/good/better/g
:1,$s/bad/worse/g

Is there a way to let vi load and run the commands in "edit_commands"?

Update: It appears the "-S" option can be used for this. Refer to: How to run a series of vim commands from command prompt

like image 651
Joe Smith Avatar asked Jun 26 '21 16:06

Joe Smith


People also ask

How do I enable editing in vi?

Vi edit modesTo enter Insert mode, press i . In Insert mode, you can enter text, use the Enter key to go to a new line, use the arrow keys to navigate text, and use vi as a free-form text editor. To return to Command mode, press the Esc key once. [ Looking for a different text editor?

In which mode of vi you can edit file content?

From Command Mode :w Write changes to your file. :sh Return to the shell to enter a number of commands without leaving vi. Press <Control>d to return to vi editing.

Which vi editor allows us to type commands?

Command Mode: When vi starts up, it is in Command Mode. This mode is where vi interprets any characters we type as commands and thus does not display them in the window. This mode allows us to move through a file, and to delete, copy, or paste a piece of text.

How do I run a command in vi?

The vi has the capability to run commands from within the editor. To run a command, you only need to go to the command mode and type :! command.

How to run Linux commands in vi editor?

How to run Linux Commands within vi/vim editor 1 Insert Linux/Unix Command Output in vi editor : Example: Insert the output of hostname in /etc/hosts file. ... 2 Starting a shell within vi Editor. Type of a shell that is started is determined by the $SHELL variable. ... 3 Open an existing file within the vi editor. ...

How do I save and quit a VI file in Linux?

Saving and Quitting. You can save and quit vi from command mode. First, ensure you’re in command mode by pressing the escape key (pressing the escape key again does nothing if you’re already in command mode.) Type :wq and press enter to write the file to disk and quit vi.

How to use VI command in AutoCAD?

Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands. Once you’re done in insert mode, press the escape key to return to command mode. You can save and quit vi from command mode.

How do I use Vivi in command mode?

Vi is a modal text editor, and it opens in command mode. Trying to type at this screen will result in unexpected behavior. While in command mode, you can move the cursor around with the arrow keys.


Video Answer


2 Answers

It seems like you would want to use a program like sed which shares a common ancestor with vi (i.e. the 'ed' editor) for such a task:

sed -i 's/good/better/g; s/bad/worse/g' your_file

See this great sed tutorial.

Is there a reason you need to use vi to do it? You could use perl if you need more advanced regex capabilities.

like image 115
mattb Avatar answered Oct 17 '22 21:10

mattb


The solution in Perl may look this way:

perl -i.old -pe 's/good/better/g || s/bad/worse/g' your_file

The -i.old option saves a copy of your old file under the name your_file.old, what can be very useful when bad comes to worse and worse comes to worst...

like image 41
Aditya Avatar answered Oct 17 '22 19:10

Aditya