Persistent Undo in Vim

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

As you already saw in the chapter on Undo/redo, Vim is pretty powerful when it comes to these features. However, there’s one more feature which I didn’t mention, as it takes a bit of configuration.

In Vim, like in every other text editor, you can perform undo/redo in your current session. Once the session is closed, and you reopen the same file, running undo will do nothing – as you will be already at the oldest change.

Vim supports persistent undo, which means that you can run undo/redo even from your previous sessions.

 

Persistent Unto in Vim

Persistent Undo in Vim

 

This is great feature indeed. This way you can go back historically through changes of any of your files.

How this works? Vim creates hidden file where it stores the undo history, for every file you edit. Now, configuration is very simple. You could add only this line to your .vimrc:

set undofile " Maintain undo history between sessions

and it would work.

However, Vim will write undo history files in the same directory as the file you edit. Overtime, this will become messy. You don’t want that. That’s why, I recommend you complete the next two steps as well.

First, create a dedicated directory for these undo history files, by running a command like:

$ mkdir ~/.vim/undodir

My assumption is that ~/.vim is your Vim directory, where your .vimrc, among others, is located.

Then, once you have created the directory, you need to add only one more line to your .vimrc file:

set undodir=~/.vim/undodir

That’s all. Vim will store all the undo history files in that directory, and you’ll have persistent undo working flawlessly.

 

 


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

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’m writing 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 want to get updates and sample content from the book, 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

Saving Read-only Files in Vim – The sudo tee trick

How many times you had the following situation: You open a file with Vim and make some changes. When you try to save the file, you realize that you didn’t run Vim with sudo?!

There is a solution for this. The next time when you get into this situation, you can use the following command in order to save the changes you made:

:w !sudo tee % >/dev/null

There’s just one problem – it’s a bit hard to remember it. That’s why, if you’re going to need it often, add the following line to your .vimrc:

cmap w!! w !sudo tee % >/dev/null

This way, when you get to the same situation again, you can just type :w!! to save the file, even if you did not run Vim with sudo.


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’ve started to write 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 want to get updates and sample content from the book, 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

What to Expect from Mastering Vim Quickly book

 

This is a post which should give you some basic info about my upcoming book, Mastering Vim Quickly The purpose of the Mastering Vim Quickly is to help you get from beginner to master of Vim in short time. That’s why the subtitle of my book is: from WTF to OMG in no time :)

 

The Reputation of Vim

There’s so much you want to do in life, and so little time. The story of our modern lives.
Take a moment to consider how many things you want to learn.

Seriously, don’t read further, but take a moment and think  :)

 

Okay. Since you’re reading this, I guess that Vim is in your to-learn list.
Now think, what’s holding you back from getting started? It’s hard? It takes time? Something else?

I can guess. You’ve heard about the reputation Vim has.

You’ve probably heard that Vim has a reputation for being difficult to learn. Or that Vim has the steep learning curve.  Or that it takes months, even years to get good at it.

 

learning_curves

Learning curves

 

Truth to be told, Vim has a deserved reputation for being difficult to learn. But, this can be avoided. There is a way to learn Vim faster that you ever imagined. Read further, and you’ll find out how.

 

The Art of Learning

Before I say anything about learning Vim, let’s just cover some general basics of learning.

Here is one uncomfortable truth: skills take time and effort to master.
There’s another one: many things aren’t fun until you’re good at them.

While learning any new skill, there is a period of time in which you’re horribly unskilled, and you’re totally aware of that fact. The same goes when learning Vim.

Mastering Vim Quickly will present my successful 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. Even only this kind of approach to learning Vim will help you learn it much faster that everyone else.

Mastering Vim Quickly represents 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! I can tell you that. You’ll enjoy even more while writing your code. You will enjoy even while editing a simple text.

Over the past few years, I’ve picked up several books on Vim, passed through hundreds of tutorials and tips, used Vim from few to 10+ hours a day while coding, writing documentation, writing blog posts, tweeting, etc. Yes, I do all of that from Vim. Along the way, I’ve collected, distilled and refined my findings into concepts and best tips. They will be presented in this book.

If you invest the time and energy necessary to learn these concepts, I believe that you will easily be in top 10% of the 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, you will use this book to help you to get what matters the most. This way you can focus on what’s actually important: getting stuff done.

 

Why another book on Vim?

You might be thinking, if there are already books on Vim, why would I write one more?

Well, what I don’t like about books on Vim is that they are too long. One of them: ~300 pages. Second: 500 pages. Third:  900 pages! That’s just way too much. I understand, Vim is really complex and powerful text editor, and has really a lot of features, but anyway, this is too much. They take too much time.

That’s just one of the reasons why I started with writing my own book on learning Vim.
Yes, I am aware that there’s big possibility that my book will become larger then I want, so that’s why I’ve decided to give myself limit right away. I think it would be ideal to have 100 pages book. But I’m not strict – I even if I reach ~120 pages, I think that’s acceptable.

So, for all of you interested in my upcoming book – now you know. It’s going to be short and really effective.

 

 

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 Mastering Vim Quickly more valuable and practical than anything you learned from other Vim resources.

I want to save you from all the difficulties that I faced, and help you understand how Vim really works.

 

The Importance of Strategy

In order to learn Vim quickly, I have created few different strategies which helped me do it. Of course, I will present those strategies in the book.

You’ve most likely heard of the 80/20 rule, or Pareto Principle. It suggests that in any endeavor, 80% of the results come from 20% of the input. For example, for a company, 80% of revenue usually comes from around 20% of its customers.  The ratios won’t always be 20% and 80%, but the point is that in anything you do, there are a few things that matter far more than everything else.

My goal is that you get that knowledge that matter the most. My book, Mastering Vim Quickly, will provide you the most important 20% of Vim knowledge.

You will learn the most important 20% things about Vim. That will get you 80% of the power of Vim and working productively with it. Simple as that.

I have to admit, I’m a learning addict. I love to read, and learn new stuff. I read 80-100 books a year in average. This helped me to actually learn how to learn effectively. I found a way to learn Vim effectively, so if I could do it – you can do it as well.

For example: when learning a new skill, what you learn and the order in which you learn it makes a big difference. In his book The 4-Hour Chef, Tim Ferriss shares his methodology for rapid skill acquisition. He says that you must first deconstruct the skill into manageable blocks, select the most important blocks (the 20%), and then learn those blocks in a logical sequence. This is exactly what I am doing.

Or another example: When you learn something new, and you have all the material you need, what else can you do to speed up learning? In his book The First 20 Hours, Josh Kaufman discusses the keys to achieving rapid progress during the early stages of acquiring a new skill.

 

“The major barrier to skill acquisition isn’t intellectual… it’s emotional” – Josh Kaufman

 

The biggest obstacle you will face in your quest to learn Vim is yourself. The fact is, like any new skill, learning Vim is tough. What makes it tough, though, is not the content, but rather the fact that you will get frustrated and feel stupid at times. Trust me, you will feel like this when you start with Vim. The road will seem too long, and it would be much easier to just give up and get back to your old text editor instead.

But I’ve passed through all of this. I’ve been there, I’ve done that. And I’ll help you to overcome this, just the way I did. Note that this is just one of the few strategies which will be presented in my book, in order to help you learn Vim quickly.

 

Work in Progress

Since there’s so much to learn about Vim, it’s not an easy task to filter all of it and keep only the best. It takes time. Also, this is going to be the first book I’ve ever published. So, I’m really trying hard to make it a good one. That’s why, book will not be ready in the next month or two – just in case you were wondering.

One more thing. You should also know that each chapter of my book will be packed with examples that support detailed explanations of all the important concepts, and they will be presented in a way that helps you avoid the confusion that I faced when I was learning. Like I said – I’ve been there, I’ve done that. I know how it feels when shit’s being weird.

With this book and plenty of practice, I truly believe that you will be amazed at how quickly you can go from complete beginner to super productive pro. From WTF (is this) to OMG (I can’t believe how awesome things I can do with Vim).

Mastering Vim Quickly is for anyone who wants to learn Vim, but either doesn’t know where to start, has tried to learn but struggled to make progress, or is 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 smarter, faster and easier.

 

If you want to get updates and sample content from the book, leave me your email below (if you did not already) , and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.

[mc4wp_form]

 


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

The History of Vim

What is Vim?

If, for some strange reason, you don’t know what Vim is, here’s the short answer:

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems. Vim is distributed free as charityware.  – http://www.vim.org/

 

You should also know that Vim (a contraction of Vi IMproved) is a text editor written by Bram Moolenaar and first released publicly in 1991. Based on the vi editor common to Unix-like systems, Vim is designed for use both from a command line interface and as a standalone application in a graphical user interface.

Simply saying, Vim is a powerful text editor for programmers, power users and command-line gurus.

 

The History of Vim

When Bram Moolenaar had bought an Amiga computer, he wanted to use the editor he learned on Unix. Since there was no good Vi for the Amiga, he started with the best that was available (a program called Stevie) and started improving it.

At first, his goal was to imitate Vi. Later new functionality was added, and  by version 1.22 Vim included more features than Vi. That time it was renamed from Vi IMitation to Vi IMproved. Over time the gap has grown wider, now it’s hard to think of a reason to use Vi instead of Vim. I would recommend all Vi users to switch to Vim.

Of course, ports to various operating systems have been done by different people. The port to Unix was a milestone, since Vim started competing with Vi. Currently Vim runs on more than a dozen different operating systems. That is awesome indeed.

Vim was made such that it works on many different operating systems. This wasn’t easy. Just supporting the most popular Unix versions is already a big task. Adding support for MS-DOS and MS-Windows gives the additional problem of using file names with a backslash in them. Machines such as the Amiga and the Macintosh have a different kind of operating system, which require specific solutions. – Bram Moolenaar for Free Software Magazine

 

The first version of Vim that was released to the general public was 1.14, which was published on November 2, 1991. Over time Vim has grown constantly. It has become a complex program. Adding functionality is taking much more effort now than before.

Here’s the timeline of Vim development:

1988       Vim 1.0     Vi IMitation on the Amiga
1991       Vim 1.14    First public release (on Fred Fish disk #591)
1992       Vim 1.22   Port to Unix, renamed to Vi IMproved
1994       Vim 3.0     Multiple windows
1996       Vim 4.0     GUI
1998       Vim 5.0     Syntax highlighting
2001       Vim 6.0      Folding, multi-language
2006       Vim 7.0      On the fly spell checker, support for tabs

The list shows only the most important features, but each version brought us a lot of nice features. For example, if we take version 6.0: the major new feature was folding. But, there were other great features such as UTF-8 support, multi-language support (translated messages and menus), vertical window split, improved performance for syntax highlighting, etc.

Vim has grown to an big open source project, but it’s also important to mention that it’s charityware. The basic idea with charityware is that everybody who uses Vim is asked to donate to a charity. Thus the use of Vim is free, if you think it’s worth something, you can give that money to a good cause.  Moolenaar helped with establishing a foundation called ICCF Holland that works to support to a children’s center in Uganda.

 

Vim today

Unlike the other vi clones, Vim is still actively developed and has a large base of contributors. New features are still being developed and added to the text editor every year, making it even more powerful. Besides that, number of Vim plugins is rapidly growing.

At the moment, Vim 7.4 is the latest stable version. It is highly recommended to update your Vim if you didn’t, because many bugs have been fixed since 7.2 and earlier.

Vim has been my editor of choice since 2008, few months after Linux became my main desktop operating system. In a meanwhile, I have changed few different distributions of Linux, but Vim was always there.

I’ve used it to write many lines of code, edit lots of configuration files, and write tons of documentation. Although I’ve tried to use some of the modern text editors, I haven’t found any that match Vim’s efficiency.

I’m not the only one who, after using Vim almost every day for so many years, is still discovering new features, capabilities, and useful tips that further improve my productivity.

At the moment, my favorite work environment is Gentoo with i3 window manageroh-my-zsh and Vim. I have never been more productive.

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’ve started to write 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.

Sign up below for updates about the book.

[mc4wp_form]


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