вторник, 2 декември 2008 г.

VI help

%s/foo/bar/g go through all the file and replace foo by bar :12,20s/foo/bar/ from line 12 to 20 replace foo for bar :s/foo/bar/g in the current line replace foo for bar the g after the last / means to replace all the occurrences of foo vby bar and not only the first one.

The one I find really useful is .,+20s/foo/bar/g

Replace all occurrences in the next 20 lines from the current line only. Great when your editing code and you've realised you used the wrong variable name in that method for example.

  1. ^V for block select
  2. Select the lines you want (10j for the next 10 lines)
  3. I for insert mode
  4. Type the # or whatever you want to prefix

Use visual mode (shift-v) to highlight lines, then shell out to external programs to filter them, such as perltidy. To do that, with lines highlighted, type !perltidy (assuming you have it on your machine). This lets you filter specific lines instead of the whole file.

Not horribly exciting ones, but useful:

xp - reverse next two characters
dL - Delete to end of page, in other words, everything visible.
C - Often overlooked: chop off end of line and go into insert mode.

:ret over highlighted text will reformat using the tabbing rules set up in your
.vimrc files. Quite handy when you have legacy code and new code mixed together leaving a big mess when opened in a viewer with different settings.
And, to remove the ^M from files that came from windows:
:se ff=unix

Am I just a vim noob? After doing a search and loving the nice highlighting, is there a way to unhighlight the search term without doing a "/lkasjdfkjdfdf"? In less(1), you'd hit u but haven't found anything for vim.

The tricks I use in vi/vim are mostly the arcane flags.

:set nows

will not search past the top or bottom.

:set sw=4

will make a nice indentation shiftwidth, especially for using the indent command (>). Works great for programming, especially with autoindent (:set ai). But when programming with autoindent, you often need to unindent one shiftwidth... do that by typing control-D at the beginning of the line. You can go to the very beginning of an autoindented line with 0 control-D.

:set list
:set nolist

will turn on/off hidden characters, and show end of lines. Great for finding tabs or spaces at the end of a line.

:set nu

will turn on line numbering.

Of course, if you want actual line numbers in your file, in *nix you'd use
:%!cat -n


when pressed over a parenthesis, finds the matching parenthesis or brackets

Now, I want someone to write a lisp interpreter based in vi macros. That way we can port emacs to vi.

move up to the top line of the block to be delete

mm (sets a marker "m")

move down to the last line in the block

d`m (deletes to marker "m", and that's the grave below the tilde, not the back-quote)

Ah, add "set compatible" to your .vimrc file and you have 99% vi behavior.

Also, you can do use "ma" to mark the beginning line, "mb" to mark the ending line, and then:


(in vim only), Ctrl-A and Ctrl-X [vim.org] find the next number on the line starting at the cursor, and then increment or decrement it respectively.

Apart from being weird, these are surprisingly useful sometimes, e.g. toggling "#if 0" to "#if 1"...

Very cool. I didn't know how to mark a range like that before.

And, while we're having fun with search and replace, ^ will match the beginning of a line, so if you mark as above, and then change the command to: :'a,'bs/^/#/

you will have commented out a section of your code without having to insert a comment character independently on each line.
Reverse it with: :'a,'bs/^#//

to remove the comments.

Also, you don't have to use the / command as a separator. Anything typed after s will become the separator, so if you want to, say, change all your Windows paths to Unix paths, instead of starting with: :%s/\\/\//g

which, while undeniably cool, can be more easily written as: :%s;\\;/;g

which is a little easier to read.

Two other interesting bits:

u all by itself will undo the last command. Handy when you're testing your commands before posting them to Slashdot.

Also, Slashdot's editor will remove the newlines before any line that starts with a :
In my examples, I put each command on it's own line, but Slashdot keeps appending them to the previous line. Weird.

And if you add a c (confirm) to the end


you will get a Y/N to replace that instance or not, in case you don't want to replace every occurrence. if you search like this :'a,'b g/FINDME/ s/FROM/TO/gc

vi will ask for confirmation to replace FROM to TO only on line between a and b markers on lines with the string FINDME on it.

:.,$ g/FINDME/p will search from your current cursor position (.) to the end of the document ($) and get /regular expression/ print (i.e grep) inside of vi.


go to the 456 line (G for the last line)

These are a few of my favourite things. Vi plugin for Eclipse and Visual Studio actually makes them have a worthwhile editor, I couldn't imagine not having all the effort I invested into using vi available in some of the "editors" available today.

By Mi
On some Line just type any number let say 8:
- If press ENTER go 8 Lines Below
- if Press SPACE go 8 symbols ahead
- ? what will do with - in front :)

Няма коментари: