26

As a complete beginner with no vi experience, how can I learn to use Vim using just Vim itself? Is there a built-in tutorial, and how can I access it?

Assume I have a laptop with Linux and Vim installed, and no Internet connection.

2
  • 4
    Start with vimtutor for the first 30 minutes. Continue with real practice for the rest of the week. Boom! You can fluently use Vim and have all its pros.
    – gon1332
    Commented Feb 3, 2015 at 19:28
  • 1
    I found this eBook by Swaroop C H, very useful: files.swaroopch.com/vim/byte_of_vim_v051.pdf
    – senpai
    Commented Feb 23, 2016 at 0:00

5 Answers 5

28

vimtutor is a program that comes with vanilla vim installations. It's a 30-minute-ish tutorial program that explains the basics of vim. On *nix systems, you generally only need to execute vimtutor on the command-line. Unix also has gvimtutor available, if you'd prefer a GUI version.

On Windows, vimtutor.bat is located in $VIMRUNTIME (generally something like C:\Program Files (x86)\Vim\vim74 or similar). vimtutor.bat will default to GUI vim on Windows; pass -console if you want to force it to use console vim. On Windows 8, at least, you can also just search for 'vim tutor' (without the quotes, of course) from the Start Menu to open the tutor.

20

You can learn Vim by reading :help from top to bottom. When you enter :help, it looks like this:

*help.txt*      For Vim version 7.3.  Last change: 2010 Jul 20

                        VIM - main help file
                                                                         k
      Move around:  Use the cursor keys, or "h" to go left,            h   l
                    "j" to go down, "k" to go up, "l" to go right.       j
Close this window:  Use ":q<Enter>".
   Get out of Vim:  Use ":qa!<Enter>" (careful, all changes are lost!).

Jump to a subject:  Position the cursor on a tag (e.g. |bars|) and hit CTRL-].
   With the mouse:  ":set mouse=a" to enable the mouse (in xterm or GUI).
                    Double-click the left mouse button on a tag, e.g. |bars|.
        Jump back:  Type CTRL-T or CTRL-O (repeat to go further back).

Get specific help:  It is possible to go directly to whatever you want help
                    on, by giving an argument to the |:help| command.
                    It is possible to further specify the context:

As you can see, it starts from the essentials, such as moving around, jumping to subjects and back, and so on. If you scroll down a little bit, this list of documents becomes visible:

------------------------------------------------------------------------------
                                                *doc-file-list* *Q_ct*
BASIC:
|quickref|      Overview of the most common commands you will use
|tutor|         30 minutes training course for beginners
|copying|       About copyrights
|iccf|          Helping poor children in Uganda
|sponsor|       Sponsor Vim development, become a registered Vim user
|www|           Vim on the World Wide Web
|bugs|          Where to send bug reports

USER MANUAL: These files explain how to accomplish an editing task.

|usr_toc.txt|   Table Of Contents

Getting Started
|usr_01.txt|  About the manuals
|usr_02.txt|  The first steps in Vim
|usr_03.txt|  Moving around

Using the navigation tips that were given at the top (and Ctrl] to follow a link) you can start visiting the sections, for example starting from tutor. (This is actually how I learned Vim in the first place.)

1
  • 1
    This makes me feel silly that I never ran this command. I use the built-in help all the time. I learned the Ctrl-] and Ctrl-t shortcuts from a random tips website, and I thought they were ingenious and tremendously useful, and wondered why I hadn't heard about them...duh...I never ran just :help and started reading.
    – Wildcard
    Commented Oct 11, 2015 at 3:23
9

Like others have mentioned vimtutor is a good place to start, there are several other resources available to you for improving on your knowledge & understanding of the vim philosophy, which needless to say is not only brilliant but surprisingly & pleasantly intuitive.

However I would still like to use an example to draw a parallel to how one should approach learning vim.

As we are born, we are pretty much immobile, we need to be carried to wherever we need to go, slowly our legs & arms develop muscles and gradually with practice we learn both how to use them & control them precisely to make use of them to start crawling so we can explore our surroundings ourselves. As we grow further and develop more muscles, strength & coordination we start to walk and with some more of the same we eventually are able to run. Now you might think this would be the end of it but it isn't, after we're fairly comfortable walking / running to wherever we desire, we feel the need to go farther and farther and realise the need for more tools (different vehicles) to go farther and faster.

Learning vim IMO should be similar to that, in that you should learn the basics first and then gradually and incrementally keep learning more advanced patterns, ideas, plugins to keep improving your workflow. With practice everything you learn would get embedded in your muscle memory and become second nature and that would the time for you to learn further more tricks and repeat this cycle. Since VIM is still under development and adding new features (there's also NeoVIM), this process can go on forever.

After a certain point, when VIM's philosophy has embedded deep in your muscle memory you will stop spending time thinking about how to do what you want and be completely focused on what you really want to do. When that happens you will be feel frustrated at how painfully slow and awful it is to edit text in other applications / editors that do not have some decent support for emulating vim.

6

You can learn the basics by running vimtutor at the command line. This ships with vim and provides a basic interactive tutorial on it's use.

2

I'll mostly second @janos's answer.

I learned vim by

  1. Running vimtutor
  2. Deciding I would program exclusively in vim
  3. Reading :help top-to-bottom
  4. Continuing to program exclusively in vim
  5. Reading Practical Vim by Drew Neil
  6. More programming in vim
  7. &c.

I now spend a lot of time on Vi & Vim, as well as writing VimScript.

I'll add that my choice to program in vim led to learning the ins-and-outs of other tools (e.g., make, javac) to facilitate my programming. I also developed a strong value for good CLI tooling and now have a gut instinct for the quality of CLI programs (hint: I always ask myself, "Could I use this from Vim? And would it be easy?"). In the same way, I've developed a gut instinct for the quality of vim plugins: I much prefer those that enhance existing behavior or fit nicely into vim's concepts (e.g., operators and motions) over those that seek to retrofit other concepts on top of vim. (This purism is not for everyone, and is not even the most extreme vi(m)-purism out there. It works for me, though, because I like the language of vim, and want my commands and plugin mappings to make sense in the context of vim.)

I don't live in vim. I live in a terminal (usually in tmux). But I am in-and-out of vim all day long, and that makes a difference for learning how to use it.


One last piece of advice: make it easy to get into vim. Here's a short listing of code that has helped me along the way.

Shell functions

Most up-to-date is always in my Dotfiles

v() {
  vim "$@"
}

vv() {
  vim +'edit $MYVIMRC' "$@"
}

vq() {
  if (($# > 0)); then
    vim -q <("$@" 2>&1)
  else
    printf '%s\n' 'Usage: vq cmd' '' 'Use {cmd} output as quickfix list'
  fi
}

vf() {
  if (($# > 0)); then
    vim $("$@")
  else
    printf '%s\n' 'Usage: vf cmd' '' 'Use {cmd} output as filenames' \
      'Brittle: {cmd} output will be word-split'
  fi
}

vff() {
  if (($# == 1)) ; then
    # equivalent to vf ack -g "$1"
    vim $(ack -g "$1")
  else
    printf '%s\n' 'Usage: vff filename' '' 'Find {filename} to edit' \
      'Relies on ack(1)'
  fi
}

vc() {
  if (($# > 0)); then
    if pushd "$1" >/dev/null ; then
      # ${@[0]} = $0 = name
      # ${@[1]} = $1 = directory
      # ${@:2}  = remaining args
      vim "${@:2}"
      # this must work because pushd succeeded
      popd >/dev/null
    fi
  else
    printf '%s\n' 'Usage: vc dir [args]' '' 'Execute vim in {dir}'
  fi
}

vs() {
  if (($# > 0)); then
    local session="$1"
    shift
    vim "$@" -S "$session"
  else
    vim -S
  fi
}

# Start vim with its last cursor position
lvim() {
  vim +'normal '"'"'0' "$@"
}

vh() {
  vim +"help $*" +only
}

vgrep() {
  local pattern="${1////\\/}"
  for f in "${@:2}" ; do
    if [[ -f "$f" ]]; then
      ex -N -i NONE -n -- "$f" <<<"argdo global/$pattern/print" \
        | sed "s/^/${f////\\/}:/"
    # else
    #   printf '%s\n' "${FUNCNAME[0]}: $f: Is a directory" >&2
    fi
  done
}

vw() {
  if [[ "$(type -t "$@")" != file ]]; then
    printf '%s\n' 'Usage: vw cmd' '' 'edit file defining {cmd}'
  else
    vim "$(command -v "$@")"
  fi
}

Shell variables

EDITOR=vim
VISUAL=vim
MANPAGER=vim -M +MANPAGER -

Tmux bindings

# create a throwaway vim
bind-key v new-window -a vim
bind-key V command-prompt -p 'vim +' { new-window -a vim +"%%" }

Not the answer you're looking for? Browse other questions tagged or ask your own question.