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

推荐订阅源

MyScale Blog
MyScale Blog
博客园 - 叶小钗
小众软件
小众软件
宝玉的分享
宝玉的分享
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
Last Week in AI
Last Week in AI
量子位
腾讯CDC
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - Franky
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
有赞技术团队
有赞技术团队
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Visual Studio Blog
爱范儿
爱范儿
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
雷峰网
雷峰网
V
V2EX
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
Vercel News
Vercel News
L
LINUX DO - 热门话题
The Hacker News
The Hacker News
The Register - Security
The Register - Security
IT之家
IT之家
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
人人都是产品经理
人人都是产品经理
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
N
News | PayPal Newsroom
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Attack and Defense Labs
Attack and Defense Labs
S
Securelist
T
The Blog of Author Tim Ferriss
GbyAI
GbyAI
Microsoft Security Blog
Microsoft Security Blog
H
Hacker News: Front Page
博客园 - 聂微东
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Project Zero
Project Zero
I
InfoQ
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
PCI Perspectives
PCI Perspectives

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 Mintty Tips and Configurations Generating Table of Contents for Markdown with Tagbar Convert Python Script to Exe on Windows with Pyinstaller Ubuntu on Windows Missing after Windows Update 使用代理加速 Mac 终端下载速度 My Experience with Several Zsh Plugin Managers 深圳租房小记 How to Install zplug inside Docker Container Why don't settings inside bashrc or bash_profile take effect? Setting Up Locale in Linux 谷歌 Adsense 申请及在 Hugo 中的配置 How to Write Algorithm Pseudo Code in LaTeX Nifty Nvim Techniques That Make My Life Easier -- Series 4 A Few Grammar Questions in Writing How to Read and Write Images with Unicode Paths in OpenCV Creating A Professional Table in LaTeX with booktabs How to Create Proper Folding for Vim/Nvim Configuration Linux Tips and Tricks -- s1 JPEG Image Orientation and Exif How Do I Show the Current File Path In Neovim? JPEG Image Quality in PIL Difference between view, reshape, transpose and permute in PyTorch Convert PIL or OpenCV Image to Bytes without Saving to Disk Unintuitive Behaviour of Case Sensitivity in Python glob Binding Keys in Zsh 几把机械键盘试用体验 Nvim Autocompletion with Deoplete Converting Markdown to Beautiful PDF with Pandoc Exclusive and Inclusive Motion in Neovim/Vim Nifty Nvim Techniques Which Make My Life Easier -- Series 3 Why Doesn't Jedi Autocompletion Work for Some Methods Vim-like Editing inside Browser Markdown 生成 HTML 时汉字之间出现多余空格问题 小米 9 安装谷歌商店(Google Play Store)与相关配置 Create Mappings That Take A Count in Neovim Spell Checking in Nvim English Words Completion inside Neovim/Vim How to Use Python Inside Vim Script with Neovim Nifty Little Nvim Techniques to Make My Life Easier -- Series 2 Setting up Ultisnips for Neovim Mac 上罗技 M590 鼠标设置 Nifty Little Nvim Techniques to Make My Life Easier -- Series 1 A Complete Guide on Writing LaTeX with Vimtex in Neovim Manipulating Images with Alpha Channels in Pillow Sublime Text Regular Expression Cheat Sheet Cropping Rotated Rectangles from Image with OpenCV Boosting Your Productivity on Terminal with Zsh and Plugins 最新版 Rime 输入法使用 (2022 更新) Display Image with Pillow inside Ubuntu on Windows Faster Directory Navigation with z.lua Cmder Advanced Configurations Nvim-qt Settings on Windows 10 Tmux Plugin Install and Management How to Debug Python Code in Terminal Markdown Writing and Previewing in Neovim -- A Complete Guide Line Number Settings for More Efficient Movement in Neovim 两个大规模中文语料库介绍以及处理 Windows 系统下几款程序员不可不用的神器 我的 2018 阅读清单 A Complete Guide to Neovim Configuration for Python Development How Is Newline Handled in Python and Various Editors? Two Issues Related to ImageFont Module in PIL 在 Listary 中调用 GoldenDict 或欧路词典查单词 Reading and Writing Text Files on Windows The Mathematics behind Font Shapes --- Bézier Curves and More 快速识别图片字体:字体识别工具介绍 Deoplete Failed to Load at Startup after Updating Python neovim Package What Is The Difference between pip, pip3 and pip3.6 Shipped with Anaconda3? Windows 10 系统下 Neovim 安装与配置 How to Use shutil.move() on Windows and Linux
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.