Skip to content
Home » Navigate & Search Like a Ninja in Vim

Navigate & Search Like a Ninja in Vim

Welcome to this tutorial on how to navigate in vim using the keyboard! Vim is a powerful text editor that is widely used in the programming community. One of the key features of vim is its ability to be completely controlled using only the keyboard, making it faster and more efficient for experienced users.

Here are some examples of how to navigate in vim using the keyboard:

Moving the cursor: To move the cursor left, right, up, or down, use the h, j, k, and l keys respectively. For example, if you want to move the cursor to the right, you can use the l key.

itvraag.nl
  ^

Jumping to the beginning or end of a line: To jump to the beginning of a line, use the 0 key. To jump to the end of a line, use the $ key.

itvraag.nl
          ^

Jumping to the beginning or end of a file: To jump to the beginning of a file, use the gg keys. To jump to the end of a file, use the G key.

In vim, motion commands are used to move the cursor within a document. These commands can be used in command mode (when you are not in insert mode). Here are some examples of motion commands in vim:

  1. h – moves the cursor one character to the left
  2. j – moves the cursor one line down
  3. k – moves the cursor one line up
  4. l – moves the cursor one character to the right
  5. w – moves the cursor to the beginning of the next word
  6. e – moves the cursor to the end of the current word
  7. b – moves the cursor to the beginning of the current word
  8. 0 (zero) – moves the cursor to the beginning of the current line
  9. $ – moves the cursor to the end of the current line
  10. gg – moves the cursor to the beginning of the document
  11. G – moves the cursor to the end of the document

These are just a few examples of the many motion commands available in vim. To see a full list of motion commands, you can use the :help motion.txt command in vim.

Searching

Searching for a word: To search for a word in the current file, use the / key followed by the word you want to search for. For example, to search for the word “itvraag.nl“, use the /itvraag.nl keys.

Replacing a word: To replace a word in the current file, use the :%s/old_word/new_word/g keys. For example, to replace the word “itvraag.nl” with “example.com“, use the :%s/itvraag.nl/example.com/g keys.

In command mode, you can use the keys listed above to navigate and perform actions in vim. In insert mode, you can type text as you would in any other text editor. To switch between these modes, use the Esc key to go to command mode and the i key to go to insert mode.

For more information about different “modes” (command- and insert mode) see this article.

The most important thing to keep in mind when navigating in vim is to practice and become familiar with the keyboard shortcuts. The more you use vim, the faster and more efficient you will become.

Here are five ways to apply these vim navigation skills to increase productivity and efficiency:

  1. Use vim to edit files in a terminal: Vim is often used in a terminal environment, where you can use the keyboard shortcuts to quickly edit files without using a mouse.
  2. Use vim to edit multiple files at once: Vim allows you to edit multiple files at once using “tabs” or “windows”. This can be especially useful when working on larger projects.
  3. Use vim to quickly search and replace text: The / and :%s commands can be very useful for quickly searching for and replacing text in a file.
  4. Use vim to write code: Many programmers use vim to write code because of its powerful features and keyboard shortcuts.
  5. Use vim to edit configuration files: Vim is often used to edit configuration files in Linux systems because it is a lightweight and efficient text editor.

Bonus Tips

# navigate to the next match of a specific word
/word
# navigate to the previous match of a specific word
?word
# search for a specific word and replace it with another word
:%s/old_word/new_word/g
# search for a specific word and replace it with another word only in the current line
:s/old_word/new_word/g
# search for a specific word and replace it with another word in the whole file with confirmation
:%s/old_word/new_word/gc
# search for a specific word and replace it with another word only in the selection
:'<,'>s/old_word/new_word/g
# search for a specific word and replace it in all open buffers
:bufdo %s/old_word/new_word/g
# search for a specific word and replace it in all files in the current directory
:argdo %s/old_word/new_word/g
# search for a specific word in the current buffer and all open buffers
:bufdo grep -n "word" **
# search for a specific word in the current directory and all subdirectories
:vimgrep /word/g **
# search for a specific word and replace it in the current buffer only
:%s/old_word/new_word/gc
# search for a specific word and replace it in the current line only
:s/old_word/new_word/gc
# search for a specific word and replace it only in the selection
:'<,'>s/old_word/new_word/gc
# search for a specific word and highlight all matches
:match Error /word/
# clear all search highlights
:nohlsearch
# search and replace a word while preserving the case
:%s/\(\u\)\(.*\)/\L\1\E\2/g

Leave a Reply

Your email address will not be published. Required fields are marked *

five + 10 =