Notepad/enter/Coding Tips (Classical)/Terminal Tips/CLI Tools/CLI Tool Collection/Vim/Vim.md

4.7 KiB
Raw Blame History

About Vim

I was extremely surprised I didn't set up a note about my default and OG editor of choice, vim. It is what has helped long term become very fast at typing and in general for understanding the power of just using whatever editor you are most comfortable with at the end of the day. Vim is what I used when I was editing t things on a chromebook and it is still what I am using to this day. Interact with a Vim tutorial on Open Vim. Or open up the vim tutorial


There are many purposes to vim, you can use it for:

  • typing up your math notes by incorporating vim with LaTeX
  • this website seems the easiest way to learn vim hands-on if you need a refresher
  • here are more tutorials for reference, don't go to the site above
  • ooh you can even create actions through vim for functions called "tags"
  • Tricks and tips from and for vim users is posted on the wiki
  • generate the config file for your vimrc for different languages no matter where you go here

Find & Replace Text in Vim:

:[range]s/{pattern}/{string}/[flags]
  • What does each mean?
    • [range] indicates that you can pass the range of lines. Pass % to find and replace in all lines. The range is separated by a comma. To find and replace between lines 5 to 10, pass 5,10. Use . to represent the current line and $ the last line of the file.
    • {pattern} indicates the pattern to find the text. You can pass regex patterns here.
    • {string} is the string to replace in the found text.  
    • [flags] indicates if you wish to pass any additional flags (for example, the flag c is passed to confirm before replacing the text). By default, this does a case-sensitive search. You can change it to do a case-insensitive search by passing a i flag.

Copy, Cut and Paste in Normal Mode

When you launch the Vim editor, youre in the normal mode. In this mode, you can run Vim commands and navigate through the file.

To go back to normal mode from any other mode, just press the Esc key.

Vim has its own terminology for copying, cutting, and pasting. Copy is called yank (y), cut is called delete (d), and paste is called put (p).

Copying (Yanking)

To copy text, place the cursor in the desired location and press the y key followed by the movement command. Below are some helpful yanking commands:

  • yy - Yank (copy) the current line, including the newline character.
  • 3yy - Yank (copy) three lines, starting from the line where the cursor is positioned.
  • y$ - Yank (copy) everything from the cursor to the end of the line.
  • y^ - Yank (copy) everything from the cursor to the start of the line.
  • yw - Yank (copy) to the start of the next word.
  • yiw – Yank (copy) the current word.
  • y% - Yank (copy) to the matching character. By default supported pairs are (){}, and []. Useful to copy text between matching brackets.

Cutting (Deleting)

In normal mode, d is the key for cutting (deleting) text. Move the cursor to the desired position and press the d key, followed by the movement command. Here are some helpful deleting commands:

  • dd - Delete (cut) the current line, including the newline character.
  • 3dd - Delete (cut) three lines, starting from the line where the cursor is positioned,
  • d$ - Delete (cut) everything from the cursor to the end of the line.

The movement commands that apply for yanking are also valid for deleting. For example dw, deletes to the start of the next word, and d^ deletes everything from the cursor to the start of the line.

Pasting (Putting)

To put the yanked or deleted text, move the cursor to the desired location and press p to put (paste) the text after the cursor or P to put (paste) before the cursor.

How to Undo or Redo in Vim

To undo a change in Vim, press u in command mode. To redo, press CTRL + R. You can prepend a number (n) with u to undo n times. for example, 2u will undo 2 times. To list the available undo options, type :undolist in command mode.

Vim vs NeoVim?

There's a long history and different strains of Vim on the internet. One of the more popular alternatives seems to be NeoVim

!Pasted image 20221208182151.png If you're comfortable using Vim, there may be no need to switch.


Vim for different use-cases

In fact, there is even a vim key-binding settings option here in Obsidian.

  • here is the docs for vim-go for the go language.