You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vim中如何快速跳转到最后一行非空行?

How to Jump to the Last Non-Empty Line in Vim Without Counting Lines

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 .vimrc file to save keystrokes. For example:

    nnoremap <leader>l G?^.<CR>
    

    Now, pressing your leader key (default is \) plus l will 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:35:01