Vim

the ultimate editor

Intro to Vim Tip #6 (Multiple viewports)

1

One feature of vim I don’t use enough is the ability to split the screen and view multiple files at once. I use this feature all the time when I use vimdiff, but not really any other time. I thought I’d take a moment to lay out some uses of it (thank Linux.com for the reference):

From Command Mode:

  • :sp splits the screen horizontally
  • :vsp splits the screen vertically
  • Ctrl-w Ctrl-w moves between viewports
  • Ctrl-w [right arrow] moves active viewport 1 to the right
  • Ctrl-w [left arrow] moves active viewport 1 to the left
  • Ctrl-w 3[left arrow] moves active viewport 3 to the left
  • Ctrl-w q will close the active window.

and remember that you can open a file with 😮 filename in the newly created viewport

Intro to Vim Tip #5 (Recording)

1

Search and replace is a great feature in most text editors, but what happens when you want to do more? Vim has a solution- recording macros. Suppose you have the following output from some ancient program that needs to be tweaked:


X1222 22323 2A22 3303 0000 3334esss test 123
X2222 22353 2A22 3303 0001 3334esss tacd 456
X3222 22383 2A22 3303 0010 3334esss fals 789
X4222 22393 2A22 3303 0011 3334esss true 012

It is doesn’t really matter what it is, this example is somewhat contrived. Suppose you needed to make the following changes for each line that starts with X:
* change the ID from X_222 to Y_223
* reverse the 4th and 5th fields
* copy the second character from the beginning and insert it before the last character of the line

If it were only 4 lines, you could handle this yourself, but it would be very tedious. Suppose rather than 4 lines, you had 400- it’d be much easier to automate it. The best way to take care of it would be with a macro:

[esc]qa
/^X[enter] i[delete]Y[right][right][right][delete]3
[esc]wwwdww[left]p
0[right]d[ctrl+v]y$[shift+p]
q

That right there is a MESS, but gets the job done- it’s not something you want to repeat for fear of a typo. Notice that the first characters you typed were qa: ‘q’ starts recording, and ‘a’ is the slot we’re using to store the macro. From here we record how *we* would make the changes, making sure to keep our keystrokes to a minimum. When we’re done, we stop the macro by pressing ‘q’ again.

To run our macro on the next line, press ‘@a’ to run the newly created ‘a’ macro- it should find the next line that starts with an X (notice the /^X in our first command) and run those commands to massage our text.

Remember how we were talking about 400 lines like this? Even at 2 characters each, that’s 800 characters to type which is still annoying. Here’s where the magic comes in- you can record macros of macros:

qb
@a@a@a@a@a@a@a@a@a@a
q

Now each time you run @b, you’ll run the a macro 10 times. A more efficient way to handle this would be to use

@a
398@@

the first one was done manually to record the macro, the second to play the macro, and the third to say run it 398 more times.

And there you go- a quick tour of recording macros. I’m sure there’s much more than what I’ve shown, but that’s enough to keep you busy.

Spell checking only *.txt files in Vim

2

So vim 7 has native spell checking- which is great if you remember to turn it on. Enabling it in your .vimrc is fine if all you view is written text files, but annoying when it’s running while working on code.

That’s why I created this little snippet of sunshine:

add this to ~/.vimrc:

au BufNewFile,BufRead *.txt call ConfigureTxtFile()

func! ConfigureTxtFile()
    setlocal spell spelllang=en_us
    set wrapmargin=80
    set textwidth=80
endfunction

I also use it to set my text width and wrap margins. You can have it match other types of files as well and create new functions- I plan on using this to set .py files to use space-replaced tabs. As I go forward I might have it do something a bit more fancy.

Intro to Vim Tip #4 (Pasting)

1

If you need to paste into vim from somewhere else, and your code has tabs or spaces in it, you’ll notice that vim may add extra tabs. see, vim doesn’t see it as a paste event, it sees it as “you typing really fast”- and one thing vim does will is auto-indent. The problem is when you paste, you don’t want auto-indentation because your code is already indented.

to temporarily turn off auto-indenting, try this from insert mode:

[esc]:set paste[enter]

go back to insert mode and you should be able to paste without the extra tabs.

Intro to Vim Tip #3 (Visual Mode)

0

Another well used mode is Visual Mode, which turns your cursor into a hilighter.
open a textfile with several lines of text ad move the cursor to the middle

switch from command mode to visual mode:

v

You’ll notice as you move the cursor around, you highlight different sections from the point you started to the point you left. you can press [esc] to return to command mode.

hilight a few lines of text from command mode:

[shift]v[upkey]

my adding the modifier [shift] when pressing V, you switch to ‘visual line mode’. This allows you to copy paragraphs easily.

hilight a block of text from command mode:

[ctrl]v[upkey][upkey][rightkey]

Now what good is visual mode? we” for deleting or replacing, of course! This is great when you have the following block of text:

           [option min="1"  max="10"   ][/option]
            [option min="11" max="19"   multiplier="10000" ]cp[/option]
            [option min="20" max="38"   multiplier="1000"  ]sp[/option]
            [option min="39" max="95"   multiplier="100"   ]gp[/option]
            [option min="96" max="100"  multiplier="10"    ]pp[/option]

and you’ve decided you no longer need min and max.
– move the cursor in command mode over the ‘m’ in ‘min’ on the first line
– press [ctrl]v[downkey][downkey][downkey][downkey]
– press the right arrowkey until the closing quote on “100” is covered
– press ‘d’
all your text will be gone. But suppose you didn’t want the text to be gone, you wanted to replace it with something else?
– press ‘u’ and the text will reappear
– re-highlight it and press ‘c’ (text should disapepar)
– type your replacement string (use=”false”) and press [esc]
within a moment or two, use=”false” should jump up across all five lines

Visual modes are also useful for narrowing the scope of a search and replace, but I’ll get to that when I cover search and replacing.

Intro to Vim Tip #2 (deleting)

0

Deleting in vim can be done several ways- in insert mode, the delete key and backspace key perform as you’d expect them to, but what if you want more?

delete the character to the left of the cursor:

[esc]d[left arrow]

delete the character to the right of the cursor:

[esc]d[right arrow]

deleting the current line from insert mode:

[esc]dd

deleting the current line and the one below from insert mode:

[esc]d[downkey]

deleting the current line and the one above from insert mode:

[esc]d[upkey]

deleting current character and 4 to the right:

d5[rightkey]

deleting current line and 2 below:

d2[downkey]

You’ll notice from those last two examples that deleting characters to the left and right include the current character in the count, but deleting lines above and below do not. Weird.

Intro to Vim Tip #1

0

Vim is a great tool, but using is can be a pita in the beginning- hence, we go through the basics. There are several command modes, but we’ll only discuss a few at first: Command Mode and Insert Mode.

Command mode is used to perform actions like saving, searching, etc. Insert mode is used to insert and delete text. You’ll be switching between them a lot.

Open a file from the cli:

  $ vim foo.php

change to insert mode from Command mode:

  press 'i'

change to command mode from insert mode:

  press [esc]

save from insert mode:

  press [esc]:w[enter]

save and quit from insert mode:

  press [esc]:wq[enter]

quit and do NOT save from insert mode:

  press [esc]:q![enter]

An there you have it.

Go to Top