Vim in Linux

Source: Internet
Author: User

No matter how many files you have to edit, you can process them in vim. You can define a list of files to be edited, switch from one file to another, or copy and paste between different files.

A. Edit another file
B. File List
C. Switch to another file
D. Backup
E. copy and paste files
F. view files
G. Change the file name.

Edit another file:

So far, we have used Vim to run Vim once for every file to be edited. This is the simplest usage.

: Edit foo.txt

You can edit another file in vim. Of course, you can replace "foo.txt" with any file name ". Vim will close the file being edited and open the specified new file for editing. If the current file has unsaved content, VIM will display the following error message and will not open another file:

E37: No write since last change (use! To override)

At this time, you can have several options, you can save the file:

: Write

Alternatively, you can force Vim to discard unsaved changes and edit new files, and use the Force Execution modifier:

: Edit! Foo.txt

If you want to edit another file but do not want to save the changes in the current file, you can make it a hidden Buffer:

: Hide edit foo.txt

File List:

You can specify to edit multiple files when starting vim, for example:

Vim one. C Two. c Three. c

Run this command to start vim and tell it that you want to edit three files. Vim will only display the first file after it is started. After editing the file, run the following command:

: Next

Start editing the next file. If your current file contains unstored content, you will get an error message as before, and the ": Next" command will not continue, this is the same as the ": edit" command mentioned above. You must discard these changes and use:

: Next!

However, in most cases, people still need to save their work results and edit the next file. There is a command to merge the process:

: Wnext

This command completes the following two separate commands:

: Write
: Next

Which file is being edited?
You can view the title bar of the window to learn the name of the file you are editing. It should also display information like "(2 of 3. This means that you are editing three
List of 2nd files.
If you want to view the files in the entire list, run the following command:

: ARGs

This is a short form of "arguments". The output result is as follows:

One. C [Two. C] Three. c

This is the list of files to be edited specified when you start Vim. The file you are editing will be enclosed in square brackets.

Move to another file:

To return to the previous file:

: Previous

Just like the ": Next" command, but it is oriented to another file. There is also a corresponding shortcut command:

: Wprevious

To move to the last file:

: Last

To the first one:

: First

However, there is no such command as ": wlast" or ": wfirst.

You can also use a command to count before the ": Next" and ": Previous" commands, to skip two files forward:

: 2 next

Automatic disk storage

When transferring between different files, you must remember to use the ": write" command to save the disk. otherwise, an error message is returned. if you are sure that you want to save the file every time, you can tell Vim to automatically save the file whenever necessary, without having to ask:

: Set autowrite

If you are editing an object that you do not want to save automatically, you can disable this option:

: Set noautowrite

Edit another file list:

You do not need to restart Vim to redefine a file list. The following command defines three files to be edited:

: ARGs five. c six. c Seven. h

Or use a wildcard, just like using a wildcard in shell:

: ARGs *. txt

Vim will open the first file in the list. Similarly, if the current file is modified but not saved to the disk, you need to save the current file first, or use ": ARGs! "(Added one !) Discard the content that is not stored in the current file.

Have you edited the last file?

When you have a list of files to edit, VIM assumes that you want to edit them all. to prevent premature exit of some files, VIM will provide an error message when you want to exit the last file without editing it:

E173: 46 more files to edit

If you are sure you want to exit, you only need to execute the exit command again. This time, you can actually exit (but do not perform other operations in the middle of the two executions of the same command)

Switch to another file:

To quickly switch between two files, use ctrl-^ (on the American keyboard ^ on 6 of the primary key). For example:

: ARGs one. C Two. c Three. c

The currently edited file is one. C.

: Next

Now it becomes two. c. you can use ctrl-^ To return to one. c. run Ctrl-^ again and then go to two. c. If you execute the following loop:

: Next

Now you will go to three. c. note that the Ctrl-^ command does not change the current position in the file list. Only the commands ": Next" and ": Previous" will cause this location change.
The last file you edited is the "alternate" file. Therefore, when you enter vim, you can use this command without any other files.

Copy and paste files:

This section describes how to copy content between different files. Let's start with a simple example. edit the file from which you want to copy the content. move the cursor to a file and press "V ". this command starts the visual mode. now move the cursor to the end of the file to be copied and press "Y ". this command will copy the selected content of Yanks.
To copy the above section, what you need to do is

: Edit thisfile
/This
Vjjjj $ y

Now, edit the file you want to copy. Place the cursor where you want to copy the content, and paste the copied content here with "P.

: Edit otherfile
/There
P

Of course, you can use other commands to copy the content of Yank. for example, you can use the "v" command to select the entire line of text in visual mode. or use a CTRL-V to select the content of a rectangle. or use "Y" to select the content of the current row, and use "Yaw" to yank-a-word, and so on.
The "p" Command places the copied content after the cursor. "P" is used to place the content to be copied before the cursor. note that Vim will know whether the copied content is a whole line or a rectangle. It will also adopt the corresponding method when pasting the content.

Append content to a file

To collect multiple lines of text and write them together into a file, run the following command:

: Write> logfile

This will append the content of the current file to the file "logfile". This will avoid using the previous method to copy the content and edit the log file. This will save two steps.
The limitation is that content can only be appended at the end of the file. to append only a few lines of content to the file, you can select the content to be written in visual mode before using the command ": write. in Chapter 10th, you will learn about other optional text lines.
Method.

View files:

Sometimes you only want to view the content of the file and won't write anything into it. however, using ": W" without thinking about it may lead to the risk of overwriting the original file. to avoid this error, You can edit the file in read-only mode.
The following command runs Vim in read-only mode:

Vim-r File

Now you will edit "file" in read-only mode, and then try to use ": W" to get an error message telling you that the file cannot be saved.

If you try to modify this file, you will also receive a warning message:

W10: Warning: Changing a readonly File

However, you can still make modifications so that people can format the file for convenience of browsing.
If you modify the file but forget that it is read-only, you can save it. Use it after the ": write" command! Force modifier 1.
If you want to forcibly avoid modifying the file, you can use the following command:

Vim-M file

In this way, every attempt to modify the file will fail. The help file is like this. For example, when you try to modify the Help file, you will see the following error message:

E21: cannot make changes, ° modifiable ° is off

You can use the-M option to tell Vim to work in the viewer mode. This indicates that you are willing to do so. After all, the following command can still remove this layer of protection:

: Set modifiable
: Set write

Change File Name:

The best way to edit a new file is to modify it based on a file with similar content. for example, you want to write a program to move files. at the same time, you already have a program that can copy files. You can start like this:

: Edit copy. c

You can delete unnecessary parts. Now you can save it as a new file. The ": saveas" command is exactly like this:

: Saveas move. c

Vim will save the content in the current buffer with the given file name and edit the file. in this case, the next time you use the ": write" command, it will be saved as "move. C ", and" copy. c. Keep the original content.
If you want to change the file name being edited but do not want to save the file, you can run the following command:

: File move. c

Vim will mark the file as "not edited". This means that Vim knows that this is not the file you started to edit when you enter Vim. You will receive this error message when saving the file:

E13: file exists (use! To override)

This protects you from accidentally overwriting other files.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.