Vim中如何快速跳转到最后一行非空行?
Great question—counting line numbers manually is such a tedious workaround, especially when working with files of varying lengths. Here are a few straightforward ways to jump straight to the last non-empty line, no line counting needed:
Single Ex Command: Type
:g/./$and press Enter. This command searches for all lines containing at least one character (./) and jumps directly to the last occurrence of such a line. Perfect for a one-off use when you don’t want to switch modes multiple times.Normal Mode Sequence: Stay in normal mode and use
G(jumps to the very last line of the file) followed by?^.(searches backwards for the first line that starts with any character—meaning it’s non-empty). Hit Enter, and you’ll land right on the last non-empty line. If the final line is already non-empty, this just keeps you there.Custom Mapping (for frequent use): If you do this often, add a shortcut to your
.vimrcfile to save keystrokes. For example:nnoremap <leader>l G?^.<CR>Now, pressing your leader key (default is
\) pluslwill instantly take you to the last non-empty line. Adjust the key combination to whatever feels natural for you.
Once you’re on that line, just use o as usual to insert a new line below and start typing your imports—no more guessing line numbers!
内容的提问来源于stack exchange,提问作者avriis

