solairis.com

ViM Notes

There are many flavors of the text editor, Vi. My personal favorite is Vi iMproved. Some of these tips may only apply to the "improved" features of Vi. But alot of them should be good for the other versions as well.

While there is a wealth of information over at the ViM Documentation Project and on the Vim site itself it can be a bit overwhelming. The big problem I've found is that what I want to accomplish, however simple it may seem, could actually be a complex string of commands in Vi. So all the documentation is there, I just don't know what I'm doing with it. This is a collection of my adventures in vim.

Table of Contents

.vimrc ^

My .vimrc

So I can have it wherever I go.

set autoindent
set wmh=0
syntax on

" Easy-on-the-eyes colorscheme for black-background terminal
"colorscheme elflord

set nu
"set cul

" Map Ctrl-j and Ctrl-k to move split screens up and down.
"map  j_
"map  k_

" The next two functions do tab completion with ^n and ^p
function InsertTabWrapper()
 let col = col('.') - 1
 if !col || getline('.')[col - 1] !~ '\k'
   return "\"
 else
   return "\"
 endif
endfunction

function InsertTabWrapperShift()
 let col = col('.') - 1
 if !col || getline('.')[col - 1] !~ '\k'
   return "\"
 else
   return "\"
 endif
endfunction


" Filetype plugin
filetype plugin on
filetype indent on

"execute pathogen#infect()

" June 27th, 2018
autocmd BufRead,BufNewFile /usr/local/etc/vhosts.frontend.d/solairis.com.conf set syntax=apache
	

General Editing ^

Moving Around ^

In general you should move around with the hjkl keys. If you use the arrow keys then quickly break that habbit. You will find your life much happier.

You can quickly jump to a line by doing :# where # is the line number. So :1 (or :0) will take you to the first line. :42 will take you to the 42nd line. G takes you to the last line.

$ and ^ will take you to the end and beginning of a line, respectively.

i will start you inserting under the cursor, a just to the left of it, A at the end of the line, and I at the beginning of the line.

x deletes the character under the cursor. 5x will delete 5 characters starting wit the one under the cursor (you could now paste these characters).

dd deletes the current line. d13d deletes 13 lines (you could now paste these lines).

Copying (Yanking) Text ^

To yank (copy) text simply yy to yank a line. y78y yanks 78 lines. yl will yank the character under the cursor, yh the character left of the cursor, y3l the next 3 characters, yw the next word and so on. Now that you've yanked it you should paste it.

Pasting Text ^

Yanked and deleted characters and lines can be pasted. p pastes text under or below the cursor and P pastes left of or above the cursor. 10p pastes what is in the buffer 10 times.

Undo/Redo ^

Uh oh, you made a mistake. No prob, just u to undo or 12u to undo your last 12 actions. Ooops! Now you've undone too much! Just :redo to, well, redo. If you get lost between all of the un and redoing, then you can always revert to the last saved version of your file with :e!) (you are saving your files constantly right?).

Formatting (Word Processor) ^

You can get vim to act like a "Word Processor" in that it will automagically flow a paragraph together as you add/delete words. The trick is telling vim how to define your paragraphs. By default it assumes paragraphs are separated by a blank line. So a simple :set formatoptions=a will do the trick for that. You can, however, get a little fancier if you want.

Multiple File Editing ^

Working with multiple files at once is easy in Vi, you just have to learn a few things. But after that it is extremely fast and efficient to move between the files and copy and paste and such.

Split Screen Goodness ^

ViM w/Split Screens I knew that you could have split screens in vim. Just do: :sp otherfilename and you would now have two "screens" open in vim with a different file. For a completely blank screen just do :sp. However, when you do this, your screen divides into two equal parts. And moving between the two windows is a little bit clunky. I came across this bit of configuration which allows you to quickly move through the windows, having the one that you are working on take up most of the screen:

set wmh=0
map <C-J> <C-W>j<C-W>_
map <C-K> <C-W>k<C-W>_
Just put this into your ~/.vimrc file and you are ready to go. Ctrl+k moves up a screen and Ctrl+j moves down a screen. I chose those keybinding because they are already familiar to the classic hjkl move keys.

If you like this one then you will find it useful to open multiple files on the command line as well as close multiple files.

Resize Split Screens ^

To resize the splits do Ctrl+w + and Ctrl+w -. Resize multiple rows at a time with 10 Ctrl+w +. Or use :resize. :res +10

Opening Multiple Files at Once ^

Sometimes I want to open a bunch of files at once. Like all the .html files in one directory. Or all the .pl, .php, .jsp files etc. Well you can do it by starting vim with all the files on the command line:

$ vim *.html
Now you have all of the files. You can move through them by doing :next and :prev. You can even copy and paste between them. But its too much work to type :next to get to the next file. And if I make a change to the first file but I don't want to save it until after I see the next file, I can't do it. Vim won't let you move to the next file until you've saved the first. You can override this with :next! but then you've lost the changes. Wouldn't it be nice if you could open all your files (as per above) on the command line and then put them into split screens to move between easily? Well you can. And its very easy. After opening your files just do a :all and Vim will load em all up. Or you can vim -o *.html to open them all up at once or vim -o2 *.html to only open 2 up into the buffer.

Note: It is also nice to be able to close mulltiple files.

Closing Multiple Files ^

If you have a lot of screens open then at some point you will need to close them all. Sometimes you work through the files, closing one-at-a-time until you are all done. That works well, if thats what you do. But I often find myself with 10-15 screens open and its time to call it a day. Well you could do :wq or ZZ 10-15 times. Or you could do :wqall. Thats it. Nothing fancy. To quit them all without saving you would :qall!. I think you get the drift.

Searching ^

/ in command mode will start searching a file for that string. After you hit Enter, n will search forward for that string again and N will search backward. You can use regular expressions.

To search case-insensitively add a \c somewhere. /\cthank you will match "thank you", "Thank You", "THANK YOU" and everything in between.

...and Replacing ^

:%s/search/replace/g will search an entire file and replace each occurance of "search" with "replace". You can use regular expressions. % means "search the whole file." You can limit that in different ways.

If you want your replacement to contain part of what you searched for you can use \(\) and \1, \2, \3, etc. Also, & contains your original search pattern. So %s/and/&\/or/g will replace every occurance of and with and/or.

Diff ^