Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Vim

You're reading from   Mastering Vim Efficient and effortless editing with Vim and Vimscript

Arrow left icon
Product type Paperback
Published in Jul 2024
Publisher Packt
ISBN-13 9781835081877
Length 300 pages
Edition 2nd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Ruslan Osipov Ruslan Osipov
Author Profile Icon Ruslan Osipov
Ruslan Osipov
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Chapter 1: Getting Started 2. Chapter 2: Advanced Editing and Navigation FREE CHAPTER 3. Chapter 3: Follow the Leader Plugin Management 4. Chapter 4: Understanding Structured Text 5. Chapter 5: Build, Test, and Execute 6. Chapter 6: Refactoring Code with Regex and Macros 7. Chapter 7: Making Vim Your Own 8. Chapter 8: Transcending the Mundane with Vimscript 9. Chapter 9: Where to Go from Here 10. Index

Making simple edits in insert mode

When working with Vim, you usually want to spend as little time as possible in the insert mode (unless you’re writing and not editing). Since most text operations involve editing, we’ll focus on that.

You’ve already learned to enter the insert mode by pressing i. There are more ways to get to the insert mode. Often, you will want to change some piece of text for another one, and there’s a command just for that – c. The change command allows you to remove a portion of text and immediately enter an insert mode. Change is a compound command, meaning that it needs to be followed by a command that tells Vim what needs to be changed. You can combine it with any of the movement commands you’ve learned before. Here are some examples:

Command

Before

After

Cw

c3e

Cb

c4l

cW

words versus WORDS

Remember, non-alphanumeric characters (including punctuation) are treated as words. For example, prepare_menu_item(ingredient, has_spam) will be considered six words (but only one WORD): prepare_menu_item, (, ingredient, , (comma), has_spam, and ).

Exception

As an odd exception, cw behaves like ce. This is a leftover from Vi, Vim’s predecessor.

As you learn more complex movement commands, you can combine these with a change for quick and seamless editing. We’ll also be covering a few plugins that will supercharge a change command to allow for even more powerful editing, such as changing text within braces or replacing the type of quotes on the go.

Just like a sentence

All of these examples follow the <command> <number> <movement or a text object> structure. You can put a number before or after <command>.

For example, say you wish to change the following line:

prepare_menu_item(ingredient, with_spam=has_spam)

Say you want to change the ingredient to 'egg':

prepare_menu_item('egg', with_spam=has_spam)

To accomplish that, you can execute the following set of commands:

Contents of the line

Action

Start with a cursor at the beginning of the line

Hit 2w to move the cursor three words forward to the beginning of ingredient

Press cw to delete the ingredient word and enter the insert mode

Type 'egg'

Hit the Esc key to return to NORMAL mode

Sometimes, we just want to cut things, without putting anything instead, and d does just that. It stands for delete. It behaves similarly to c, except that the behavior of w and e is more standard, as can be seen in the following example:

Command

Before

After

dw

d3e

db

d5l

dW

There are also two more nifty shortcuts that allow you to change or delete a whole line:

Command

What it does

cc

Clears the whole line and enters insert mode. Preserves current indentation level, which is useful when coding.

dd

Deletes an entire line.

For example, look at the following piece:

Figure 1.35 – A piece of code we wrote (or copied) earlier.

Figure 1.35 – A piece of code we wrote (or copied) earlier.

By hitting dd you will completely remove a line, as demonstrated in the following example:

Figure 1.36 – dd removes a line and places the cursor on the next line

Figure 1.36 – dd removes a line and places the cursor on the next line

Hitting cc will clear the line and enter insert mode with the proper indent, as shown in the following example:

Figure 1.37 – cc clears the line, and puts you into insert mode at the beginning of the line.

Figure 1.37 – cc clears the line, and puts you into insert mode at the beginning of the line.

Visual mode

If you run into difficulties picking the right movement commands, you can also use the visual mode to select the text you want to modify. Hit v to enter the visual mode and use the usual movement commands to adjust the selection. Run the desired command (such as c to change or d to delete) once you’re satisfied with the selection.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime