Skip to content

09.Navigate Between Files with Jumps

Motions allow us to move around within a file Jumps are similar, except that they can also move us between files

Vim provides a couple of commands that turn keywords in the document into a warmhole, allowing us to jump quickly from one part of our codebase to another. That might seem disorienting at first, but Vim always traces our path by leaving a trail that we can easily follow to get back to where we came from.

56. Traverse the Jump List

Vim records our location before and after making a jump and provides a couple of commands for retracing our steps.

jump list :jumps

  • <C-o> back button

  • <C-i> forward button

  • motions move around within a file

  • jumps move between files

  • some motions are also classified as jumps

Any command that changes the active file for the current window can be described as a jump. In the jump list, Vim records the cursor location before and after running such a command.

  • :edit command to open a new file
  • [count]G moving directly to a line number
  • The sentence-wise and paragraph-wise motions are jumps

This table summarizes a selection of jumps:

CommandEffect
[count]GJump to a line number
/pattern<CR>/?pattern<CR>/n/NJump to next/previous occurrence of pattern
%Jump to match parenthesis
(/)Jump to start of previous/next sentence
{/}Jump to start of previous/next paragraph
H/M/LJump to top/middle/bottom of screen
gfJump to file name under the cursor
<C-]Jump to definition of keyword under the cursor
'{mark}/`{mark}Jump to a mark

Test the gf command with below:

  1. Place cursor on the the file name
  2. Run gf in Normal mode
  3. Vim will open this config file
  4. Use <C-o> to back out
  5. Use <C-i> to back in

C:\Users\Alowr\AppData\Local\nvim\lua\alowree\core\keymaps.lua

Test this command gf once again after you introduce some more colorschemes from the plugin.

Under some unknown conditions, sometimes (mostly likely after you've done some motions) when you run gf command in Neovim, you'll get the following alert message:

E447: Can't find file "\Users\Alowr\AppData\Local\nvim\lua\alowree\core\keymaps.lua" in path

Problem 3

Under certain circumstances, the gf inside Neovim does not work as expected and triggers error message E447: Can't find file "\Users\Alowr\AppData\Local\nvim\lua\alowree\plugins\nvim-tree.lua" in path. This has something to do with the escape of character \.

Rewrite \ inside path with /: C:/Users/Alowr/AppData/Local/nvim/lua/alowree/plugins/nvim-tree.lua, and gf works just fine. Rewrite \ inside path with \\: C:\\Users\\lowr\\AppData\\Local\\nvim\\lua\\alowree\\plugins\\nvim-tree.lua, and gf works just fine.

By comparison, the gf inside Vim works on all three representations.

See :h gf and :h path.

57. Traverse the Change List

58. Jump to the Filename Under the Cursor

Vim treats filenames in our documents as a kind of hyperlink. When configured properly, we can use the gf command to go the filename under the cursor.

Test 1: the gf command

  1. Place cursor on the the file name
  2. Run gf in Normal mode
  3. Vim will open this config file
  4. Use <C-o> to back out
  5. Use <C-i> to back in

Test 2: the <C-w> gf command

  1. open the file under curso in a new tab

C:\Users\Alowr\AppData\Local\nvim\lua\alowree\core\keymaps.lua

Test this command gf once again after you introduce some more colorschemes from the plugin.

Under some unknown conditions, sometimes (mostly likely after you've done some motions) when you run gf command in Neovim, you'll get the following alert message:

E447: Can't find file "\Users\Alowr\AppData\Local\nvim\lua\alowree\core\keymaps.lua" in path

Problem 3

Under certain circumstances, the gf inside Neovim does not work as expected and triggers error message E447: Can't find file "\Users\Alowr\AppData\Local\nvim\lua\alowree\plugins\nvim-tree.lua" in path. This has something to do with the escape of character \.

Rewrite \ inside path with /: C:/Users/Alowr/AppData/Local/nvim/lua/alowree/plugins/nvim-tree.lua, and gf works just fine. Rewrite \ inside path with \\: C:\\Users\\lowr\\AppData\\Local\\nvim\\lua\\alowree\\plugins\\nvim-tree.lua, and gf works just fine.

By comparison, the gf inside Vim works on all three representations.

See :h gf and :h path.

Specifiy a File Extension

See :h 'suffixesadd'

Specifiy the Directories to Look Inside

See :h 'path'

Tip 103,Configure Vim to Work with ctagesTip 104,Navigate Keyword Definitions with Vim's Tag Navigation Commands

While the jump list and change list are like breadcrumb trails that allow us to retrace our steps, the gf and <C-]> commands provide wormholes that transport us from one part of our codebase to another.

Question

How to quickly add the filename into the current buffer?

To yank from the register % from the Insert mode: <C-r>%

  • % 全路径的文件名
  • %:h 全路径,不包括文件名。如 D:\folder\hello\world.c 则值为 D:\folder\hello
  • %:t 文件名的尾部。例如 ../path/test.c 就会为 test.c
  • %:r 无扩展名的文件名。例如 ../path/test 就会成为 test
  • %:e 扩展名

59. Snap Between Files Using Global Marks

A global mark is a kind of bookmark that allows us to jump between files. Marks can be especially useful for snapping back to a file after exploring a codebase.

The m{letter} command allows us to create a mark at the current cursor position (:h m). Lowercase letters create marks that are local to a buffer, whereas uppercase letters create global marks. Having set a mark, we can snap our cursor back to it with the `{letter} command (:h `).

Set a Global Mark Before Goging Code Diving

Global marks can be specifically useful when we need to browse through a set of files and then quickly snap back to where we started.

Remember, you can set up to twenty-six global marks, which is more than you'll ever need. Use them liberally; set a global mark any time you see something that you might want to snap back to later.

Use case

  1. Open the keymaps config file in path C:\Users\Alowr\AppData\Local\nvim\lua\alowree\core\keymaps.lua, add a line of comment -- C:\Users\Alowr\AppData\Local\nvim\lua\alowree\core\keymaps.lua to the first line to indicate its path, and then save the file
  2. On this newly added first line, run mK to leave a global mark
  3. From any project folder within Vim, run `K to pull up the keymaps config file

The key message is that once a global mark is set, Vim will remember it, across folders and files, even if you quit and re-enter Vim. Once set, the global mark will always be there until it is overwritten.

Marks and positions

  • :marks - list of marks
  • ma - set current position for mark A
  • `a - jump to position of mark A
  • y`a - yank text to position of mark A
  • `0 - go to the position where Vim was previously exited
  • `" - go to the position when last editing this file
  • `. - go to the position of the last change in this file
  • `` - go to the position before the last jump
  • :ju[mps] - list of jumps
  • Ctrl + i - go to newer position in jump list
  • Ctrl + o - go to older position in jump list
  • :changes - list of changes
  • g, - go to newer position in change list
  • g; - go to older position in change list
  • Ctrl + ] - jump to the tag under cursor

TIP

To jump to a mark you can either use a backtick (`) or an apostrophe ('). Using an apostrophe jumps to the beginning (first non-blank) of the line holding the mark.