Showing posts with label Philosophy. Show all posts
Showing posts with label Philosophy. Show all posts

Tuesday, March 2, 2021

Vim - Ex Mode

When learning Vim, it's important to understand its modes, including the Ex Mode where you can continually type your commands using Vim's built-in Ex-compatibility mode.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Ex Mode. This is the mode where you can commands in batch and even emulate the old Ex text editor.

What's Ex Mode?

If you learned about the Command-line mode you already know how to enter Ex commands in Vim. Turns out that the Ex mode is an extension of that mode and allows us to run not only single commands but also serves as a built-in batch processing tool for Vim.

It's also worth pointing out that Ex is a text editor, predecessor of Vim, and from which Vim (and vi) got a lot of inspiration from. Vim still today supports a direct Ex emulation (via the Q command) which is less and less used.

The Ex mode is also part of a broader group of advanced capabilities which we'll discuss in future posts.

Entering Ex mode

To enter the improved Ex mode from Vim, press either gQ in Normal mode. You should see a message similar to:

Type gQ to get to Ex mode
From there, you can start typing your commands. For example, highlighted in yellow are three commands run in the same Ex session:

While in Ex mode, keep entering your Ex commands

Ex mode from the shell

Ex mode can also be accessed from the shell via the ex command (or vim -e). Running it, should result in a screen similar to:
Vim started in ex mode

Getting to Normal Mode

To get back to Normal mode from Ex mode, enter the :visual c(or :vi for short).

Quitting Ex Mode

To quit Ex mode, enter the already known :q command.

When to use the Ex mode?

Advanced Vimmers prefer using the Ex for batch processing since they can combine Vim's awesome features (such as buffers and regular expressions) in more powerful ways than if they had to perform the same operations by combining tools like awksed and others.

Apart from batch processing, other more advanced use cases are:

  • editing multiple files non-interactively (for example, as part of the script)
  • an alternative for slow connections
  • mappings and abbreviations are disabled
  • restricted access to keys such as Escape or Control
Keep tuned as we will provide more examples of the above use cases in future blog posts.

Learning More

Ready to learn more about the Ex mode? Open its dedicated manual with:

:h ex-mode

Mode-specific help

If you want to know more about specific keys, 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 Ex 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.

Recommended Reading

Since the use cases for Ex mode are more advanced, we recommend reading the following articles:

See Also

Tuesday, February 16, 2021

Vim - Command-Line Mode

When learning Vim, it's important to understand its modes, particularly the Command-Line (or Command or Cmdline mode) where you can enter your own commands in Vim.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn another very important mode in Vim: Command-Line Mode (or Command mode, or Cmdline mode). This is the mode where you enter actual commands in Vim.

The Command-Line Mode

It's probably not new to you that Vim is one of the few text editors that allows you to extensively use commands. Combined with Vim's modal design, the Command-Line mode (or to simplify, Command mode) is yet another of Vim's characteristics that make it stand out from alternatives.

Vim's Command mode has a wide variety of commands that allow you to do practically everything. Let's take a look.

Accessing the Command-Line Mode

There are three ways to enter the Command-line mode. Type in Normal mode:

  • : - to enter Ex commands
  • / or ? - to search
  • ! - to filter

Entering commands with :

Typing the : key in Normal mode will allow you to enter Ex commands. Ex commands are Vim's built-in commands you can run to perform any type of operation. Typing : from Normal mode sets your cursor on the last line of the editor where you can type your commands:

Type : in normal mode to get to the command-line mode

From there, you could enter commands that virtually would do anything. For example, to open a file, type:

:e <filename>

Next, to list which files are currently open, type :ls:

Type :ls to list which files are currently open

We could use the :e[dit] <filename> command to open other files, make our modifications and save them with either :w or :saveas <another-name>. Once done, we quit with the ultra-famous Vim quit command :q!

Searching with / and ?

The two other search-related keywords that get you to the Command-line mode are / (search forward) and ? (search backwards). For example, by typing /praes would highlight the first element found by the search:

Filtering with !

The last way to get to Command-line mode is by typing ! in Normal mode. This operator allows us to filter and run commands on any file opened. For example, for the below file:
Type !sort and let Vim do its magic
If I typed !sort, Vim would automatically sort all the lines alphabetically (since sort is considered a filter):

Some commands you should know

There are so many commands you can use that we'll list some interesting Ex commands you should know.

Editing Files

  • :e <filename> - edits file
  • :bd! - closes the current file (buffer)
  • :ls - lists open files
  • :bn - goes to the next open file
  • :bp - goes to the previous open file

Quitting Vim

  • :q - quit (short for :quit)
  • :q! - quit without saving (short for :quit!)
  • :wq - write and quit
  • :wq! - force write and quit
  • :x - write and quit (similar to :wq, but only write if there are changes)
  • :exit - write and exit (same as :x)
  • :qa - quit all (short for :quitall)
  • :qa! - quit ignoring changes

Searching

To search, use / or ? as previously mentioned. We will cover search and replace more in detail in future posts.

Getting Help

Vim has a number of other methods that you can read about in the help documentation, :h or :help.

Command-Line Completion

It's important to note that Vim also provides you command-line completion via the <Tab> key. For example, if you wanted to change your colorscheme, you could type :colorscheme <Tab> and from there, move to the option you want using the <Tab> key:

Type :colorscheme <Tab> and let Vim complete it for you

Previous Commands

Vim also offers contextual history for previous commands. While in Command-line mode, press the <Up> and <Down> to view previously executed commands.

Getting back to Normal Mode

To get back to Normal mode from Command mode (or one of Vim's other modes), as you might expect, press <Esc> or <Ctrl-C> or <Ctrl-[>.

Learning More

Ready to learn more about the Command mode? Open its dedicated manual with:

:h cmdline-mode

Mode-specific help

If you want to know more about specific keys, 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 Command-Line 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, February 9, 2021

Vim - Replace Mode

When learning Vim, it's important to understand its modes, particularly the Replace Mode where you can replace text.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Replace Mode. This is the mode where you replace text instead of the more common insert done in Insert mode.

What you need to know

The Replace mode allows you to replace existing text by directly typing over it. It's similar to when you press the <Insert> key in a any other text editor. To enter Replace mode, either: 

  • press R in Normal mode 
  • press <Insert> while in Insert mode

Once you enter Replace mode, you should see in the message -- REPLACE -- in the status bar:

Press R in Normal mode to get to replace mode

From here, simply type or text and each original character will be replaced by the new keys. If no character exists, Vim will keep adding as if you were in Insert mode.

Interesting Behaviors

But the Replace mode is a little more than a text editor with the <Insert> key on. Here's what you should observe:

  • Some of the Insert mode keywords do not work in this mode
  • <Backspace> will not delete backwards but bring back characters previously replaced
  • <Enter> creates a line break and doesn't delete any character
  • Ctrl+W and Ctrl+U bring back replaced characters instead of deleting as in Insert mode

To learn more about the Replace mode, open its dedicated manual with:

:h replace-mode

Getting to Normal Mode

To get back to Normal mode from Replace mode (or one of Vim's other modes),  press <Esc> or <Ctrl-C> or <Ctrl-[>.

Virtual Replace Mode

For reference, there's also another mode called Virtual Replace mode which can be activated with gR. It's similar to replace mode but instead of replacing actual characters in a file you are replacing screen real estate so that characters further on in the file never appear to move.

To know more about the Virtual Replace mode, run:

:h vreplace-mode

Mode-specific help

If you want to know more about specific keys, 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 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 Replace 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, February 2, 2021

Vim - Blockwise Visual Mode (aka vertical selection)

When learning Vim, it is important to learn the Blockwise Visual Mode where you can work with vertical selections
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood the Visual mode it's time to investigate further the Blockwise Visual Mode, one of the sub-modes of Vim that allows you to perform actions on blocks of texts, including vertical blocks.

Visual Mode Recap

But before getting to the Blockwise Visual Mode, let's quickly recap yet again the Visual mode which should probably be familiar to you at this point. It's the mode in which Vim highlights the selected text so you can run operations on it. It's equivalent to how you select text in other text editors using the mouse or the keyboard. 

There are three visual modes in Vim:

  • Visual Mode per character (v) - sets a starting selection point and keep altering the selection while you move the cursor
  • Visual Mode linewise (V) - selects line by line
  • Blockwise Visual mode (Ctrl+V) - allows vertical selections

Today we will focus on the Blockwise Visual Mode and learn how it can be used to solve problems efficiently.

Blockwise Operators

The Blockwise Visual Mode contains its own blockwise operators:
  • I - inserts at the start of the block on every selected line
  • A - appends to the end of the block on every selected line
  • c - change the selection
  • r - replace each word of the selection by the supplied character

Example 1 - Inserting text

So let's see an example. Imagine that we have this piece of Markdown and we want to make the lines 2 - 6 a markdown list by prefixing it with "- ".
Our list before: no - in the beginning of the line
By pressing Ctrl+V}I- we'd end up with:
Our list after: we added - to the beginning of the line
Where:
  • Ctrl+V - enters visual block mode
  • } - selects until the end of the paragraph
  • I- - inserts "- " at the beginning of the block 

Example 2 - Uppercase item

Let's continue our example by setting making item uppercased. First, we select our column by positioning our cursor in the first i and press Ctrl+V5j to select vertically:
Then we press ~ to change the case of each character in the selection:
Where:
  • Ctrl+V - enters visual block mode
  • 5j - selects the next 5 lines
  • - changes the case (making i -> I)
We could have achieved the same results above using the replace tool. In the end, use whatever makes sense for your use case.

Example 3 - Replace text

Let's continue our example by replacing item by word. First we select item with Ctrl+Ve5j:
Then we replace it with cvim tip, ending up with:
Where:
  • c - changes the selection
  • vim tip - is the text we wanted the target to be

Other Operations

Apart from the previous examples, here are other interesting commands to run on your selections:

  • y - to yank (copy) the text
  • ~ - change case of the selection
  • d - delete the selection
  • U - to uppercase
  • u - to lowercase
  • gg - format lines
  • J - to join all lines
  • : - to run any command on the selection
  • r - to replace any character in the selection by another
  • > - to indent the selection
  • < - to un-indent the selection

Getting to Normal Mode

To get back to Normal mode from Visual mode (or one of Vim's other modes), press <Esc> or <Ctrl-C> or <Ctrl-[>.

Learning More

Ready to learn more about the Visual mode? Open its dedicated manual with:

:h visual-mode

To learn more about blockwise operators, run:

:h blockwise-operators

Mode-specific help

If you want to know more about specific keys, 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 Visual Block 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 26, 2021

Vim - Visual Mode

When learning Vim, it's important to understand its modes, particularly the Visual Mode where you can visually select portions of your content.
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn yet another Vim mode: Visual Mode. This is the mode where you insert/modify text.

On this tutorial we will understand more about it and how to use it effectively.

Visual Mode 101

The Visual mode will probably be familiar to you. It's the mode in which Vim highlights the selected text so you can run operations on it. It's equivalent to how you select text in other text editors using the mouse or the keyboard. 

Vim allows any operation on visual blocks including copying, deleting, replacing, changing case, etc. 

Visually selecting a portion of text is not necessary to perform the above operations as we'll see in future blog posts.

Visual Mode types

There are three visual modes in Vim:

  • Visual Mode per character (v) - sets a starting selection point and keep altering the selection while you move the cursor
  • Visual Mode linewise (V) - selects line by line
  • Blockwise Visual mode (Ctrl+V) - allows vertical selections

Once the visual mode is started with one of the options above, move the cursor to the desired end selection point. You will see the text selection being highlighted. Let's see them in action.

Visual Mode per character

Start the visual mode with character with v then move your cursor around to keep expanding your selection. For example, if from the start of the file you typed v4w, you would have:

Visual selection after pressing v4w from the top of the file

Where:

  • v - enters visual mode per character
  • 4w - moves 4 words ahead

Visual Mode linewise

The difference between the visual mode linewise and the previous one is that this selects line by line (but is not limited to only lines). Start the visual mode linewise with V then move your cursor around to keep expanding your selection. For example, if with my cursor located on the top of the file I pressed V2}, I'd have:

Visual selection after pressing V2} from the top of the file
Where:

  • V - enters visual mode linewise
  • 2} - 2 paragraphs forward

Blockwise Visual Mode

The Blockwise visual mode allows us to perform vertical selections. For example, if you pressed Ctrl+V4j$ from the quae word, you'd have:


Where:

  • Ctrl+V - enters blockwise visual mode
  • 4j - moves 4 lines down
  • $ - moves to the end of the line

Selecting with search

Another interesting thing we could with any of the previous modes is select up to a certain point using the built-in search (/). For example, if I typed v/libe from the quae word, I'd have:
Altering the selection with search

Fine-tuning the selection

If for some reason the selection is not where you need, you can still change your selection. Press <Enter> and move the selection using any of the motions (hjklw}($, etc). You can also move the cursor to the other end of the selection with:
  • o - go to the other end of the highlighted text
  • O - similar to o but in visual block mode, moves to the other corner in the same line

Performing Operations

With the selection made, press <Enter> to set it so you can run interesting commands like:

  • y - to yank (copy) the text
  • ~ - change case of the selection
  • d - delete the selection
  • U - to uppercase
  • u - to lowercase
  • gg - format lines
  • J - to join all lines
  • : - to run any command on the selection
  • r - to replace any character in the selection by another
  • > - to indent the selection
  • < to un-indent the selection

Getting to Normal Mode

To get back to Normal mode from Visual mode (or one of Vim's other modes), press <Esc> or <Ctrl-C> or <Ctrl-[>.

Learning More

Ready to learn more about the Visual mode? Open its dedicated manual with:

:h visual-mode

Mode-specific help

If you want to know more about specific keys, 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 Visual 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 19, 2021

Vim - Insert Mode

When learning Vim, it's important to understand its modes, particularly the Insert Mode, Vim's second most popular mode.

       
Photo by Alex Knight on Unsplash

Once you installed Vim, learned the basics of Vim Modes and understood Normal mode, it's time to learn Vim's second most used mode: Insert Mode. This is the mode where you add/remove/modify text. 

Insert Mode 101

The Insert mode will probably fell very familiar to you since it's the default (and only) mode most text editors operate with. It's the mode where you add/remove text.

But you may be surprised to know that it's not Vim's default mode. Why?

Because in Vim, it all comes back to efficiency. Since most of the time you're reading text and not altering it, it just makes sense that the default mode (Normal mode) be the one that operates by default.

It's an anti-pattern to stay too long in the insert mode. As soon as you make your changes, you should go back to Normal mode.

Entering Insert Mode

There are many ways to enter insert mode, mainly:

  • i - enters insert mode and inserts text before the cursor
  • I - enters insert mode and inserts text before the first non-blank of the line
  • o - enters insert mode and inserts a new line below
  • O - enters insert mode and inserts a new line above
  • a - enters insert mode and append text after the cursor
  • A - enters insert mode and appends text at the end of the line
Which one should I choose? The one that makes sense given where your cursor is located in the file. With time you will memorize the differences and will automatically use the best one.

Other Interesting Commands

There are other interesting commands in Insert mode that are work mentioning, including:

  • Ctrl+@ - insert previously inserted text and stop insert
  • Ctrl+A - insert previously inserted text
  • Ctrl+W - delete the word before the cursor (as in bash)
  • Ctrl+U - delete all the previous characters in the current line (as in bash)
  • Ctrl+J - begin a new line (as in bash)
  • Ctrl+N - find the next keyword (autocompletion)
  • Ctrl+P - find the previous keyword (autocompletion)
  • Ctrl+R - inserts content from a register
  • <Insert> - toggles between insert and replace mode

Getting to Normal Mode

To get back to Normal mode from Visual mode (or one of Vim's other modes), press <Esc> or <Ctrl-C> or <Ctrl-[>.

Learning More

Ready to learn more about the Insert mode? Open its dedicated manual with:

:h insert-mode

Mode-specific help

If you want to know more about specific keys, 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 Visual Block 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 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, 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

    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