Read Viminfo When Argc() > 0

by ADMIN 29 views

Introduction

When working with Vim, it's essential to understand how it interacts with the viminfo file. The viminfo file stores information about the current session, including the buffer list, cursor positions, and other settings. In this article, we'll explore how Vim reads the viminfo file when called with a file argument and how to force Vim to restore the buffer list in such cases.

Does Vim read the viminfo when called with a file argument?

When you call Vim with a file argument, such as vim file.txt, it will read the viminfo file to restore the buffer list and other settings. However, the viminfo file is not read if the --noplugin option is used or if the viminfo file is not present.

How can I force Vim to restore the buffer list in that case?

To force Vim to restore the buffer list when called with a file argument, you can use the following methods:

1. Use the -R option

You can use the -R option when calling Vim to read the viminfo file and restore the buffer list. For example:

vim -R file.txt

This will read the viminfo file and restore the buffer list, even if the viminfo file is not present.

2. Use the :source command

You can use the :source command to read the viminfo file and restore the buffer list. For example:

:source ~/.viminfo

This will read the viminfo file and restore the buffer list.

3. Use the :set viminfo command

You can use the :set viminfo command to read the viminfo file and restore the buffer list. For example:

:set viminfo=

This will read the viminfo file and restore the buffer list.

Use case & context

When called as vi, enable read/write of the viminfo file:

if argc() > 0
  set viminfo=
endif

This will enable the reading and writing of the viminfo file when called as vi.

Additional information

When called as vi, the viminfo file is not read by default. However, you can use the :set viminfo command to read the viminfo file and restore the buffer list.

Conclusion

In conclusion, when called with a file argument, Vim will read the viminfo file to restore the buffer list and other settings. However, the viminfo file is not read if the --noplugin option is used or if the viminfo file is not present. To force Vim to restore the buffer list in such cases, you can use the -R option, the :source command, or the :set viminfo command.

Example use cases

Here are some example use cases:

1. Enable read/write of the viminfo file when called as vi

if argc() > 0
  set viminfo=
endif

This will enable the reading and writing of the viminfo file when called as vi.

2. Use the -R option to read the viminfo file

vim -R file.txt

This will read the viminfo file and restore the buffer list, even if the viminfo file is not present.

3. Use the :source command to read the viminfo file

:source ~/.viminfo

This will read the viminfo file and restore the buffer list.

4. Use the :set viminfo command to read the viminfo file

:set viminfo=

This will read the viminfo file and restore the buffer list.

FAQs

Here are some frequently asked questions:

Q: What is the viminfo file?

A: The viminfo file is a file that stores information about the current session, including the buffer list, cursor positions, and other settings.

Q: Why is the viminfo file not read when called as vi?

A: The viminfo file is not read by default when called as vi. However, you can use the :set viminfo command to read the viminfo file and restore the buffer list.

Q: How can I force Vim to restore the buffer list when called with a file argument?

Q: What is the viminfo file?

A: The viminfo file is a file that stores information about the current session, including the buffer list, cursor positions, and other settings.

Q: Why is the viminfo file not read when called as vi?

A: The viminfo file is not read by default when called as vi. However, you can use the :set viminfo command to read the viminfo file and restore the buffer list.

Q: How can I force Vim to restore the buffer list when called with a file argument?

A: You can use the -R option, the :source command, or the :set viminfo command to force Vim to restore the buffer list when called with a file argument.

Q: What is the difference between the -R option and the :source command?

A: The -R option is used when calling Vim from the command line, while the :source command is used within Vim. The -R option will read the viminfo file and restore the buffer list, while the :source command will read the viminfo file and restore the buffer list, but only within the current Vim session.

Q: Can I use the :set viminfo command to read the viminfo file?

A: Yes, you can use the :set viminfo command to read the viminfo file and restore the buffer list. However, this command will only read the viminfo file and restore the buffer list, but it will not write any changes to the viminfo file.

Q: How can I write changes to the viminfo file?

A: You can use the :set viminfo command with the += option to write changes to the viminfo file. For example:

:set viminfo+=

This will write any changes to the viminfo file.

Q: Can I use the viminfo file with other editors?

A: Yes, you can use the viminfo file with other editors that support the viminfo format. However, you will need to configure the other editor to read and write the viminfo file in the same format as Vim.

Q: How can I customize the viminfo file?

A: You can customize the viminfo file by using the :set viminfo command with various options. For example, you can use the += option to add new settings to the viminfo file, or the -= option to remove existing settings from the viminfo file.

Q: Can I use the viminfo file with a remote Vim session?

A: Yes, you can use the viminfo file with a remote Vim session. However, you will need to configure the remote Vim session to read and write the viminfo file in the same format as the local Vim session.

Q: How can I troubleshoot issues with the viminfo file?

A: You can troubleshoot issues with the viminfo file by checking the Vim error messages, or by using the :verbose set viminfo command to see the current settings for the viminfo file.

Q: Can I use the viminfo file with a plugin?

A: Yes, you can use the viminfo file with a plugin. However, you will need to configure the plugin to read and write the viminfo file in the same format as Vim.

Q: How can I reset the viminfo file?

A: You can reset the viminfo file by deleting the viminfo file and then restarting Vim. Alternatively, you can use the :set viminfo command with the -= option to remove all settings from the viminfo file.