Vim 语言、移动和模式详解
Vim Language, Motions, and Modes Explained (2023)

原始链接: https://www.ssp.sh/blog/why-using-neovim-data-engineer-and-writer-2023/

Vim 是一款强大的、基于快捷键的文本编辑器,能显著提高编码和写作效率。不同于传统的编辑器,Vim 使用基于“Vim 语言”的指令,这套语言由各种动作和语法构成。用户可以组合少量指令来执行复杂的编辑操作,从而提高速度和精度。 你无需记忆大量的快捷键,而是学习动词(复制、删除)、宾语(行号、段落)和目标(寄存器)来创建自定义命令。常见的动作包括使用 `hjkl` 键按字、行或字符进行导航。 Vim 拥有不同的模式,包括用于导航和执行命令的普通模式、用于输入的插入模式和用于文本选择的视觉模式。Neovim 是一个现代化的 Vim 版本,使用 Lua 进行配置,因其可扩展性而广受欢迎。LunarVim 提供了一个预配置的 Neovim 体验。虽然 Vim 的初始学习曲线较陡峭,但掌握 Vim 和终端可以极大地提高开发技能,提倡极简主义,并打造个性化的编码环境。

这个Hacker News帖子讨论了Vim文本编辑器的优缺点,Vim以其模态编辑和键盘中心化的方法而闻名。用户们就Vim是否提高了他们的效率展开了辩论,一些人认为其人体工学设计和避免使用鼠标是关键优势。另一些人强调它打开速度快且没有烦人的弹出窗口。一些人提到了Helix和Kakoune等替代方案,它们使用“动作-移动”键绑定,而另一些人则为Vim传统的“移动-动作”键绑定辩护。 几位评论者指出,Vim的价值不仅仅在于速度,还强调了它的可编程性、编写结构化文本的愉悦体验以及它的普遍可用性。一些人指出了最初的学习曲线,但也强调它带来的思维方式的改变非常值得。一个常见的论点是,即使现代IDE提供了类似的功能,Vim仍然具有价值,因为它具有可定制性,并且能够适应用户的操作流程。一些人甚至认为Vim治愈了他们的RSI(腕管综合症)。讨论还涉及到键盘布局和自定义对人体工学效率的重要性。
相关文章
  • (评论) 2024-01-17
  • 无模式 Vim 2024-01-17
  • 理解Vi和Vim的起源和演变 2025-04-18
  • (评论) 2024-08-05
  • 在终端中输入文本很复杂 2024-07-10

  • 原文

    Throughout my time as a developer, I’ve used VS Code, Sublime, Notepad++, TextMate, and others. But shortcuts like cmd(+shift)+end and jumping with option+arrow-keys from word to word needed to be faster at some point.

    I was hitting my limits. Everything I was doing I did decently fast, but I didn’t get any faster.

    I’ve since learned that Vim is the only editor that you get faster using with time.

    Vim is based solely on shortcuts. When I discovered that and played around a bit, I felt numb and a little stupid, having not learned the shortcuts (called Vim language) much earlier in my career.

    I realized there was a keystroke to get to any specific position I wanted to jump. It was like a game, seeing if I could use fewer shortcuts to accomplish a particular edit. It’s where many Vim users get a lot of pleasure from coding and writing. It felt liberating, moving my cursor with the precision of a surgeon.

    Although speed is a smaller benefit, it got me started when I saw others navigating in Vim. After climbing the steep learning curve, it’s still one of the most powerful skills I’ve ever learned in my career, working for a living on a computer.

    Let’s debunk the myth of Vim and learn how it’s possible to remember all the shortcuts using the specific Vim language. We’ll see how to move with vim motions, and I’ll share what I’ve learned so far, and why you might give Vim a try as well.

    Learning the Vim Language

    Lots of things have been said about Vim – how fast it is, how only Linux nerds use it, and that it’s impossible to exit Vim.

    For myself, I fell in love with the “Vim language”. You see, I’m bad at remembering anything and thought that Vim was not for me. But this wasn’t the case for one specific reason: Vim motions and its language.

    I learned that there’s a grammar behind the editor. With it, you express what you want to do first, how many times, and then what you want it to apply.

    Let’s get deeper into Vim and the language behind it.

    How the Vim Language and Motions Work

    Vim has a terrific language or grammar behind its shortcuts. Instead of remembering a thousand shortcuts, you can learn a couple and combine them.

    These are often called the Vim language or Vim motions for moving around. This has nothing to do with the editor yet – these are universal and available in other editors as well.

    For example, there’s VSVim for VSCode, IdeaVim for the JetBrains products, Vintage Mode for Sublime, and so on. But there are also Browser extensions like Vimium or Firenvim, and Gmail even adapted some of Vim’s shortcuts for navigation (jk for moving, g for jumping).

    Everyone who types on a computer eight hours a day should learn the Vim language. Yes, it’s hard in the beginning, but that’s the case with everything new and different. But getting better every day and having more fun coding or writing should be motivation enough. You’re not too busy to learn - you’ll learn as you go.

    /blog/why-using-neovim-data-engineer-and-writer-2023/weel-too-busy.png
    Are you too busy to improve | Image from steenschledermann

    Vim Grammar

    Just as spoken language grammar has verbs, subjects, and objects, so does the Vim language. The grammar has different verbs to begin with. Copying (or yanking) in Vim with y, deleting with d, pasting with p, changing with c, and so on.

    For example, the easiest shortcut is copying a line with yy. In this case, yank is the verb and the second y is a synonym for y_. The y is doubled up which makes it easier to type since it’s a joint operation.

    Next, we can add movements. Each verb takes a subject for their movements. There are lots of movements (more in the next section) – the easiest is with numbers.

    For example, to copy three lines, you add a 3 in front, such as 3yy. You can do that with all verbs, like deleting three lines is 3dd. Another would be { and } to move to the beginning or end of the paragraph, respectively.

    In addition to verbs and subjects, the Vim language also has objects. For example, we can save text into different clipboards (called a register in Vim) with "ay. Here, we copy it into register a, which would be the object. We can paste it again by doing the same but using the verb paste instead of yank "ap.

    There are even adjectives and adverbs with prefixes. Usually, you use a verb and an object. But instead of going down three lines with 3J, which joins the following three lines, you could add d5}, which means “delete from the current line through the end of the fifth paragraph down from here.”

    For me, the most magical thing about Vim is how you navigate and edit text – and it still has nothing to do with the editor.

    Sure Vim was the one that introduced and perfected these actions, but again – you can get them anywhere else. This goes deep into the Vim language, yet we still need to touch on the editor. This is important to know.

    I hope you’ve started seeing the power of such patterns, though. With a couple of verbs and objects, you can already know hundreds of combinations without memorizing each one individually.

    You can watch a video on Mastering the Vim Language or read a full exposition of the Vim language on this terrific StackOverflow comment.

    Vim Motions

    Vim motions are how you navigate, whether you navigate to the end of the word or back to the start of the document – these are all motions.

    These are the first things you start learning (and hating) when learning Vim. They’re extra hard to figure out initially, but they’re something you’ll want to use everywhere when you get used to them.

    Instead of using arrow keys, Vim uses jk to move down and up and hl to move left and right. The main idea is to use the keys your right hand naturally rests on. You do not need to move your hands or even fingers for navigation.

    Again, this seems like a small thing, but once you’ve learned it, you know why everyone is telling you about it.

    Some common motions are:

    1
    2
    3
    4
    5
    6
    
    h,j,k,l - left, down, up, right
    w - to start of next word
    b - to start of previous word
    e - to end of word
    $ - to end of line
    ^ - to start of line
    

    You can find the most important motions to start with in this cheatsheet:

    /blog/why-using-neovim-data-engineer-and-writer-2023/vim-language-cheetsheet.png
    Vim Command Cheat Sheet from Cloud Guru

    How to Use Vim Modes (normal, insert, visual, and command)

    Modes are another thing that might get you confused at the beginning.

    When you launch Vim, you are not typing what you click on your keyboard as you are not in the “insert” mode that you’re likely familiar with from other editors. Instead, the normal mode you are in lets you do the commands explained in the above Vim language and motions.

    Vim is the only editor that optimizes editing text instead of writing from a blank page.

    /blog/why-using-neovim-data-engineer-and-writer-2023/vim-modes.png
    Three modes illustrated (escape mode being the command mode) | Image from Geekforgeeks

    That’s another reason Vim makes you so efficient: you have different modes for each phase of your current work or task.

    • Normal mode is for reading code and navigating quickly.
    • Insert mode is for when you want to add some code or text.
    • Visual mode is unique, the same as highlighting text with the mouse, but with the above Vim motions.
    • And command mode is the powerhouse, where you can type Linux commands such as formatting a JSON file with :%!jq (whereas jq is a command line tool installed on your machine) and execute them within Vim. This is also where you can use Vim commands such as :sort for sorting your files.

    I could go on here, but I want to dive into the editor itself now and explore why I learned it initially and how to get started.

    Introduction to Vim the Editor (Neovim, Lunarvim, and Helix)

    So what is Vim the editor, then? It started with the simple vi editor, a basic editor that implements the Vim language and can edit text. It’s a little like Notepad++, which you might use on Windows, but without a mouse and context menu.

    Vim is simply an improved version of Vi with more features.

    /blog/why-using-neovim-data-engineer-and-writer-2023/vi-vs-vim.png
    Differences between Vi and Vim | Image by Linuxiac

    Today there is even a newer version of Vim called Neovim. This version is super popular, and I’ve started using Neovim as well. Compared to Vim, Neovim uses Lua, an actual programming language, to configure and extend the editor. This makes writing plugins and configuring Neovim easier compared to Vim’s native Vimscript.

    Neovim is a great place to start learning Vim today, as it has so many awesome plugins. Neovim also won the most loved IDE on the StackOverflow survey a couple of times, last in 2022.

    There is also an editor called Helix built in Rust, but it has minor deviations from the Vim language, which make it a less optimal place to start.

    If you want to get started without needing to know anything about Neovim and spending hours on configurations, you can begin with LunarVim. It’s a distro with all the features you know from VS Code already included.

    Suppose you are comfortable with the terminal and realize you want to change the editor to your liking. In that case, you can kickstart your journey with a simple single-file configuration with many explanations that will work out of the box. You can also learn each config by opening the single config file.

    Why I Learned Vim

    Using the standard input method we use in our editors daily, we will eventually stagnate at a certain level. Sure, you can use cmd+arrow-keys (on a Mac) to jump to the beginning of a line or option+arrow-keys to jump between words instead of characters.

    But what happens once you’ve mastered that? What if you need to change something in the middle of a sentence? There is no other way to jump several times with this option, or you move your hands away each time to reach the mouse to click on the exact spot.

    One day, I saw a coworker work in Vim, and everything clicked. The Vim language and motions were the things I needed all along. So I installed the VS Code plugin, watched a couple of YouTube videos, and started my journey to learn the basic movements.

    I also love learning new things and, even better, I’m always looking for ways to make me more productive 😉.

    But as many of you might have experienced, the hardest part of learning Vim is getting started. The initial learning curve is very steep. Below is an illustration that shows this :).

    /blog/why-using-neovim-data-engineer-and-writer-2023/vim-learning-curve.png
    The learning curve for text editors. Although funny, very accurate | Image from Why I Love Using Vim To Write Code

    It took me also two or three times trying to start learning Vim until I fully grasped it. I switched back and forth. As well as having to get work done, it is sometimes hard to switch entirely from one day to another. But I just loved learning all the movements, and I knew it would make me faster after a short time.

    Why I Love Vim

    I have been using Vim for only eight months, and coding with Python for around six years. I’ve been using other code editors since my career started in 2003. Each editor I used had its strengths and its appeal. But I never experienced such efficiency gains as I have with Vim.

    In the end, use the editor that works best for you. Personally, I want my editor to help me work as fast as possible, especially since I use it daily. Investing a bunch of time learning Vim is necessary, but it pays off over time. That’s the whole point with Vim and especially the Vim language.

    An underrated skill in general among programmers is using the terminal. By learning your editor, especially with Vim, you will naturally learn more about the terminal and improve your Linux skills (reverse search, lazy git, Tmux, and many more).

    Before Vim, I only used the terminal if I had to. I googled everything, and today, I use the terminal with its helpful manuals whenever I can.

    Sometimes I’m surprised by myself as well, and it’s super nerdy – but it’s so effective. I’ve become a much better developer since starting to get comfortable with Vim.

    Tweaking and optimizing Vim can take hours and days, and it’s unavoidable in the beginning. But after a while, your dotfiles mature, and you start changing things less. You will also get much faster at trying out a new plugin or adding a remap.

    Also, Vim is fun! Working in Neovim is one of the highlights of my everyday work. Improving your text editor and making it your own – maybe in ways no one else has optimized – is awesome.

    For example, I write a lot, so I optimized for writing markdown and programming in Python. That’s what adds a lot to my happiness as a coder.

    PDE (Personalized Development Environment

    Because of all this, TJ DeVries calls Neovim a PDE (Personalized Development Environment), not “just” an IDE. You can learn more about this in ThePrimeagen’s truly inspiring Vim videos and learn why he used Vim in 2022.

    Vim also manifested minimalism more in me. I used the terminal instead of fancy GUIs and plain text files for clarity, freedom, blazingly fast shortcuts, no vendor lock-in, and staying in the Flow with the content in front.

    Vim changed not only my workflow but how I was able to edit at the speed of thought. Instead of thinking, “I want to edit that word”, my fingers jump to that word and change it with a few keystrokes.

    Vim for Data Engineering

    My data engineering workflow uses Neovim with the LSP (Language Server Protocol) pyright installed with mason. There’s much more with Tmux, but you can find all the details on dotfiles/nvim.

    /blog/why-using-neovim-data-engineer-and-writer-2023/vim-mason-install.png
    Installing Pyright with Mason

    Vim for Writing

    I’m still using Obsidian a lot more for writing (see more on my PKM workflow) due to its additional features of supported images, backlinks, graphs, and plugins specified for note-taking, such as ReadWise (syncing my highlight from books, and tweets), Dataview (using notes as a database), Excalidraw (drawing with Markdown format), Templates, and so on.

    Nevertheless, I write more and more in Neovim. For now, I use ZenMode (for centering the text), Grammarly (for linting grammar), write-good (linting grammar), and specifically Obsidian.nvim (follow backlinks, and so on.). You find all details in my dotfiles.

    In Obsidian, I use the Vim mode Obsidian .vimrc and map most hotkeys to my Vim settings. At the same time, I’ve been writing more and more in Neovim and have been progressively moving over to full Neovim.

    Plugins such as Telescope and simple grep features that I use for coding work very well with Markdown. Here are some clips showing what is possible in an excellent talk about Writing, Editing, and World-Building at the speed of thought with Vim.

    Why You Should Learn Vim, Too

    When I heard about Vim, I thought that it was only for software engineers and Linux nerds 😅. I never thought I was going to use it as well. But how did I get into it?

    I’ve already shared some reasons why I love Vim. But it really changed all my workflows, not only as a developer but also how I surf the internet, write, navigate, and use tools. I search for a Vim mode in any application I use.

    But if you don’t enjoy fiddling and optimizing your workflow, and if you don’t write or code for a living, Vim might not be for you. Start with your current editor and activate the Vim mode before you do anything with Vim. It will save you a lot of frustration, trust me 😅.

    How to Get Started Using Vim

    There are many good resources that’ll help you get started with Vim. The easiest is to type vimtutor into your terminal, which is an interactive tutorial for Vim.

    I’ve already linked a few YouTube videos above – especially check out ThePrimagen starting with Vim As Your Editor or Why even bother with Vim or Neovim in 2022.

    I started with Ben Awads’s Vim tutorial back then. An excellent Lecture: Editors (Vim) (2020)Mastering the Vim Language. I also collect a small playlist on YouTube with Vim content. A big inspiration also dev workflow using Tmux and Vim from Takuya.

    Wrapping Up

    We have learned that Vim is a powerful text editor popular among developers. It’s based on shortcuts, called the Vim language, which can make coding and writing faster and more efficient.

    With Vim, you can jump to any specific text position and rapidly make precise edits. While learning Vim can be challenging, it is well worth the effort in the long run as it will improve your productivity and bring joy to your coding experience.

    If you want to go further, read my follow-up on My Vim-Verse, or try Tmux, which plays well with Vim. You could even go one level deeper, which is a dedicated keyboard layout such as Dvorak or Halmak (which I started learning at some point). Or buy a fancy ergonomic keyboard or build one yourself.

    Thanks for reading this far. I hope you enjoyed this article. I’m looking forward to hearing your comments and experiences.


    Republished on FreeCodeCamp.
    联系我们 contact @ memedata.com