Files
memex/0_inbox/Scrivener_Emacs_Vim/content/VimValleyExample.org

17 KiB
Raw Blame History

Vim Valley Course

Scratch

This section is for random mental outbursts related to this project that I will organize later.

Prose, posts

Replacing Scrivener with Emacs and Vim

Notes

Misc stuff here

Launch list

Things left to do before launch go here

Something awesome
A subitem for something awesome

Blah blah this is very imaginative sample text. Tremendously imaginative.

Something else awesome
Another subitem

Blah blah this is very imaginative sample text. Tremendously imaginative.

Task List, Timeline

CURRENT:

Done stuff

Course Outline

A better way of moving

Getting started with Vim Valley

check out http://vimvalley.com for the full course.

If you're new to Vim, the thing that is most confusing is that it's something called a modal editor. That means that when you press keys in Vim, they do different things depending on what mode you're in.

Don't worry, it will make complete sense soon. For the first few sections of the course, we're going to focus on the two modes you'll use the most: normal mode, and insert mode.

If you're not new to Vim, you'll breeze through the first few sections, but will probably pick up a few new tricks along the way.

## Normal Mode

Normal mode is where you're going to spend most of your time. After you get used to it, you'll pity the misguided souls who edit text in any other way.

Normal mode has an abundance of useful movement and editing commands available to you. Almost every key does something in normal mode. What keys don't do in normal mode is actually type characters. For example, pressing the `w` key doesn't type w it moves the cursor forward a word.

The first way you'll see how different Vim keybindings are is when you see how you move the cursor around in Vim. While you can use the arrow keys to move around, it's strongly discouraged. To move the cursor left, down, up, and right, you use the `hjkl` keys, respectively.

Look at where the hjkl keys are on the keyboard. They are sitting right underneath your fingers in the touch typing position. One of the things that makes Vim so fast and efficient is that you can do everything without changing your hand position.

Constantly reaching over to the arrow keys wastes a lot of your time when added up over the thousands of times a programmer (or non-programming writer!) needs to move the cursor every day. It also hurts your wrists, contributes to RSI, and likely causes a cute baby seal somewhere to cry.

Consider normal mode as the moving around mode. You can also delete and change things in this mode, but we'll get to that soon.

## Get started with the course!

Time to roll up our sleeves. Let's take a quick tour of the Vim Valley interface, and complete a very short exercise. Click the button below to begin.

<button id="welcomeTour" class="btn btn-lg btn-primary">Take a tour of the Vim Valley course interface.</button>

=ThingsToKnow=

h :moves the cursor left
j :moves the cursor down
k :moves the cursor up
l :moves the cursor right
w :moves the cursor forward one word

=EndThingsToKnow=

Bread and butter

This exercise is going to give you a whirlwind tour of some of the commands you'll use to move around in Vim's normal mode.

You'll be using these movement commands many times in future exercises, so don't worry about memorizing them all right now.

You may have noticed green and red lines underneath characters in the editor, like this: <span class="startPosition">h</span>ell<span class="endPosition">o</span>.

The green underline marks the start position for the current step. If you want to reset only the step of the exercise, rather than the whole exercise, press `F2`. This will move the cursor back to the start position for the current step.

The red underline marks the end position for the step.

In future exercises, not all steps involve movement so these lines will not always be there.

Let's take a look at the bread and butter Vim movement commands.

=ThingsToKnow=

h :moves the cursor left
j :moves the cursor down
k :moves the cursor up
l :moves the cursor right
w :moves the cursor forward one word
b :moves the cursor to the beginning of the word
e :moves the cursor to the end of the word
g g :moves the cursor to the beginning of the file
G :moves the cursor to the end of the file
0 :moves the cursor to the beginning of the current line
$ :moves the cursor to the end of the current line

=EndThingsToKnow=

Movement within lines

Moving by words is great, and it's faster than moving character by character. But there are usually even faster ways to get to where you want to in Vim.

There are commands that let you move with laser-like precision to where you introduces you to the commands that you'll frequently use to move within the current line.

These commands are useful on their own for movement purposes, but their real power is in how well they combine with other Vim commands, which we'll learn about in a later lesson.

Lets see how we can move exactly where we want to on a line, with a surprisingly small number of keypresses.

=ThingsToKnow=

f {char} :"find", this command moves the cursor to the next {char} on the current line
F {char} :reverse "find", this command moves the cursor to the previous {char} on the current line
t {char} :"till", this command moves the cursor just before the next {char} on the current line
T {char} :reverse "till", this command moves the cursor just after the previous {char} on the current line
; :repeat the last search in the same direction
, :repeat the last search in the opposite direction

=EndThingsToKnow=

A better way of editing

First edits from normal mode

We're going to be staying in normal mode again, but this time we'll actually be editing text.

This is a good time to introduce you to the concept of composability which is a big part of what makes Vim so powerful.

Composability means that you can take many of the Vim commands you learn, and combine them together to make new commands. Like a composer turns notes into songs. Or how a writer turns words into sentences.

Understanding and using composability is a big part of making the most of Vim, and we'll be exploring it in more detail soon.

We're going to learn 2 new normal mode mode commands for this exercise: `x` and `d`. `x` deletes the character under your cursor. `d` doesn't do anything by itself it needs to be combined with a subject or motion command in order to delete things.

`dw` - delete word: from cursor to next word

`db` - delete to beginning of word: from cursor to beginning of the current word

Composing commands in Vim is a lot like composing sentences. You have a verb (such as `d`) and a subject (`b` or `w`). As you go through the course, you'll add to your Vim vocabulary and be able to compose your own Vim "command sentences" with ease.

This lesson also introduces Vim's undo and redo commands from normal mode.

=ThingsToKnow=

x :delete the character under your cursor
d&nbsp;{subject} :delete, this command needs to be combined with a subject that defines what it deletes.
d w :deletes from the cursor position to the next word
d b :deletes from the cursor position to the beginning of the current word (or to the beginning of the previous word, if already at the beginning of a word)
u :undo last action
ctrl+r :redo last action

=EndThingsToKnow=

Insert mode: dangerous familiarity

Now we'll introduce a second mode in Vim: insert mode. When Vim is in insert mode, it works almost exactly like any other editor. Pressing the `j` key will type the j character while in insert mode, not move the cursor down a line.

Your goal while mastering Vim should be to spend as little time in insert mode as possible. Staying in insert mode too much will keep you from realizing the true power of the editor.

As a masterful Vim user, you'll primarily be in normal mode. Normal mode is what you use to move around and is also the most efficient way to make most edits.

There are many ways to enter insert mode. The two simplest ones are `i` and `a`. Think of `i` as insert, and `a` as append. Pressing `i` in normal mode will put you into insert mode just in front of the cursor. Pressing `a` will put you in insert mode just after the cursor.

Another common way you will move from normal to insert mode is by using the `c` command. This is Vim's change verb, and it's very similar to the `d` delete verb. It also needs to be combined with a subject to do anything. The primary difference between `c` and `d` is that `c` will put you into insert mode after it removes the text you tell it to remove. So that you can change the text. Clever huh?

After you enter insert mode, you want to make your edits and additions and then switch back to normal mode as painlessly as possible.

The default key to exit insert mode and get back to normal mode is the `Esc` key. Look at where that key is on your keyboard. It's so far up there in the corner, it forces us to move our hands! This is a problem, because we're going to be switching from insert mode back to normal mode a lot in Vim.

The reason that the `Esc` key is used for such a frequently used action is historical. The creator of Vi used a keyboard where the `Esc` key was much easier to reach.

<img src="/lib/img/KB_Terminal_ADM3A.svg" alt="The original keyboard layout used by the creator of Vi" style="width: 100%;"/>

Since our keyboards are a bit different now, using `Esc` is a lot less attractive. Remember, we never want to have to move our hands from their comfortable home positions.

Don't worry, in the next lesson we'll show you a better way. But for now, go ahead and allow your hand to make the tortuous journey to the `Esc` key. It will help you appreciate not having to do it again after the next lesson.

=ThingsToKnow=

c :"change", this command needs to be combined with a subject that defines what it changes. Similar to the `d` command
i :"insert", puts you into insert mode just before your cursor
a :"append", puts you into insert mode just after your cursor
Esc :exit insert mode

=EndThingsToKnow=

A better way of switching modes

You'll be switching from insert mode back to normal mode a lot. You don't want to have to reach way up there to the `Esc` key every time you need to leave insert mode.

I recommend setting up your editor so that pressing the `jk` keys will escape from insert mode when they are pressed quickly one after another. This is a popular customization and you should be able to set this up for whichever Vim client or Vim enabled editor that you use.

The Vim Valley editor is already configured to exit insert mode when `jk` is pressed, and in this exercise you'll practice switching modes in this way. Try to press `jk` as one rolling motion, and before long it will become second nature.

If you're already a Vim user and are used to using `jj` to exit insert mode, that works in the Vim Valley editor too! But for new Vim users, I recommend `jk`. I find this combination is easier on the fingers over the long term. Another neat advantage of `jk` is that if you press this accidentally while already in normal mode, it leaves your cursor exactly where it was before.

This is the only custom mapping that is in the Vim Valley editor. Everything else here uses default Vim settings.

Something else useful to know if you're using Vim on a system where you haven't loaded your `jk` customization is that `Ctrl+c` works as a substitute for `Esc`. `Ctrl+c` is a default Vim keybind to exit insert mode, and it works everywhere. This is much better than reaching up to the `Esc` key, and is a good second choice if `jk` doesn't work on the machine you are on.

Lastly, you might have noticed that in the last exercise we were adding things to the beginning and end of lines pretty often. This is something that we do often as writers or programmers so Vim has a shortcut for them. They are called combination commands, which are commands that do two or more things with one keystroke. The two we'll introduce in the section are:

`I` - This command combines `0` and `i`. It puts you into insert mode just before the first non-whitespace character of the line you are on.

`A` - This command combines `$` and `a`. It puts you into insert mode at the end of the line that you are on.

=ThingsToKnow=

j k :when in insert mode, type j k quickly to switch back to normal mode.
Ctrl+c :this key combo also escapes from insert mode
I :puts you into insert mode just before the first non-whitespace character of the current line
A :puts you into insert mode at the end of the current line

=EndThingsToKnow=

You'll want to start using Vim in your actual editor as soon as possible. So check the following link for how to configure a large number of editors to work with `jk`.

How to map in your editor:

Vim:

Sublime Text Vintageous:

Emacs EVIL mode:

I'd also like to mention a large efficiency improvement that is somewhat outside of the scope of this course. Look at the CAPSLOCK key. It's in prime real-estate, and is almost never used. Remapping the CAPSLOCK key to CTRL will make all CTRL-<key> bindings much easier to hit. There are many ways to do this and your options vary depending on which OS you are using.

Key Vim concepts

Talk to Vim like it's a person

To be a power user of any editor, you need to learn the editor's keyboard shortcuts.

In the case of common shared shortcuts these aren't too bad. For example, if you've used a computer for a while you've likely already internalized the shortcuts to save a file, copy, and paste.

You start to run into problems when you want to do something a little more specific. When this happens, most editors fail in one of two ways:

  1. They have no shortcut available for what you want to do, and you have to laboriously select the command from a menu. Many times going through multiple levels of menus. Major waste of time.
  2. The shortcut is something long and almost impossible to remember like `Ctrl-x Ctrl-h Meta-z k`. Good luck remembering a hundred or more of those shortcuts, some of which you might only use infrequently. Almost as bad as having no shortcuts at all.

Vim has solved this problem with that thing we mentioned earlier: composability. You only have to remember a relatively small number of commands and and you can compose them into a massive amount of very specific text editing and movement commands.

Not only that, but most of Vim's commands are basically English sentences! Vim's "command sentences" are composed of a verb and subject. Take one of the verbs you've learned so far, `c`. Do you want to change a word? You tell vim `cw`. Verb (change) + subject (word). That's pretty easy to remember compared to `Ctrl-x Ctrl-h Meta-z k`.

You might have noticed that the Vim Valley exercise engine has a line above it that starts with the text "Key buffer" and sometimes has some stuff in it. This is to help you understand what Vim is doing behind the scenes.

Whenever you type a command that doesn't do anything by itself, like `c` or `d`, Vim goes into what is called operator pending mode. This is where Vim is asking you to complete the sentence. Change what? Delete what? It's as if you told Vim "Sally bit…" and Vim is hanging on you to tell it what exactly Sally took a bite out of. Hope it wasn't human.

You'll know that Vim is in operator pending mode whenever there's anything displayed in the key buffer. You might be surprised at some of the commands that put you there.

This exercise will introduce a new verb: `r`. This command replaces the character that is under your cursor with whatever you type. Pressing `r` in normal mode will also put you in operator pending mode, as Vim waits for you to tell it what character you want to replace it with.

You can also cancel a Vim command sentence halfway through by either `Ctrl+c` or `Esc`. That will tell Vim nevermind, Sally didn't bite anything, continue on with normal operations. It will exit operator pending mode.

You'll also learn a frequently used shortcut: `dd`. This deletes the current line, no matter where your cursor is on it. Faster than `0d$` or `$d0`.

=ThingsToKnow=

r :replace the character under the cursor
Esc :exit operator pending mode
Ctrl+c :exit operator pending mode
d d :shortcut command to delete the current line

=EndThingsToKnow=

The around and inside modifiers
Visual mode introduction
Text objects

Rounding out the fundamentals

Yanking and pasting
Combination commands
Repeating commands X times
What's in a word?
The magical dot command
Couple unsorted things not worth a full lesson

Moving faster

Screen based movement
Moving by searching
Scratch? Moving by text objects
Searching with * and #
Bookmarks for writers
Moving by enclosures

Editing faster

Editing with linewise movements
Editing by searching
Line creation and joining
Manipulating case

Registers, the temporary storage of Vim

What's a register, anyway?
Named registers
The many uses of registers

Visual modes in detail

Getting to know the visual modes
Faster ways to select

Some Final Essentials

Macros
Search and Replace
What next?

SCRATCHED

Lessons that I removed were here