207

I am using Vim for windows installed in Unix mode. Thanks to this site I now use the gf command to go to a file under the cursor.

I'm looking for a command to either:

  1. return to the previous file (similar to Ctrl+T for ctags), or
  2. remap gf to automatically launch the new file in a new window.
ib.
  • 27,830
  • 11
  • 80
  • 100
JayG
  • 4,339
  • 3
  • 23
  • 19

11 Answers11

322

I use Ctrl-O

ib.
  • 27,830
  • 11
  • 80
  • 100
jop
  • 82,837
  • 10
  • 55
  • 52
  • 46
    CTRL-O to go to an older position in jump list, or CTRL-I to go to a newer position. – Sébastien RoccaSerra Sep 26 '08 at 10:57
  • 5
    @g33kz0r the mnemonic would be O = OUT, I = IN => Ctrl - O brings you out, Ctrl brings you in. If every jump is like going through a door, that is. – kronn Oct 14 '15 at 11:36
  • CTRL-O will jump to last visited position, this is annoying when the new file has been searched in. I prefer Shift-Ctrl-6 (in my case Ctrl-6 don't work) – Adriano Jan 11 '18 at 09:11
  • I think paxdiablo's answer is better. :e# goes just one step back(even when we opend file or directory). ctrl-o goes to the first file. – Chan Kim Jan 23 '21 at 02:08
83

I frequently use Ctrl-6 for this.

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

ib.
  • 27,830
  • 11
  • 80
  • 100
Dominic Dos Santos
  • 2,623
  • 2
  • 18
  • 30
53

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.

Roman Odaisky
  • 2,811
  • 22
  • 26
  • 7
    tab is more convenient than buffer for me, and using `nnoremap gf gf` to open in a new tab may be better. – Searene Feb 08 '13 at 01:47
15

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

ib.
  • 27,830
  • 11
  • 80
  • 100
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
14

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

Ram
  • 157
  • 1
  • 2
  • 1
    I had two windows open in a tab and this just opened the second window in the first instead of jumping back to the previous buffer. – Kenny Evitt Sep 24 '15 at 19:24
12

Ctrl-Shift-6 is one.

:e#↲ is another.

AEM
  • 1,354
  • 8
  • 20
  • 30
tzot
  • 92,761
  • 29
  • 141
  • 204
  • 10
    The shift is not necessary. Stop abusing your fingers. – Zathrus Sep 25 '08 at 14:42
  • 6
    Um. I'm kind of an old timer. Ctrl-6 wouldn't work with vi on the Wyse terminal I used to have at work years ago. See, some things remain etched. And not using Shift in this combo would hardly make a difference to my total finger abuse. Hey, at least you tried your best. HTH. HAND. – tzot Sep 25 '08 at 23:21
  • 1
    This switches to what's called the [*alternate file*](http://vimdoc.sourceforge.net/htmldoc/editing.html#alternate-file). – jpaugh Jul 03 '18 at 13:57
8

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.

AEM
  • 1,354
  • 8
  • 20
  • 30
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>`
codebunny
  • 2,829
  • 3
  • 22
  • 24
4

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+ or Alt+.

The complete documentation on mappings is here:

:help map.txt
AEM
  • 1,354
  • 8
  • 20
  • 30
Oli
  • 15,345
  • 8
  • 30
  • 36
2

See :help alternate-file.

Bruno De Fraine
  • 45,466
  • 8
  • 54
  • 65
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>