The one I find really useful is
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.
- ^V for block select
- Select the lines you want (10j for the next 10 lines)
- I for insert mode
- Type the # or whatever you want to prefix
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.
And, to remove the ^M from files that came from windows:
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
The tricks I use in vi/vim are mostly the arcane flags.
will not search past the top or bottom.
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.
will turn on/off hidden characters, and show end of lines. Great for finding tabs or spaces at the end of a line.
will turn on line numbering.
Of course, if you want actual line numbers in your file, in *nix you'd use
%
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.
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:
:'a,'bs/FROM/TO/g
(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:
you will have commented out a section of your code without having to insert a comment character independently on each line.
Reverse it with:
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:
which, while undeniably cool, can be more easily written as:
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
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.
456G
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 :)
Няма коментари:
Публикуване на коментар