Calculator in Vim – The Expression register

As a software developer you probably have to perform actions like copying different pieces of your code from multiple files into different locations of your current Vim session. Using only system clipboard, this can be a cumbersome and time consuming task. Once you master Vim registers, your text editing efficiency will greatly improve.

A register is a kind of clipboard. It is a place in memory you can use for storing text. Vim supports several kinds of registers and it fills some of them automatically when you perform actions like yanking or deleting text. Others can be filled explicitly by the user.

It’s possible to be productive in Vim without knowing much about registers, but if you really want to edit texts efficiently, you have to understand how they work.

Think of registers as of different buffers for text. Most operating systems and applications have only a single clipboard that you can use for copy, cut and paste operations. Vim is different. In Vim you have not one or two but nine different clipboards!

Yes, there are nine types of registers in Vim. But in this post, I’ll mention only one – the expression register.

This register lets us evaluate the snippet of Vimscript code. This also means, that we can use it as a simple calculator.

Once you’re in INSERT mode, you only need to hit <Ctrl-r>= and then type the expression. The result will be automatically inserted to your current cursor position.

Have a look at the GIF below, it’s that simple!

 

vim calculator

Btw, if you want to learn more about other Vim registers, get started with Vim, or improve your Vim skills – you should definitely check out my book Mastering Vim Quickly.


If you liked the post, we should get connected - follow me on Twitter

My Story: How to Accidentally Self-Publish a Book

Intro

This is the first part of a post which will guide you through the entire process of writing and self publishing a book for the first time.

It’s about my book called Mastering Vim Quickly: From WTF to OMG in no time.

The idea

Vim is a very powerful text editor, used mainly by sysadmins, software developers and similar IT people. It’s known by its steep learning curve, because it’s different than majority of all editors.

This post shows that more than a million people looked for the answer on how to exit Vim. If you need to Google or go to StackOverflow in order to figure out how to exit a text editor – there must be something specific about it, right? :)

So sometimes in 2014, I wanted to improve my Vim skills. I was already a Vim user for more than a couple of years, but I knew there’s much more I can learn.

I did a quick research on the best books on Vim, and I’ve bought a few. Soon enough, I was pretty disappointed. All of these books had too much information which I didn’t really need or want to know.

They were also way too long. I really didn’t have the wish or time to spend reading a 300 page book, just to learn some new Vim features and concepts.

About that time I came up with idea to write my own book – a book from which I would like to learn Vim.

It should be a short read, not more than 150 pages. It should contain the most important Vim concepts, and only the most important features.

If you never used Vim, you should know that this is very important, because Vim has really a lot of features. And you can never stop learning it.

I wrote a few blog posts about the idea for the book. I also left a small sign up form below these posts, for people to sign up for the updates, in case they were interested in the book.

I also wrote a very simple Twitter bot, which would read RSS feeds of some blogs and websites writing often on Vim, and from time to time tweet out those posts with the original links, of course. So all it was really doing was sharing the Vim links consistently.

I did follow a couple of hundred Vim users on Twitter as well, and I let the little script to run on my VPS.

And about that time, I was looking for a new job…

The problem

It was December 2014.
I just moved to Berlin, Germany.

I had a few job offers, and I was about to join one startup.
Those days, I finally decided to realize my idea – write a book on Vim.

However, unexpectedly things went wrong with my work permit.

Long story short: My homeland is not part of the European Union. The only way to get a work permit was – I had to have a university degree recognized in Germany.

Now, I did have the Bachelor university degree. But, it wasn’t recognized in Germany. That’s why, I couldn’t get any job in Germany. I had to come back to Serbia.

Obviously, I had to completely cancel all the work on Mastering Vim Quickly. I closed my freelancing business. I’ve fired all the clients. And I had to go back to university again.

I spent a whole year on completing my Master degree in Computer Science. The one which is recognized in Germany. It took a lot of hard work, but I did it!

I got back to Berlin.

It was a long way. It took a lot of time and effort, but it was worth of it.
I’ve found a dream job in an awesome startup.

Surprise #1

I got back to Berlin, ready to start with new job. The previous year was very though. My whole life, for most of the days, was dedicated to getting that Master degree.

I already kinda gave up from writing a book on Vim. I already had different ideas and priorities, and my motivation wasn’t as high as before.

However, I remembered and decided to check what’s going on with my bot and old Twitter account. I totally forgot about these. I finally had some time to clean up my VPS and remove this bot and some other obsolete stuff.

To my great surprise, I discovered that my Twitter account @MasteringVim had around 4000 followers! How the hell that happened? Turns out, bot was tweeting regularly.

Most of the tweets were Vim articles from other websites. From time to time, it would tweet my few of my articles about the idea for the book “Mastering Vim Quickly”.

Lots of these Vim articles were good. Some of them were tweeted more than a few times. Sometimes, articles would be crappy. But, the it seems like the consistency was the magic trick.

A small group of about a hundred followers I had (before I left Berlin) started to retweet those, and bit by bit, my account started to reach bigger audience.

About a year after, 4K followers, Wow!

I quickly checked my free Mailchimp account (after more than a year!) to see if anyone actually signed up for the updates on my book. You can guess – there was much more people that I expected – around 400 of them!

Crap. Now I felt responsible to at least inform the subscribers why they didn’t hear a word from me for more than a year. I went through the list of emails.

There were people I knew, and people I “knew” from some online communities. Still, the majority were total strangers.

So after a bit of thinking, I’ve decided – I’ll write them an email. I’ll tell them what happened in my private life. I’ll inform them that I didn’t even start writing Mastering Vim Quickly.

And I thought, for sure most of them won’t be interested in the book after a year. Some marketing gooroos told me that email list which you don’t contact often is “cold” and pretty much non-responsive.

Here’s what I had in mind: I’ll write them the truth, there’s no book yet. They’ll get disappointed, or they’ll unsubscribe and/or send hate emails – because they won’t even remember me and my idea.

That will let me give up of writing the book without guilty conscience that I let anyone down.

Surprise #2

Something totally unexpected happened. I’ve sent that email, as planned. What happened is that I got a bunch of email replies, congratulating me on my persistence to get the CS degree and get back to Germany.

Many of these subscribers were still interested in the book, and more than a few already wanted to pre-order it, and asked me how to pay for it! WOW!

I had no choice really. Giving up now would be pretty bad. I knew many of these people, and they were expecting on me to deliver what I promised – a great book on Vim.

So that’s how I decided again, I’m going to write Mastering Vim Quickly!

The first thing I did was to stop the Twitter bot. That time I started tweeting personally, and I still do today. Via @MasteringVim I share the best tips on Vim. It currently has around 20K followers.

Surprise #3

While I was writing the book, few things happened, which I would never expect.

First, I’ve got contacted to cooperate with one big publishing house. We tried talking, but we had different visions, so no cooperation happened.

Then, I got invited to a meetup to talk about a book. A book, which still doesn’t exist. I actually did it. :)

Lastly, I got invited to a conference to talk about Vim – and my book wasn’t even written yet. I had to say no, and focus on the actual writing.

What’s next

In the next post (part 2 of this story) I’ll share my experience on the writing process itself, the book “launch” and the process of publishing a paperback on Amazon.

 


If you liked the post, we should get connected - follow me on Twitter

The Power of Recursive Macros in Vim

If for some crazy reason you’re not already a user of Vim, shutdown your computer and go think about your life choices.

Joking aside, Vim is really a great editor. And if you didn’t know – Vim supports macros.

Basics of macros in Vim

Macros represent a simple concept which can be described as “record the sequence of my actions, save them, and anytime I need them again, execute them.”

This is probably the most underused Vim feature which can improve your productivity dramatically. You can do all sorts of amazing stuff with your code using macros.

Incredibly powerful. Here’s a simple example:

What’s happening here:

  • qa – start recording macro in register a
  • i<Tab>' – enter Insert mode, insert Tab and 
  • Esc – get back to Normal mode (so I can run next command)
  • AAppend command, which places cursor at the end of the current line, in Insert mode
  • ', – Insert ',
  • Esc – get back to Normal mode again
  • j – Go down one line
  • ^ – Go to the start of the current line
  • q – Stop recording the macro

Then, using command @a I run the macro on the current line. After that, I can just hit @@ to run the macro I previously run.

Recursive macros

Using macros can be even more effective with recursion. Recursive macros are especially useful when you need to act on many lines in a file.

In order to record a recursive macro, you need to start with an empty register. You can make the register a empty by hitting qaq.

Now, let’s see recursive macro in action, with a slightly different example:

 

Let’s see what’s going on here:

  • qa followed by Tab and ' followed by Esc – same as in first example
  • f: – find the first occurrence of : and place cursor on it
  • C – command to delete everything from cursor to the end of the line, and enter Insert mode
  • ', followed by Escj and ^ – same as in the first example: insert ', get to Normal mode, move one line below and jump to beginning
  • @a – this is the key step: while recording a macro in register a, we call it inside itself!
  • q – stop recording the macro
  • @a – now we run the recursive macro – and there you go – magic! :)

 

There’s so much more you can do with macros in Vim!

My colleagues sometimes stare at my screen and wonder wtf is going on, when my hands are even not on the keyboard – and my code is being edited by a macro :D

You can master this magic too!

Get Macros chapter (plus three other!) from my book Mastering Vim Quickly for free.

Get 4 chapters for Free!


If you liked the post, we should get connected - follow me on Twitter

The “dot” command in Vim

This post is a part of a chapter from my book Mastering Vim Quickly: From WTF to OMG in no time

I believe you have already heard of the principle Don’t Repeat Yourself.

In software engineering, this is a principle of software development where your focus is on reducing repetition of all kinds. As you’ll see throughout the book, Vim has many ways and commands to automate different kinds of tasks, so you don’t have to repeat your actions.

One of the most powerful Vim command when it comes to avoiding repetition is the . (“the dot”) command.

Hitting . in Normal mode will repeat the last native Vim command you’ve executed.

Let’s say you want to delete 5 words from the cursor forward. As you already know, you could press 5dw and it’s done. However, sometimes it’s not convenient to mentally count the number of words.

An alternative would be to use dw to delete one word. And then press .... to call the dot command four times. In this case, you would repeat the latest, dw command, four more times, and in this way achieve the same effect without counting the words.

If you used dd to delete a line, and you want to delete 4 more lines, you could also execute 4. instead of pressing .... . That also works.

It’s very important to understand what is actually repeatable by the dot command. For example, if you have a sample code like this:

my $i
my $learn
my $quickly

and your cursor is positioned on the first line. You want to append ; to all three lines.
You could run a command like: A;<Esc>j

• A – would place your cursor at the end of the first line in Insert mode.
• ; – you press to actually insert it, and then you press Esc to get back to Normal mode.
• j – to move one line down

Now, your cursor is at the second line. If you then press . to repeat the change in next (second) line, this won’t work. Here’s what you’d get:

my $i;
my $learn;
my $quickly

Your cursor will still be at the second line rather than on the third line, but ; will be appended. This brings us to conclusion that only this part of our original command was repeated: A;<Esc>.

Now, why is this the case? It’s important to remember that with the dot command, you can repeat the commands which change the contents of the buffer.

A change is any command which you can use to modify your text. In our example, we had the case that command j wasn’t repeated, and our cursor wasn’t moved to the third line.

Commands like j are called motions (or nouns)—and they don’t affect the text itself. Command j just moves the cursor, but doesn’t change text anyhow, so it can’t be repeated.

Think in terms of the grammar of your native language: Not nouns, but verbs are used to express some sort of action. The same is true in Vim: nouns (or motions) can’t affect the text, so they can’t be repeated with the dot command.

Of course, if you’d like to repeat multiple changes, or a combination of movements and changes, you can easily record those into a macro. You can learn all you need on macros from Macros chapter of my book Mastering Vim Quickly-download it here. To see all the commands which can affect the text in a buffer, take a look at :help change.txt

Still reading? Every week I publish awesome tips on Vim in my Mastering Vim Quickly Newsletter. Join thousands of other Vim fans here: masteringvim.com


If you liked the post, we should get connected - follow me on Twitter

Mastering Vim Quickly: Introduction

This post presents a chapter from my upcoming book Mastering Vim Quickly: From WTF to OMG in no time

 

Mastering Vim Quickly book

Introduction

There’s so much you want to do in life, and so little time. The story of our modern lives. Take a moment and consider how many things you want to learn. Since you’re reading this, for sure one of them is Vim. Now think, what’s holding you back from getting started? It’s hard? It takes time? Effort?

Over the years, I learned two uncomfortable truths related to learning. First: skills take time and effort to master. And second: many things aren’t fun until you’re good at them.

While learning any skill, there is a period of time in which you’re horribly unskilled, and you’re painfully aware of that fact. The same goes when learning Vim. This book presents my personal quest to quickly learn Vim, and as such, it will help you to acquire new Vim skills in record time.

One of the beautiful things about learning any subject is the fact that you don’t need to know everything. What’s important is that you only need to understand a few critically important concepts that provide the most of the value. The same goes for Vim.

Mastering Vim Quickly presents a set of fundamental Vim concepts you can use to get things done. Once you master the fundamentals, you can accomplish even the most challenging Vim magic with surprising ease. And you will love it!

Over the past few years, I read several books on Vim, passed through hundreds of tutorials and tips, used Vim from few to 10+ hours a day and coded in more than few different programming languages. Along the way, I’ve collected, distilled and refined my findings into concepts and best tips presented in this book.

If you invest the time and energy necessary to learn these concepts, you’ll easily be in top 5% of the human population when it comes to productivity in coding, programming and text editing.

Think of this book as a filter. Instead of trying to absorb all of the Vim knowledge – and
there’s really a lot out there – use this book to help you what matters the most. This way you can focus on what’s actually important: getting stuff done.

The art of learning

I’m a learning addict. I usually read few books every month. This is good, because I learn a lot. Then I try out what I learned, and adopt what works for me. In this short chapter, I will present you the three most important principles which work for me when it comes to learning.

Don’t skip them. I believe it’s very important that each one of you has the same basic start when it comes to learning techniques. I really want to help you learn Vim quickly. That’s why, pay attention to these principles. Once learned, you can use them for learning and improving on any
topic, not just Vim.

Pareto principle

Italian economist and sociologist Vilfredo Pareto (1848-1923) observed that 80% of the land in Italy was owned by 20% of the population. While investigating other countries he found the same unequal distribution of income and wealth in each.

He published his observations and the math supporting his findings. After publication, researchers in other areas of science and business began to find the same unequal distribution in their fields. That’s how Pareto’s Principle became the name of one of the most significant of universal principles.

Pareto’s Principle basically states that roughly 20% of all our actions produce 80% of our results. This means that 80% of effects come from 20% of causes. Because of these numbers, it’s also called 80/20 principle.

We know that it doesn’t have to be 80/20. It can also be 90/10, or 70/30. That’s not important right now. What you must know is that 80/20 principle works, regardless of whether you’re conscious of it working or not. This is true for your business, personal life, and everything you learn. Including Vim.

This means that, more or less, around 80% of what you’ve done today, has been pretty much worthless to your bottom line. You probably know there are things you should be doing, that you’re just not doing for whatever reason. Maybe you’re overweight or out of shape and you know you should work out more. But, back to the topic.

Why am I telling you this at all, in a book about Vim? Well, I truly believe that this principle is true. I use it in almost every important area of my life, and it gives me very good results. I also used this principle to quickly master Vim. This book provides you 20% of the most important Vim fundamentals, which will help you to learn Vim really fast.

Mini habits

In order to learn Vim, you need to commit to it.

Think of the last time you made a commitment to learn something. Or to change something in your life. It was easy to make a commitment, wasn’t it? Maybe you even had a plan! Then, forward a few weeks. Where’s that commitment? Gone, right? You’re not so motivated anymore. Your willpower is close to zero.

I know this story very well. I’ve been there, and done that. I had to read a lot and try out what works to find my way out. And I did! In the next couple of paragraphs, you’ll read the summary. That’s all you need to know.

When you commit to something, the best way to reach your goal is to create a habit. In this case, your goal would be mastering Vim quickly, and the habit you need to adopt is to regularly learn.

The biggest barrier to forming new habits is usually the fact that it takes discipline to keep doing something you don’t really feel like doing. I found a workaround for this. It’s called the mini habit. This is a game changer! And you need to know this.

I’ll show you how it worked on my own example with writing this book. As I was working on this book, sometimes I struggled to start writing. But once I start, I can easily write for hours. That’s how it works for me. The problem is that, on some days, I don’t want to start writing. So I won’t. I’m pretty sure you experienced something similar, even with different activity.

The key to forming a habit, is the consistency.

So I decided to create a new habit, and promise myself that no matter what, I’ll stick to it for the next 30 days. I promised myself to write 600 words every day for 30 days. Here’s what happened: for the first couple of days I was motivated to stick to the plan. Then my motivation got lower (always does), so I used my willpower to keep going with the habit.

However, after a two weeks or so, I missed a day. And once I missed one day, it’s was pretty easy to miss another. That’s the biggest problem with forming new habits. If you’re not consistent, you can’t create a habit.

So I finally understood the problem. When you feel resistance to something, you won’t do it. If you don’t feel like start with writing (like me in the above example), motivation and willpower can’t really help for the long-term.

With mini habits, you make a workaround for this resistance. And when there’s no resistance, you just start and do what you should.

Back to the previous example: 600 words a day doesn’t seem like much. And it’s not in the beginning. But after a week or two, as I started to lose motivation, it’s becoming harder. To solve this problem, instead of making a commitment to write 600 words a day, I’ve made a commitment to write just 50 words a day. One paragraph or one Vim tip.

When you do this, you’ll notice that there’s absolutely no resistance whatsoever. I know I didn’t feel resistance to write only 50 words. Even if I was having a bad day – I was still able to find a couple of minutes and write the damn 50 words! Anyone can do that. Actually, I realized that it was easier to write those 50 words, than it is NOT to write them.

Why? Because if you make a commitment, and no matter how good or bad you feel, you can’t motivate yourself to write the damn 50 words, then you’re basically saying yourself that you’re a big loser. Your pride won’t let you fail at something so ridiculously small. Especially if it takes less than two minutes to complete. This was a game changer for me.

You might think: “Yeah, but there’s nothing you can achieve with writing 50 words per day…”. Well, that’s absolutely wrong. How so? You can try it. Sit down to write 50 words. Or trust me. When I would start with writing (with 50 written words goal in mind), I would usually write far more than 50 words. Because once I would start, it was difficult to stop.

And this is the real power I wanted to share with you. No matter how you feel. No matter how busy you are. It’s very hard to fail with such a tiny commitment you’re after.

So, the whole trick for me was to make a commitment to write just 50 words every day. Your commitment could be learning Vim quickly. That’s why you must always go in with the intention to complete the smallest possible step. If you make it bigger, you will start to feel resistance.

Now, let’s get back to Vim. This book is carefully structured, so you can get knowledge from it step by step. Don’t make a goal to read 3 chapters of it today. Don’t make that mistake. Instead, read a page or two. And then go practice what you’ve learned.

Don’t forget. Habits require consistency. That’s how you’ll master Vim quickly.

1% improvement per day

You can’t master any skill in one day. You have to improve a little every singe day. It compounds. That’s the how you should approach to learning Vim as well.

Every day matters. You either increase your skill level for 1% or decrease it. It’s your choice. In the beginning, there’s no really difference between making a choice that is 1% better or 1% worse. It won’t impact you today. But over time these small choices compound.

When 1% compounds every day, it doubles every 72 days. If you commit to improve your Vim skills 1% every day, in one year your skills will be 38 times better!

How do you know how much is 1%? Well, when it’s about Vim, it’s hard to measure it. I would suggest that you decide what your 1% is doing to be. It can be reading one page of this book. Or learning one new Vim feature, command or trick.

Another way is to dedicate a fixed time for learning Vim every day. Let’s say that you dedicate 20 minutes every day to learning Vim. During those 20 minutes, you’ll improve your Vim skills – sometimes by 1%, sometimes by less or more. It’s not so much important to be that precise. What’s the most important here is consistency. Keep improving you skills every single day.

No Experience Necessary

Don’t worry if you’re a complete beginner. I don’t assume that you’re already good in Vim (but this book will still be very useful if you are!). You’ll find the information in this book more valuable and practical than anything you learned from other Vim resources.

Each chapter is packed with examples that support detailed explanations of all the important concepts, and they are presented in a way that helps you avoid the confusion that I faced when I was learning. With this book and plenty of practice, you will be amazed at how quickly you can go from complete beginner to super productive pro.

Mastering Vim Quickly is for anyone who wants to learn Vim, but either doesn’t know where to start. For anyone who has tried to learn it, but struggled to make progress, or was intimidated by how difficult Vim appears to be.

This book is designed to give you the head start I didn’t have. Wherever you are, if you want to learn Vim, the book will help you learn it faster and easier. Start exploring Vim today, and have fun!


If you want to get notified when I publish the book or get more content from the book for free, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.


If you liked the post, we should get connected - follow me on Twitter

5 Phases of Vim Use

 

1. WTF is going on here?

 

2. Hmm… this is cool. I want to see other interesting features this thing has.

 

3. OMG. <mind blown>

 

4. The worst thing about Vim…

worst_thing_vim

 

5. I’m actually a wizard… and this is how I feel when someone sees me using Vim

 

vim_wizard

 


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I wrote a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

 


If you liked the post, we should get connected - follow me on Twitter