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

  • Mastering Vim
  • Ruslan Osipov
  • 455字
  • 2021-06-10 18:51:28

Creating, deleting, and navigating windows

Let's give working with windows a shot. Open animal_farm.py (either from a command line by running $ vim animal_farm.py or from Vim with :e animal_farm.py).

Open one of our files in a split window:

:split animals/cat.py
You can shorten this command to :sp.

You can see that animals/cat.py was opened above the current file and that your cursor was placed there:

You can split the window vertically as well by running the following code:

:vsplit farm.py

As you can see, this creates another window in a vertical split (your cursor is now moved to the new window):

:vs  is a shorter version of the   :vsplit  command.

You can combine the :split and :vsplit commands indefinitely to create as many windows as you need.

All of the commands you've learned so far will function as usual within this window, including changing buffers. To move between the windows, use Ctrl w, followed by a directional key: h, j, k, or l. Arrow keys work as well.

If you use windows a lot, you might benefit from binding Ctrl h to go to the split to the left,  Ctrl j to go to the split at the bottom, and so on. Add the following to your .vimrc file:

" Fast split navigation with <Ctrl> + hjkl.
noremap <c-h> <c-w><c-h>
noremap <c-j> <c-w><c-j>
noremap <c-k> <c-w><c-k>
noremap <c-l> <c-w><c-l>

Give it a shot: Ctrl  followed by j will move you to the window below, and Ctrl w, k will move the cursor back up.

You can close the split window in one of the following ways:

  • Ctrl + w, followed by q will close the current window

  • :q will close the window and unload the buffer; however, this will close Vim if you only have one window open
  • :bd will delete the current buffer and close the current window
  • Ctrl + w, followed by o (or the :only or :on command) will close all windows except for the current one

When you have multiple windows open, you can quit them all and exit Vim by executing :qa. This can be combined with the  :w command to save every open file and quit: :wqa.

If you want to close a buffer without closing the window it's in, you can add the following command to your .vimrc file:

command! Bd :bp | :sp | :bn | :bd  " Close buffer without closing window.

You'll be able to use :Bd to close the buffer while keeping a split window open.

主站蜘蛛池模板: 乌恰县| 广丰县| 扬州市| 尖扎县| 房山区| 南木林县| 望都县| 罗平县| 乳山市| 焉耆| 漳州市| 奈曼旗| 会泽县| 万全县| 河南省| 买车| 井冈山市| 元谋县| 翁牛特旗| 桑日县| 菏泽市| 阳山县| 滦平县| 图们市| 安龙县| 定远县| 哈尔滨市| 浦北县| 富顺县| 谢通门县| 大姚县| 连州市| 鹤壁市| 江陵县| 南澳县| 岑溪市| 兴文县| 沾益县| 仁化县| 溧水县| 崇文区|