197

我正在将 Vim 用于以 Unix 模式安装的 Windows。感谢这个站点,我现在使用gf命令转到光标下的文件。

我正在寻找一个命令:

  1. 返回上一个文件(类似于 ctags 的Ctrl+ T),或
  2. 重新映射gf 以在新窗口中自动启动新文件。
4

11 回答 11

311

I use Ctrl-O

于 2008-09-25T14:23:50.343 回答
78

I frequently use Ctrl-6 for this.

It's handy because it allows me to quickly jump back and forth between the two files.

于 2008-09-25T20:21:02.887 回答
48

You might want to use CTRL-W gf to open the file in a new tab.

You can close the newly opened file as always with :bd, or use CTRL-6 and other usual ways of changing buffers.

于 2008-09-26T09:42:49.807 回答
15

Just use :e# followed by Enter - that basically says to edit the last (most recent) file.

于 2008-09-25T14:24:33.057 回答
14

Use gf to descend into a file and use :bf to get back

于 2010-05-05T10:08:29.197 回答
10
于 2008-09-25T14:21:21.410 回答
6

I got CTRL-Wf to work.
It's quite depressing that I've spent so long perfecting maps for these commands only to discover that there are built-in versions.

于 2008-09-26T10:51:27.007 回答
5

I don't know the answer to part 2 of your question, but I can help with part 1. Use

:e#

Vim maintains a list of files (buffers) that it's editing. If you type

:buffers

it will list all the files you are currently editing. The file in that list with a % beside it is the current file. The one with the # beside it is the alternate file. :e# will switch between the current and alternate file. Rather than type that much, I map F2 to :e# so I can easily flip between the current and alternate files. I map the command to F2 by adding this to .vimrc

nmap `<F2> :e#<CR>`
于 2008-09-25T15:12:10.663 回答
2

See :help alternate-file.

于 2008-09-25T14:36:10.890 回答
2

When you open a new file (with gf or :n or another command) the old file remains in a buffer list. You can list open files with :ls

If you want to navigate easily between buffers in vim, you can create a mapping like this:

nmap <M-LEFT> :bN<cr>
nmap <M-RIGHT> :bn<cr>

Now you can switch between buffers with Alt+←</kbd> or Alt+→</kbd>.

The complete documentation on mappings is here:

:help map.txt
于 2008-09-26T07:55:18.523 回答
0

I haven't looked at your gf command but I imagine it uses the :e or :find command.
Assuming that this is correct, simply replace the :e or :find with :new (or :vnew for a vertical split) and the file will open in a new window instead of the same one.

e.g.

"Switch between header and cpp
nmap ,s :find %:t:r.cpp<CR>
nmap ,S :new %:t:r.cpp<CR>
nmap ,h :find %:t:r.h<CR>
nmap ,H :new %:t:r.h<CR>
nmap ,F :new =expand("<cfile>:t")<CR><CR>
nmap ,d :new =expand("<cfile>")<CR><CR> 

于 2008-09-26T09:29:12.430 回答