官术网_书友最值得收藏!

  • Mastering Vim
  • Ruslan Osipov
  • 427字
  • 2021-06-10 18:51:27

Buffers

Buffers are internal representations of files. Every file you open will have a corresponding buffer. Let's open a file from the command line: vim animal_farm.py. Now, let's see a list of existing buffers:

:ls
Many commands have synonyms, and :ls is not an exception: :buffers and :files will accomplish the same thing. Pick one that's the easiest  for you to remember!

Here's what the output of :ls looks like (see the bottom three lines):

The status bar shows some information about the buffers we have open (we only have one right now):

  • 1 is the buffer number, and it'll stay constant throughout the Vim session
  • % indicates that the buffer is in the current window (see the Windows section)
  • a signals that the buffer is active: it's loaded and is visible
  • "animal_farm.py" is the filename
  • line 1 is the current cursor position

Let's open another file:

:e animals/cat.py

You can see that the file we initially opened is nowhere to be seen and has been replaced with the current file. However, animal_farm.py is still stored in one of the buffers. List all of the buffers again:

:ls

You can see both filenames listed:

How do we get to the file, then?

Vim refers to buffers by a number and a name, and both are unique within a single session (until you exit Vim). To switch to a different buffer, use the :b command, followed by the number of the buffer:

:b 1
You can shorten the previous by omitting the space between :b and the buffer number: :b1.

Voila, you're taken back to the original file! Since buffers are also identified by a filename, you can switch between them using partial filenames. The following will open the buffer containing animals/cat.py:

:b cat

However, if you have more than one match, you'll get an error. Try looking for a buffer with a filename containing py:

:b py

As you can see in the following screenshot, the status line displays an error:

That's when you can use tab completion to cycle through the available options. Type in :b py (without hitting Enter) and press the Tab key to cycle through the available results.

You can also cycle through buffers using :bn (:bnext) and :bp (:bprevious).

Once you're done with the buffer, you can delete it, hence removing it from the list of open buffers without quitting Vim:

:bd

This will return an error if the current buffer is not saved. Hence, you'll get a chance to save the file without accidentally deleting the buffer.

主站蜘蛛池模板: 平凉市| 乐至县| 玛多县| 白山市| 陇西县| 宜章县| 肃南| 平阴县| 石嘴山市| 富锦市| 桂平市| 玛纳斯县| 扎兰屯市| 龙陵县| 开封市| 乐业县| 楚雄市| 将乐县| 铜鼓县| 勃利县| 成安县| 阳春市| 开平市| 江西省| 罗源县| 黑龙江省| 蓬安县| 社旗县| 广安市| 黄山市| 平乡县| 富宁县| 张家界市| 嘉祥县| 阳朔县| 上虞市| 崇州市| 太谷县| 神木县| 衢州市| 天峨县|