Showing posts with label Normal Mode. Show all posts
Showing posts with label Normal Mode. Show all posts

Tuesday, January 12, 2021

Vim - Normal Mode

When learning Vim, it's important to understand its modes. Normal mode is the most common and the one you're expected to spend most of your time
Photo by Alex Knight on Unsplash

Once you installed Vim and understood the basics of Vim Modes, you probably understand that Vim has various modes. What's called the Normal Mode is Vim's default mode. On this tutorial we will understand more about it and how to use it effectively.

Normal Mode 101

Would you be able to precise how long you read a file and how long you modify it? I hope you agree that at this point in your Vim journey, unless you're not creating brand new content, you spend way more time reading and moving through the file than modifying it.

And that's exactly why the Normal mode exists and why it's Vim's default mode. Because 99% of the time you spend more time reading (and moving) your cursor through a file, it exists for you to interact with your files and with Vim and is where you should operate most of the time.

Getting to Normal Mode

So how to get to Normal mode? If you're in one of Vim's other modes, to get back to normal mode, press <Esc> or <Ctrl-C> or <Ctrl-[>.

Common Commands

Below are some of the most common commands you'll use in Normal mode.

Moving the cursor

  • h - move the cursor one character left
  • j - move the cursor one line down
  • k - move the cursor one line up
  • l - move the cursor one character right
  • 0 - move to the beginning of the line
  • $ - move to the end of the line 
  • w - move the cursor to beginning of next word
  • b - move the cursor to previous beginning of word
  • gg - move to the beginning of the document
  • G - move to the end of the document
  • Ctrl-D - move half a page down
  • Ctrl-U - move half a page up
  • :<num> - move the cursor to line <num>. Ex, :133 moves the cursor to line 133 in the current file

Undoing

  • u - undo previous change
  • Ctrl-r - redo previous change

Running Commands

  • : - opens the command mode (where you can type commands, for example to quit Vim, save or open a file)

Modifying text

  • x- deletes the next character
  • X - deletes the previous character
  • D - delete from the cursor to the end of the line
  • S - delete the whole line and set in insert mode

Searching

By typing / you open the search mode. We will study it better in a future tutorial

Marks

  • ma - creates a mark a
  • `a - moves the cursor to mark a

If all of the above sounds a lot, fear not! Keep following this blog as we will gradually guide you to understand, organically memorize (and love!) how Vim works. 

Running a single Normal Mode Command

There's one more interesting thing to mention about the Normal mode. It may happen that you just want to run one command in Normal mode and getting back to insert mode. Vim also gets you covered with Ctrl-O. For example, to delete from the cursor to the rest of the line, do:

Ctrl-O D

Mode-specific help

Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.

Conclusion

On this post we continued in our Vim journey by learning a bit more about Vim's Normal mode. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.

Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

Tuesday, January 5, 2021

Switching Modes in Vim

Learn how to swtich modes in Vim.
Photo by Alex Knight on Unsplash

On a previous post we learned about three of the most important foundational concepts of Vim: Commands, Buffers, Modes and Motions. We also recently reviewed how to install Vim on MacsWindows or Linux, learned how to get started with Vim and mastered your first Vim tutorial, let's review tips and tricks to keep learning Vim. 

Today we will continue discussing Modes, specifically talking about a common operation in Vim: how to switch modes. But before we continue, let's review what are modes in Vim and which they are.

Modes in Vim

Modes are another feature that differentiates Vim from traditional text editors and something that's important to comprehend. Modes are 

The most common modes are:

  • Normal: Vim's primary mode and the one we use to run commands:
  • Insert: the mode where you edit the contents of your files
  • Visual: visual selection of contents
  • Replace: allows you to type over the text, replacing it
  • Command-line: allows you to execute commands via its own command-line prompt
  • Visual-block: allows you to make block-level changes (vertical selections and modifications)
  • Ex: in this mode Vim emulates the Ex editor and is used mainly for batch processing
  • Select: like the visual mode but with more CUA like behavior.

How to switch Modes in Vim

But understanding Modes is just the first part. Next, you have to understand how to switch between modes. The traditional workflow is to press <Esc> to go back to normal mode then press a keystroke to enter the other mode. Some of the keystrokes you can use to switch between modes are:

  • <Esc> - goes back to normal mode
  • i - enters insert mode (but other keystrokes exist)
  • : - enters command mode (but other keystrokes exist)
  • v - enters visual mode
  • V - enters visual mode (line selection)
  • R - enters command-line mode
  • <Ctrl-v>: enters visual block mode

Switching Modes in Action

So let's see some real use cases. In Vim, open an existing file with :e <filename> and follow the next steps.

Inserting Text in Insert Mode

For every opened file, the default mode is Normal mode. To type modifications in our file, we should either switch to Normal or Replace, Normal being by far the most common one. Some important keywords worth memorizing when switching from Normal to Insert mode are:

  • i - to insert in the current position
  • a - to insert after the next char
  • C - to change from the next char (delete all until the end of the line)
  • S - to substitute the current line (delete the whole line)

Back to Normal Mode

Next, get back to Normal Mode by pressing <Esc>, find the text hello by searching with /hello in normal mode and hit enter to set the cursor in that position.

Replacing text in Replace Mode

In case you want to type over the text, replacing it, press R to enter replace mode, enter your changes and <Esc> again to go back to normal mode.

Back to Normal Mode

Guess how should we get back to Normal Mode? You got it! By pressing <Esc>

Replacing Text using Command Mode

But we could have replaced hello by world using command mode. For example, the command below would run in command mode and would replace all occurrences of the word hello by world in the current buffer:

:%s/hello/world/g

Saving our file

Next, to save our file from Command Mode by typing :w

Quitting

And quit with :q

In which mode am I?

And how do we know in which mode we are? Assuming you're not running any plugins, Vim should show you that in the bottom of the screen:

  • Normal Mode: blank (no info)
  • Insert Mode: -- INSERT --
  • Replace Mode: -- REPLACE --
  • Visual: -- VISUAL LINE --
  • and so on
For example, in insert mode you should see something like:

If that info is not available, try resetting the showmode config with:

:set showmode

Fore more info about that configuration, type:

:h showmode

Conclusion

On this post we reviewed how to switch modes in Vim. We hope that these examples helped you understand better how these modes work in conjunction. We recommend you playing with the modes and getting comfortable with them since they're an essential part of learning and mastering Vim.

See Also

Tuesday, December 15, 2020

Vim Modes - All you need to know to get started

When learning Vim, it's important to understand well its Modes, one of Vim's most fundamental concepts.
Photo by Alex Knight on Unsplash

On a previous post we learned about three of the most important foundational concepts of Vim: Commands, Buffers, Modes and Motions. We also recently reviewed how to install Vim on MacsWindows or Linux, learned how to get started with Vim and mastered your first Vim tutorial

Today we will continue our discussion on modes: what they are, what they serve and how to use them.

What are modes in Vim?

Modes are another feature that differentiates Vim from traditional text editors and something that's important to comprehend. Modes are essentially different ways (or modes) Vim can work on. To master (and learn well) Vim it's important to understand not only which they are but how they differ and how they can you help you get your work done.

The most common Modes

Vim has seven basic modes The most common modes are:

  • Normal: Vim's primary mode and the one we use to run commands:
  • Insert: the mode where you edit the contents of your files
  • Visual: visual selection of contents
  • Replace: allows you to type over the text, replacing it
  • Command-line: allows you to execute commands via its own command-line prompt
  • Terminal-Job: allows you to interact with a job in a terminal window
  • Ex: in this mode Vim emulates the Ex editor and is used mainly for batch processing
  • Select: like the visual mode but with more CUA like behavior.

Using the Modes

So let's see some examples in how can you use these modes.

Normal Mode

In normal mode you're not supposed to type text but to send commands to Vim. Some of them are:

  • h - move one character left
  • j - move one line down
  • k - move one line up
  • l - move one character right
  • 0 - move to the beginning of the line
  • $ - move to the end of the line 
  • w move to beginning of next word
  • b move to previous beginning of word
  • gg - move to the beginning of the document
  • G - move to the end of the document
  • u - undo previous change
  • Ctrl-r: redo previous change
  • : - go to ex mode (where you can type commands, for example to quit Vim, save or open a file)
  • ma - creates a mark a
  • `a - moves the cursor to mark a
  • and more, much more
Don't worry, we'll study normal modes in the future

Visual Mode

Press v in normal mode to enter visual mode, then move the arrow with h/j/k/l or with other commands (see above for examples). After you're comfortable with the selection, you can either type y to yank (copy) the text, x to delete it of any other command available in that mode.

We'll study more about the normal mode in the future.

Command Mode

It's also possible to run commands in Vim. That mode is called Command mode (or command-line mote) and to get to it type : while in normal mode. For example, the command below would run in command mode and would replace all occurrences of the word hello by world in the current buffer:

:%s/hello/world/g

Visual Mode

To use the visual mode (similar to the select feature in your text editor can be accomplished by pressing the following keys in normal mode:

  • v - start visual mode from the current character
  • V - start visual mode with line selection

Switching between modes

The last thing is how to switch between modes. The traditional workflow is to press <Esc> to go back to normal mode then press a keystroke to enter the other mode. Some of the keystrokes you can use to switch between modes are:

  • <Esc> - goes back to normal mode
  • i - enters insert mode
  • : - enters command mode
  • v - enters visual mode
  • R - enters command-line mode
  • V - enters visual mode (line selection)
  • <Ctrl-v>: enters visual block mode

In which mode am I?

And how do we know in which mode we are? Assuming you're not running any plugins, Vim should show you that in the bottom of the screen:

  • Normal Mode: blank (no info)
  • Insert Mode: -- INSERT --
  • Replace Mode: -- REPLACE --
  • Visual: -- VISUAL LINE --
  • and so on
For example, in insert mode you should see something like:

If that info is not available, try resetting the showmode config with:

:set showmode

Fore more info about that configuration, type:

:h showmode

Mode-specific help

Vim also has an intelligent mechanism to get you to the help quickly. It follows this pattern:

What Prepend Example
Normal mode command :help x
Visual mode command v_ :help v_u
Insert mode command i_ :help i_<Esc>
Command-line command : :help :quit
Command-line editing c_ :help c_<Del>
Vim command argument - :help -r
Option ' :help 'textwidth'
Regular expression / :help /[

We hope you get used to the above syntax and use it regularly in you your Vim journey. It will not only help you learn more about Vim but also to memorize the commands better.

Conclusion

On this post we learned the most important modes, what they are, what they serve and how to use them.

If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim. Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

Tuesday, December 1, 2020

Fundamental concepts of Vim

When learning Vim, it's important to understand its most fundamental concepts: Commands, Buffers, Modes and Motions.
Photo by Alex Knight on Unsplash

Once you installed Vim on MacWindows or Linux, learned how to get started with Vim and mastered your first Vim tutorial, the next thing you should learn about Vim is its fundamental concepts: Commands, Buffers, Modes and Motions.

Commands

Most of the examples you'll see and learn are commands. Commands are operations you run in Vim that can pretty much do everything. For example, the command below, lists the open files:

:ls

Commands can also modify text. For example, the command below would replace all occurrences of the word hello by world in the current buffer:

:%s/hello/world/g

Buffers

Buffers are another essential concept of Vim. You may not have realized it yet but buffers is one of the things in Vim that differs it from traditional text editors.

Every file opened in Vim is opened in a buffer. During that process, Vim copies the contents of that file to memory and uses that until you save your modifications to disk. That way Vim can perform really quick operations on the content you need without reaching the filesystem.

Modes

Modes are another feature that differentiates Vim from traditional text editors and something that's important to comprehend. The most common modes are:

  • Normal: Vim's primary mode and the one we use to run commands:
  • Insert: the mode where you edit the contents of your files
  • Visual: visual selection of contents
  • Replace: allows you to type over the text, replacing it
  • Command-line: allows you to execute commands via its own command-line prompt
  • Visual-block: allows you to make block-level changes (vertical selections and modifications)
  • Ex: in this mode Vim emulates the Ex editor and is used mainly for batch processing
  • Select: like the visual mode but with more CUA like behavior.

Since there's a lot there to learn, try to stick with Normal and Insert for now. We'll see in detail in future posts how each of the mode works but feel free to use the help as explained on a previous post.

Motions

Another fundamental aspect of Vim is the concept of Motions. Motions are parameters that you pass into your commands or actions moving the cursor around (fore or backwards). Motions is another phenomenal feature of Vim's making it way more powerful than  traditional editors. 

Motions can also be combined with operators (we'll learn more about them in the future) so you can run powerful commands like:

dap

which runs a command that can be read as "delete all paragraph".

Conclusion

On this post we introduced three of the fundamental concepts of Vim: Commands, Buffers, Modes and Motions. If it seems complicated, don't be concerned. It takes years to master Vim but be sure that the more you learn, the more you realize that time is that secret ingredient in getting comfortable, becoming proficient and efficient with Vim.

Learning Vim is like learning a musical instrument. It takes time, effort and discipline but once you master it, the gains are endless. You definitely won't regret.

See Also

    Monday, November 9, 2020

    How to get started with Vim

    Got Vim installed and excited to your first steps? Read to understand what you need to know next
    Photo by Alex Knight on Unsplash

    So, now that you installed Vim on your Mac, Windows or Linux workstation, let's review what's next and how to get started with Vim.

    Getting started with Vim

    The first thing you should understand about Vim is its modes. Vim is not a traditional editor in which you simply type your stuff by using the keys of your keyboard and move you cursor using the mouse and the arrow keys. Vim is way, waaaaaay more than that. And it's exactly because of that that we ๐Ÿงก it.

    But why get to the theory before we can use Vim? Because it's important to understand it so you can exit it safely ๐Ÿ˜†.

    Vim Modes

    We'll keep this straightforward and for simplification, we want you to understand the two modes you will first meet in Vim:

    1. Normal mode: the default mode. This is where you usually enter your commands or move your cursor
    2. Insert mode: the mode you type stuff. Equivalent to most text editors.
    With the above said, which is the mode Vim starts by default? As you probably expect, it's the normal mode. We'll see how to switch modes next.
    There are more modes in Vim but let's keep it simple for now. We'll address that in a future post.

    Starting Vim

    So go ahead and start Vim by typing vim on your terminal:
    vim

    This is what you should see:

    Why use Vim in the terminal? Because Vim is a terminal-based text editor. It's one of the reasons Vim is widely popular and ubiquitous.

    Scary? Maybe. So let's quickly analyze the above, shall we? There are interesting things there that tell us a lot already about Vim:

    1. VIM means Vi IMproved. Vi was a text editor originally written by Bill Joy from which Vim is inspired from
    2. Our version: 8.1.2269
    3. Bram Moolenaar, Vim's author
    4. Some commands, including how to quit Vim (yay!) ๐Ÿ˜Š

    Typing Text

    If you started a text editor, you probably want to type some text. As previously said, this is our first second contact with modes. You know already that we're in normal mode and you want to go to insert mode so you can write your text. The simple way to switch to insert mode is by typing i. A -- INSERT -- message will show up on the status bar indicating that you're now in insert mode. Go ahead and type your stuff. We'll type "Hello Vim4Us!":

    Tip: there are many ways to get into insert mode but let's stick with i for now.

    From here we may want to save our file or exit discarding the changes. Let's see how to do both.

    Saving our file and quitting Vim

    The first thing we should do in order to save our file is leaving insert mode and going back to normal mode. Going to normal mode is as simple as pressing <Esc>. Once you press it, you'll realize that the -- INSERT -- label disappears from the status.

    To save our file as hello.txt, now type in normal mode:

    :w hello.txt

    And exit with:

    :q

    Exiting Vim discarding the changes

    If however you want to discard your changes and thought that using the same :q command would work, you're wrong. This is what you would get:

    But why? Because the :q command tells Vim to quit. But Vim on the other hand is helping us and alerts us that we have unsaved changes. To confirm that we want to exit and lose changes we then have to use the ! operator. The ! operator tells Vim to override the command and execute it regardless of the effect it would have.

    So to quit Vim ignoring changes, type in normal mode:

    :q

    Next Steps

    Now that you understand the first two basic modes in Vim and knows how to change modes and run some commands in Vim, we suggest that you start your first Vim tutorial. But we'll cover that in a future post.

    Conclusion

    On this post we reviewed the basics of getting started with Vim. You now understand that Vim works with modes and know how to change modes and run some commands in it, including saving a file and quitting. What's next? Keep tuned.
    Any comment about this page? Please contact us on Twitter

    Featured Article

    Vim - Ex Mode

    When learning Vim, it's important to understand its modes, including the  Ex Mode  where you can continually type your commands u...

    Popular Posts