Learning vim Text Editor

Home / Learning vim Text Editor

vim controlling text with your mindThe text editor is where you will live, so choose it wisely. vim stands for vi iMproved, a text editor that goes back to the beginning of time—or at least 1976, and will be the main thing that computer pioneer and co-founder of Sun Bill Joy will probably be remembered for.

.vimrc – the one customization I must tolerate

I don’t like customizing my systems—like, almost at all. I’m a big believer in stripped down vanilla systems, where everything that you don’t actually need or use has been removed. You then know what to focus on mastering, so that mastery is universally applicable. When all is said and done, all you should be relying on is the presence of the Unix/Linux BASH command-line shell and a hand-full of Unix/Linux commands that you can use there. The vim text editor (or at least, vi) is always one of them. And even that, I don’t really WANT to customize, but I find it’s the one thing I really have to in order to be optimally productive. And that’s done by using vim itself, by editing the file ~/.vimrc. Here’s mine:

  1. colorscheme blackboard
  2. if &term =~ "xterm"
  3.  set t_Co=256
  4.  if has("terminfo")
  5.    let &t_Sf=nr2char(27).'[3%p1%dm'
  6.    let &t_Sb=nr2char(27).'[4%p1%dm'
  7.  else
  8.    let &t_Sf=nr2char(27).'[3%dm'
  9.    let &t_Sb=nr2char(27).'[4%dm'
  10.  endif
  11. endif
  12. syntax on
  13. syntax enable
  14. autocmd BufEnter * :syntax sync fromstart
  15. set mouse=a
  16. set encoding=utf8
  17. set expandtab
  18. set tabstop=2
  19. set softtabstop=2
  20. set shiftwidth=2
  21. set nowrap
  22. set nopaste
  23. set textwidth=79
  24. set autoindent
  25. set backspace=indent,eol,start
  26. set incsearch
  27. set ignorecase
  28. set ruler
  29. set wildmenu
  30. set foldlevel=0
  31. set clipboard+=unnamed
  32. set background=dark
  33. set mat=5
  34. set winheight=15
  35. let @j = '/boj^Mo^M^M^M^[kkk80i-^[j! date^Mjzzi'
  36. " Toggle spell checking on and off with `,s`
  37. let mapleader = ","
  38. nmap <silent> <leader>s :set spell!<CR>
  39. set spell
  40. hi SpellBad ctermbg=52
colorscheme blackboard
if &term =~ "xterm"
 set t_Co=256
 if has("terminfo")
   let &t_Sf=nr2char(27).'[3%p1%dm'
   let &t_Sb=nr2char(27).'[4%p1%dm'
 else
   let &t_Sf=nr2char(27).'[3%dm'
   let &t_Sb=nr2char(27).'[4%dm'
 endif
endif
syntax on
syntax enable
autocmd BufEnter * :syntax sync fromstart
set mouse=a
set encoding=utf8
set expandtab
set tabstop=2
set softtabstop=2
set shiftwidth=2
set nowrap
set nopaste
set textwidth=79
set autoindent
set backspace=indent,eol,start
set incsearch
set ignorecase
set ruler
set wildmenu
set foldlevel=0
set clipboard+=unnamed
set background=dark
set mat=5
set winheight=15
let @j = '/boj^Mo^M^M^M^[kkk80i-^[j! date^Mjzzi'
" Toggle spell checking on and off with `,s`
let mapleader = ","
nmap <silent> <leader>s :set spell!<CR>
set spell
hi SpellBad ctermbg=52

Why vim matters

Vim’s predecessor, vi, is actually part of the Unix standard, meaning that it is on nearly every machine Unix and Linux machine. It has a huge learning curve and reputation for being the editor-of-the-beast—but with its vocabulary-like keystrokes, you can slam a novel’s-worth of text around with the same ease as making tiny little edits. Therein lies both its initial difficulty and its brilliance. Take a deep breath, and prepare to re-wire your brain. It’s decisions like this that make all the difference in life. I myself am still learning it, and this is where I track my little realizations that build up on each other, gradually giving me near telepathic control over the text on my screen.

Indoctrination to the editor-of-the-beast

vim doesn’t let you just load the editor and start typing as you would with any other editor, so don’t bother trying. I will resist the temptation to make this a step-by-step vim tutorial, as you will find elsewhere on the net. Instead, I will just introduce you to the most important thing you need to know to get out of hell.

To run vim and start creating a new file, go to a Linux or Unix Terminal window, and type…

vim somefilename.txt

…look around. Then immediately type:

[ESC]
:q
[Enter]

That will get you out of vim and back to the Terminal. If you by some miraculous chance actually typed something on the screen, and it won’t let you quit because you didn’t save your changes, you can type:

[ESC]
:q!
[Enter]

The exclamation point after the q tells vim that it’s okay to quit without saving changes. Congratulations. You have some command over vim. As you can see, even just loading and quitting the program requires a bit of indoctrination. If you actually wanted to save the edits you may have accidentially done, you would type:

[ESC]
:w
[Enter]
:q
[Enter]

…or alternatively…

[ESC]
:wq
[Enter]

Now all these leading [ESC]’s aren’t really necessary. They are just to ensure that you are in command-mode and not insert-mode. The Escape key always takes you out of insert mode and back into the default command mode. Command mode is active by default, and this is one of the most profound things about vi and vim that you have to grok. You deliberately put yourself into insert mode for those moments when you actually are typing in new text. And so, to complete this super-brief vim tutorial, you could actually have made some edits with a session that looked like this:

[ESC]
i
Blah blah hello world
[ESC]
:wq
[Enter]

At the end of such a session, you will now have a file in your working directory named somefilename.txt that contains “Blah blah hello world”. I will note that these colon-commands are only one way of issuing commands to vim, so that you can actually see them on the screen. For historical reasons, these are called ex commands, because of a program that is even older than vim. When you hit “i” to get into insert mode, you were issuing another type of command that doesn’t require a colon to be typed first, and which doesn’t show on the screen. My last tutorial item I will do is to show you how language can be built up with these types of commands. So, open your file, and type:

[Esc]
50i
foo
[Esc]
:wq

You will now have fifty foo’s on your screen, and saved into your file. This is because any command can take a number before it to tell you how many times you want the command repeated. In this case, you are telling it:

Fifty times insert “foo”

It is useful to think of vim commands as being verbalized and expanded in this fashion, so that you can actually start to understand the text-control language that is at play here, and how you can build up your own sentences to do things that were never explicitly imagined by the people who wrote vi and vim. You can actually copy and paste paragraphs of text around this way, delete every third word, or a magnitude of other things. The sooner you realize you are dealing with a syntactical language for moving text around, of which insert-mode is only one tiny piece, the better off you will be.

That is all the indoctrination you will get from me. The rest is going to be the discreet individual discoveries that I often think of as revelations, which build up upon each other, first to regain some semblance of competency again in this totally alien environment, and then to gradually start accumulating text-editing super-powers.

Little vim Revelations

[wp_super_faq show_specific_category=vim]