A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learnbyexample.github.io/vim_reference/Normal-mode.html below:

Normal mode - Vim Reference Guide

Normal mode

Make sure you are in Normal mode before trying out the commands in this chapter. Press Esc key to return to Normal mode from other modes. Press Esc again if needed.

Documentation links:

Arrow motions

The four arrow keys can be used in Vim to move around, just like other text editors. Vim also maps them to four letters in Normal mode.

Vim offers a plethora of motion commands. Several sections will discuss them later in this chapter.

You can use the whichwrap setting to allow ← and → arrow keys to cross lines. For example, :set ww+=<,> tells Vim to allow left and right arrow keys to move across lines in Normal and Visual modes. Add h and l to this comma separated list if want those commands to cross lines as well.

Cut

There are various ways to delete text. All of these commands can be prefixed with a count value. d and c commands can accept any of the motion commands. Only arrow motion examples are shown in this section, many more variations will be discussed later in this chapter.

You can also select text (using mouse or visual commands) and then press d or x or c or s to delete the selected portions. Example usage will be discussed in the Visual mode chapter.

The deleted portions can also be pasted elsewhere using the paste command (discussed later in this chapter).

Copy

There are various ways to copy text using the yank command y.

You can also select text (using mouse or visual commands) and then press y to copy them.

Paste

The put (paste) command p is used after cut or copy operations.

Undo

See :h 32.3 for details on g- and g+ commands that you can use to undo branches.

Redo Replace characters

Often, you just need to change one character. For example, changing i to j, 2 to 4 and so on.

To replace multiple characters with different characters, use R.

The advantage of r and R commands is that you remain in the Normal mode, without needing to switch to Insert mode and back.

Repeat a change

From :h 4.3:

The . command works for all changes you make, except for u (undo), CTRL-R (redo) and commands that start with a colon (:).

See :h repeat.txt for complex repeats, using Vim scripts, etc.

Open new line

Indentation of the new line depends on the autoindent, smartindent and cindent settings.

Moving within the current line

Moving within long lines that are spread over multiple screen lines:

See :h left-right-motions for more details.

Character motions

These commands allow you to move based on a single character search, within the current line only.

Note that the previously used count prefix wouldn't be repeated with the ; or , commands, but you can use a new count prefix. If you pressed a wrong motion command, use the Esc key to abandon the search instead of continuing with the wrongly chosen command.

Word motions

Definitions from :h word and :h WORD are quoted below:

word A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, &LTEOL>). This can be changed with the iskeyword option. An empty line is also considered to be a word.

WORD A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.

All of these motions will work across lines. For example, if the cursor is on the last word of a line, pressing w will move to the start of the first word in the next line.

Text object motions

More such text objects will be discussed later under the Context editing section. See :h object-motions for a complete list of such motions.

Moving within the current file

It is also possible to match a pair of keywords like HTML tags, if-else, etc with %. See :h matchit-install for details.

Moving within the visible window Reposition the current line

See :h 'scrolloff' option if you want to always show context around the current line.

Indenting

Indentation depends on the shiftwidth setting. See :h shift-left-right, :h = and :h 'shiftwidth' for more details.

You can indent/unindent the same selection multiple times using a number prefix in the Visual mode.

Mark frequently used locations

Motion commands that take you across lines (for example, 10G) will automatically save the location you jumped from in the default ` mark. You can move back to that exact location using `` or the first non-blank character using '`. Note that the arrow and word motions aren't considered for the default mark even if they move across lines.

See :h mark-motions for more ways to use marks.

Jumping back and forth

This is helpful if you are moving around often while editing a large file, moving between different buffers, etc. From :h jump-motions:

The following commands are jump commands: ', `, G, /, ?, n, N, %, (, ), [[, ]], {, }, :s, :tag, L, M, H and the commands that start editing a new file.

When making a change the cursor position is remembered. One position is remembered for every change that can be undone, unless it is close to a previous change.

Use :jumps and :changes to view the jump and change lists respectively. See :h jump-motions for more details.

Edit with motion

From :h usr_03.txt:

You first type an operator command. For example, d is the delete operator. Then you type a motion command like 4l or w. This way you can operate on any text you can move over.

From :h usr_03.txt:

Whether the character under the cursor is included depends on the command you used to move to that character. The reference manual calls this "exclusive" when the character isn't included and "inclusive" when it is. The $ command moves to the end of a line. The d$ command deletes from the cursor to the end of the line. This is an inclusive motion, thus the last character of the line is included in the delete operation.

Context editing

You have seen examples for combining motions such as w, % and f with editing commands like d, c and y. Such combination of commands require precise positioning to be effective.

Vim also provides a list of handy context based options to make certain editing use cases easier using the i and a text object selections. You can easily remember the difference between these two options by thinking i as inner and a as around.

You can use a count prefix for nested cases. For example, c2i{ will clear the inner braces (including the braces, and this could be nested too) and then only the text between braces for the next level.

See :h text-objects for more details.

Named registers

You can use lowercase alphabets a-z to save some content for future use. You can also append some more content to those registers by using the corresponding uppercase alphabets A-Z at a later stage.

You can use :reg (short for :registers) to view the contents of the registers. Specifying one or more characters (next to each other as a single string) will display contents only for those registers.

The named registers are also used for saving macros (will be discussed in the Macro chapter). You can record an empty macro to clear the contents, for example qbq clears the "b register.

Special registers

Vim has nine other types of registers for different use cases. Here are some of them:

Further reading

Search word nearest to the cursor

You can also provide a count prefix to these commands.

Join lines

joinspaces, cpoptions and formatoptions settings will affect the behavior of these commands. See :h J and scroll down for more details.

Changing case

You can also provide a count prefix to these commands.

Increment and Decrement numbers

Numbers prefixed with 0, 0x and 0b will be treated as octal, hexadecimal and binary respectively (you can also use uppercase for x and b).

Decimal numbers prefixed with - will be treated as negative numbers. For example, using Ctrl+a on -100 will give you -99. While this is handy, this trips me up often when dealing with date formats like 2021-12-07.

Miscellaneous Switching modes

Normal to Insert mode

Normal to Command-line mode

Normal to Visual mode

See :h mode-switching for a complete table. See also this comprehensive illustration of navigating modes.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4