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 buttonmotions 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:
Command | Effect |
---|---|
[count]G | Jump to a line number |
/pattern<CR> /?pattern<CR> /n /N | Jump 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 /L | Jump to top/middle/bottom of screen |
gf | Jump 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:
- Place cursor on the the file name
- Run
gf
in Normal mode - Vim will open this config file
- Use
<C-o>
to back out - 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
- Place cursor on the the file name
- Run
gf
in Normal mode - Vim will open this config file
- Use
<C-o>
to back out - Use
<C-i>
to back in
Test 2: the <C-w> gf
command
- 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
- 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 - On this newly added first line, run
mK
to leave a global mark - 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 marksma
- set current position for mark A`a
- jump to position of mark Ay`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 jumpsCtrl
+i
- go to newer position in jump listCtrl
+o
- go to older position in jump list:changes
- list of changesg,
- go to newer position in change listg;
- go to older position in change listCtrl
+]
- 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.