Skip to content

Instantly share code, notes, and snippets.

@rpattabi
Created April 21, 2012 03:05
Show Gist options
  • Save rpattabi/2433462 to your computer and use it in GitHub Desktop.
Save rpattabi/2433462 to your computer and use it in GitHub Desktop.
"Learning vi and vim editors" - study notes

Study Notes for "Learning vi and vim editors" (7th edition)
I want to take notes for only the new things I was not aware of

Vim Practice:
I am editing this using gVim through Firefox's ItsAllText! extension.


the vi text editor

Opening a file in read only mode » vim -R [filename]

Still allows to edit. But warns while saving.
Saving the edit is still possible with ! override.

Opening last saved version of the file » :e!

There is no need to q! and reopen the file.
Useful getting out of the mess that cannot be undone without too many undos.

Save as » :w newfile

Vim stores temporary files in the same directory as the file being edited.

:preserve » Saves the current buffer as though the system is about to crash

Not quite clear what it does.
But something to remember when the house burns down (??)

Vim documentation says the below:

	:pre[serve]		
            Write all text for all buffers into swap file.  The
		original file is no longer needed for recovery.
		This sets a flag in the current buffer.  When the '&'
		flag is present in 'cpoptions' the swap file will not
		be deleted for this buffer when Vim exits and the
		buffer is still loaded |cpo-&|.
		{Vi: might also exit}

        A Vim swap file can be recognized by the first six characters: "b0VIM ".
        After that comes the version number, e.g., "3.0".

simple editing

vim has compatible and nocompatible setting for vi compatibility.

Default is compatible.
There are little differences between the two.
For example, you can't get past a line with h once you are at the beginning of the line in compatible mode. But noncompatible mode allows you to go to previous line with h.

Next sentence » ) Previous sentence >> (

Entering Special characters (copyright, registered, etc) » In insert mode ctrl-k followed by code from :digraph

Word wrap » :set wrapmargin=10 or :set wm=10

sets right margin to 10 chars
automatically inserts newline char once right margin is reached
only meant for the new lines

Move by word (without considering symbols and punctuation) » W

Note that small w moves by words counting symbols and punctuation as words.
Of course B and b does similar thing backwards

Line number display » :set number or :set nu

Enlightenment, p is for put

I always thought it is paste. Why not?

Vi style editing goes naturally with implementing proof reading suggestions. (figure 2-3 page 18)

Excellent example to demonstrate the vi way for the uninitiated

Most commands capitalized work on the line

usually considers cursor position to the end of the line Doubling makes the command work on the whole line. e.g. cc, yy, or dd.

Difference between dw and de

dw deletes space after the word while de does not.

c can be combined with any movement

cw, c2b, c0, c$
captial C is equivalent to c$

Multiplier for c, y, and d can be in any order

2dw is same as d2w

Replace r can take muliplier

Replaces those number of characters with the character inserted
Useful to mark words such as 10July2012 to XXXXXXXXXX. Done with 10rX.
|| can be easily replaced to && with 2r&.
Capital R keeps you in replace mode for the rest of the line till you ESC

Toggle case of a letter with ~

finally found it!
moves to next char, after toggling, till the end of line is hit
Can be used with multiplier

Substitute s

Substituting replaces the char at the cursor. But unlike r, s maintains insert mode
Good for substituting one char with several characters.
Can be combined with multiplier

Capital S substitutes whole line

Capital C allows to change the rest of the line from the cursor

Vim supports infinite undo unlike vi

"2p will paste 2nd last deleted line

so "1p is equivalent to p? Not exactly. p can also paste last deleted word. "1p is for last deleted line.

Interesting explanation for xp. x standing for trans- p standing for -posing. So xp for transposing.

Yanking and Deleting use same buffer.

. repeats last editing command, of course.

Undoing all the editing on a line U

Once you leave a line, you can't do this undo by moving back. But I see this is not an issue with vim. Not sure why the book didn't mention it.
u will undo U. Similarly, U can undo u.
You can move to the last position of your edit by u which will take you to that line. U will undo this u. So you are already there.
ctrl-R is for redo undone action in vim.

Inserting text at the beginning of a line » I

Similar to A which inserts at the end of a line
I takes you to the first visible character. 0 takes to the first character even if it has space.

Numeric prefixes (I call it as Multipliers) can be used for i, A, I, S, and a also.

50i*[ESC] inserts 50 *s.
25a*-[ESC] appends 25 *-s.
Useful for header decoration.
S could be combined with multiplier to replace several lines.
s with multiplier is same as R.

Identify those standard phrases...

xp toggles two chars. Good for those teh.
ea appends after a word.
wi inserts after a word and space.

J joins current line with the next line. Takes multiplier.

+ and -

+ moves to the first char of next line
- does the same for previous line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment