惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
WordPress大学
WordPress大学
I
InfoQ
Last Week in AI
Last Week in AI
Vercel News
Vercel News
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
腾讯CDC
D
DataBreaches.Net
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
Fast Movement and Navigation Inside Vim or Neovim
2019-06-26 · via jdhao's digital space

Introduction#

One key spirit of Vim is to accomplish something in as fewer key strokes as possible. When we repeat a command several times to perform an action, we need to ask ourselves if there is a better way to do it. Only in this way, can we make progress in mastering Vim. For example, to delete five lines of texts, if we press dd five times, we are certainly doing it the wrong way. After looking up the Vim manual, we know that we can use 5dd or d4j: only three keystrokes are needed instead of ten!

The other day, I saw a post in a forum asking for help on how to quickly move the cursor inside a buffer. After using Neovim for nearly a year, I would like to share what I have learnt about fast movement and navigation inside Neovim/Vim.

There are hundreds or thousands of articles about basic Vim command to move the cursor. You can check this cheatsheet to quickly get an overview.

Here I want to to talk something more advanced, at least in my opinion. Unless otherwise notified, all the commands below are performed in normal mode.

Quickly go to a line#

I have observed that one common and inefficient behaviour of Vim novices is that they often do not know that most Vim commands can be prefixed with a number to avoid manual repeat.

In this part, I will talk about how to quickly go to line. Beginners often press j or k non-stop in order to navigate to a specific line below or above the current cursor line. This is a terrible and highly inefficient way to go to where you want.

To go to a line, first press the line number, then press gg or G. For example, to go to line 87, press either 87gg or 87G.

Relative number#

We can also move the cursor relatively based on the distance between the destination line and current line. To go to ten lines below or above, we can press 10j or 10k.

This requires less key strokes once a file reaches hundreds of lines. The only inconvenience is that we have to manually calculate the distance between the destination line and cursor line. In order to be more efficient, I strongly suggest you turning on both the number and relativenumber options:

set number
set relativenumber

After setting these two options, the cursor line will show its absolute line number, while the other lines will show the relative distance to the current line. No more manual calculation needed!

For more settings about line number in Neovim, see my other post.

Precise cursor movement#

Vim provides the f, t or F, T command to jump forward or backward to a character in current line, and you can repeat the action by pressing , or ; (see :h , or :h ;). While these commands are useful, they are not powerful enough to be of real value:

  • The movement is restricted to current line.
  • There are possibly many repetitions since you can only provide one character. As a result, you may need to press ; several times to go to where you want.

vim-sneak1 solves these two issues neatly. With vim-sneak, you can go anywhere your eyes can see in just four key strokes in most cases. It is one of the most amazing plugins I have ever seen.

After installing this plugin, add the following minimal settings to your configuration:

let g:sneak#label = 1
nmap f <Plug>Sneak_s
nmap F <Plug>Sneak_S

The above settings will map f and F to search forward and backward respectively and use the sneak label mode.

For example, if you want to go to the character i that is followed by m, first press f, then press im. All occurrences of im will be highlighted and given a label (see image below).

Press the character in a label, the cursor will jump to that location, which is really amazing.

Quickly go to a previous edit position#

When I am writing Python source files, a pattern I notice is that I often forget to import a package or module when I need to use it. My usual work flow will be like:

  • Change to normal mode with ESC.
  • Go to the top of the source file and add the import statement I need.
  • Make other necessary adjusts.
  • Scroll down the source file and find the line I have been editing previously.
  • Go to the precise location where I am editing.

The annoyance is that it will take some time to go to the exact position we are editing before. This is when marks come to help2. A mark denotes a particular position in the buffer. The following is a simple overview to use marks:

  • ma: create a mark named a (can be a character in a-z)
  • `a: go the exact position denoted by mark a
  • 'a: go to the line denoted by mark a

With marks, we can simplify our workflow. If we want to move to other places but want to go back later, we can simply create a mark in this position. After finishing work in other locations, we can directly go to the previous location with its mark label.

The native functionality provided by marks are powerful but not very convenient:

  • If you create several marks, you may not remember the correspondence between buffer positions and mark labels.
  • You have to manually select an unused label for newly created marks. It is also cumbersome to delete a mark.

I recommend using plugin vim-signature to manage your marks. This plugin will show the mark labels in the sign column3 so that we can easily go to a marked position. It also provides other commands to manage marks.

Switching buffers quickly#

When we are working on a large projects, we may open several files at the same time. We can go to a buffer quickly if we know its buffer index. To show the buffer index of opened files, use :ls. An example output is:

2 %a   "movement_navigation_inside_nvim.md" line 153
5 #    "~/.config/nvim/init.vim"      line 758

The number in the first column is the buffer index. To go to a buffer, first press the buffer index, then press Ctrl + 6.

It works but are still not convenient enough, because we have to first find the buffer index using :ls command. My solution is to use vim-airline, which can be configured to show the buffer index on the tabline along with the file names. After installing Vim-airline, add the following settings to your configuration:

let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#buffer_nr_show = 1

Restart Nvim and you will find that each buffer has a number on the left of the file name (see image below).

Conclusion#

In this post, I shared several ways for faster movement and navigation inside Neovim/Vim. I hope that you can apply the techniques here to improve your efficiency and save your time. Hopefully, you may share your own ideas on how to further improve efficiency in the comment below.