(Useful) Stupid Vim Tricks? 702
haroldag writes "I thoroughly enjoyed the recent post about Unix tricks, so I ask Slashdot vim users, what's out there? :Sex, :b#, marks, ctags. Any tricks worth sharing?"
"Gravitation cannot be held responsible for people falling in love." -- Albert Einstein
Vim tips (Score:5, Informative)
Re:Replacement (Score:5, Informative)
Also, you can do use "ma" to mark the beginning line, "mb" to mark the ending line, and then:
:'a,'bs/FROM/TO/g
= and * (Score:3, Informative)
There are far too many "essential" commands in vim, but if I had to pick the two that make the most difference, I would pick * and =. * searches for the word under the cursor and = indents the selected text (most useful for programming).
retab (Score:5, Informative)
And, to remove the ^M from files that came from windows:
Need a way to un-highlight (Score:5, Informative)
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 <esc>u but haven't found anything for vim.
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
:%!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.
% - jump between braces, ifdefs, etc (Score:2, Informative)
v, V and ctrl-V (various visual selection modes for copy-paste)
zf, zo, zc: fold creation, open, close (hides sections of your code)
~: (toggle case)
u, ctrl-r: undo, redo
"vimdiff" or "vim -d": (visual diff of two files)
Re:Replacement (Score:5, Informative)
One of vi's best features is the '.' command to repeat what you last did. You can do 'dd' to delete a line, then press '.' (dot) to do it again. Or '100.' to do it 100 times. Typing in numbers before a command repeats the command. Typing in '100ihello[esc]' will insert 'hello' 100 times. Then typing dot will give you 100 more.
On a modern vi you can press up-arrow after pressing colon to get your previous colon command back for editing.
Some examples of changing things on various lines:
Re:Need a way to un-highlight (Score:3, Informative)
Re:Replacement (Score:1, Informative)
Don't forget you can precede your search-replace statement with a line range, like
%10,20s/From/To/
%.,$s/F/T/
Re:Just using VIM (Score:1, Informative)
I know the emacs vs. vi holy war is a funny old meme, but why not just use a modern-style editor? We have these powerful graphical desktops with very standard interfaces. I'm sure the old classics give you a lot of power (Actually, I don't know that much about vim, but I understand you can run Eliza, NetHack, or a Lisp interpreter in emacs), but who wants to learn a bunch of obscure commands and meta-keystrokes? If you just need to edit some text, there are simpler solutions. Me, I use Kate.
Re:Need a way to un-highlight (Score:3, Informative)
If you're pasting something, and don't want Vim's autoindent to screw up the formatting use: :set paste
delete a block of lines larger than the screen (Score:4, Informative)
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)
Re::Sex (Score:5, Informative)
:help sex
e.g. it give you a file exploring pane above the buffer you are currently using.
Re:Replacement (Score:5, Informative)
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. :'a,'bs/^#//
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: :%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.
Vimperator (Score:1, Informative)
Not directly related to VIM itself but, Firefox plugin called Vimperator...gives web browsing vimlike commands
Re:Replacement (Score:2, Informative)
Also g/foo/s/bar/baz/g
For all the lines that match foo, replace bar with baz
With this form, the equivalent of %s/bar/baz/g is g/bar/s//baz/g
Another useful :g command is g/foo/d to delete all lines with foo
Perhaps two of the weirdest: Ctrl-A and Ctrl-X (Score:3, Informative)
(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"...
Especially useful for PERL (Score:2, Informative)
move cursor to the start of the comment block
ma - creates a mark called "a"
scroll down to the end of the comment block
basically puts # at the start of every line from mark a to your current cursor position. You can get creative and use this method for tons of things, indenting, substituting words within a given scope, comment blocks, etc.
Re:Just using VIM (Score:5, Informative)
Re:Replacement (Score:3, Informative)
I much rather use Visual mode for this, as I hate having to remember line numbers. Just hit 'V', highlight the region, and then type ':s/FROM/TO/g'. Vim does the rest.
Re:Vim tips (Score:5, Informative)
Re:best one ever (Score:3, Informative)
Re:Filter Lines (Score:2, Informative)
These are useful with prefixes: :r!pwd (read `pwd` into to document) :%!sort (sort all lines) :'$lt;'>!sort | uniq (sort last visual selection, notice the pipe)
Another useful thing you can do is to bind a filter to a script, in your .vimrc, so you can yy pp a line, and get an instant commented debug line.
map :.! ~/scripts/javascript-debug.pl==
#!/usr/bin/perl
while() {
($line = $_ ) =~ s/^\s+|\s+$//g;
($quote = $line) =~ s/'/\\'/g;
print "console.log('DEBUG: $quote ', $line);\n";
}
Note: My normal script has half a dozen regexps in the middle, for stripping out semicolons, function declarations, return statements, such that what gets printed is almost always a valid code statememnt. However slashdot can't tell my regexps apart from line line-noise and kept complaining about too many junk characters.
However its possible to write similar scripts for various languages, and for other simple tasks such as wrapping a for statement around an array variable.
Re:delete a block of lines larger than the screen (Score:2, Informative)
Use Ctrl-v and you can select a column of chars. You can copy, delete and paste them.
I have used this technique, along with vertical alignment of code, to quickly copy/paste a huge spreadsheet of expected results, into very exhaustive unit test code.
shelling out (Score:2, Informative)
:sh works well when you don't know root, but have sudo privs.
Re:Vim tips (Score:3, Informative)
Re:Here's a few good ones (Score:3, Informative)
Actually I fix this one within .bashrc, and create a wrapper function around vim, that detects if the file is editable or not, and asks if you want to prefix an sudo.
argc () {
count=0;
for arg in "$@"; do
if [[ ! "$arg" =~ '-' ]]; then count=$(($count+1)); fi;
done;
echo $count;
}
vi () { if [[ `argc "$@"` > 1 ]]; then /usr/bin/vim $@; /usr/bin/vim; /usr/bin/vim $@; /usr/bin/vim $*; /usr/bin/vim $*;
elif [ $1 = '' ]; then
elif [ ! -f $1 ] || [ -w $1 ]; then
else
echo -n "File is readonly. Edit as root? (Y/n): "
read -n 1 yn; echo;
if [ "$yn" = 'n' ] || [ "$yn" = 'N' ];
then
else sudo
fi
fi
}
Reference card (Score:3, Informative)
All my favorites, right here: http://limestone.truman.edu/~dbindner/mirror/#Vi-Ref [truman.edu]
Re:Replacement (Score:3, Informative)
# change foo to bar for all occurrences in the rest of :s/foo/bar/g
# the file from where the cursor is
No, that only works on the current line. perhaps :.,$s/foo/bar/g
regular expression groups (Score:2, Informative)
When you want to move the words around in a line you can do something like this.
Re:Esc ends commands, not starts them (Score:3, Informative)
If you use "your" to mean "you're", you're doing it (using the English language) incorrectly.
Re:Just using VIM (Score:2, Informative)
Comments the lines in quite many languages
For C I use this
:'<,'>s+^+//+
Re:Replacement (Score:2, Informative)
Some examples of changing things on various lines:
In vim (unsure if its new or in the original vi) a % means do this on all lines.
eg: :%s/^/foo/
places "foo" at the start of every line.
Re:Ha Ha Ha (Score:1, Informative)
You can get the traditional vi here:
http://ex-vi.sourceforge.net/
Clever deleting (Score:2, Informative)
Getting copy/paste to work (Score:3, Informative)
Vim works wonderful together with X. Besides the graphical version (gvim), it's also possible to start vim in a terminal like xterm, konsole or gnome-terminal. With the right options, it's still aware of the X clipboard, the mouse, et cetera. However it's quite a pain in the ass to find out what's wrong when it doesn't work. Here's a guide for using and fixing it.
vim has one default clipboard, but has an extra one under any a-z character. The two X clipboards are available too, but under a special character. To paste the X clipboards, paste either the + or * clipboard:
This is the X clipboard for which you just select something with the mouse: [Escape]"+p
The star buffer contains the X clipboard for which you selected with the mouse, then right-clicked and selected 'copy', or pressed a shortcut like CTRL-C or similar: [Escape]"*p
If this doesn't work, check the following things:
Vim is started in compatibility mode. Solution: don't use 'vi' to start, use 'vim'. On RedHat and CentOS, it's not enough to start 'vim', use 'vimx'. If this program is not present, be sure to install the package 'vim-X11'.
$ ssh -v -X remotemachine
debug1: Requesting X11 forwarding with authentication spoofing.
debug1: Remote: No xauth program; cannot forward with spoofing.
Re:Just using VIM (Score:2, Informative)
try MacVIM which is an excellent port
Re:Just using VIM (Score:4, Informative)
It's quite easy
Re:Couple off-hand (Score:2, Informative)
I/A - insert at start /end of line
P/p - paste before / after cursor
"*y / "*p - yank & paste to / from the windows clipboard
vat/vit select around/inside a text block like html tags
vi" select inside speech marks
ciB correct inside curly brackets pair
the list goes on, the point is to treat each key as an atom of a command, and learn how they can be combined.
!python % - run current file through python
ggVG= - reindent current buffer
^wv/s - split window
^6 - switch to last touched buffer
vim cheat sheet (Score:2, Informative)
Split view, edit, next, ff ftw (Score:1, Informative)
Ctrl-w + s for split horizontal, Ctrl-W + v for split vertical. Ctrl-w then moves between the panes, and in each pane you can split it again to your hearts content. Also :edit to edit a new file, if you open multiple files like vim *.txt then :next will move to the next file.
Very useful :![cmd] to execute a command back on the shell, like :!tail /var/some/log to check errors while fixing them in your code.
Also nice to see a little firefox kickback to VIM - in firefox hit / to search through the document, shortcute for ctrl-f/splat-f
How about recording? (Score:2, Informative)
In vim/gvim, I think the most useful thing I have ever used is the recording capability. It works great for editing HTML. If you have 10 lines that look very similar (such as a list of navigation links), start recording, edit the first line making sure that you end your statement with 'j' so that it moves to the next line, then finish recording and you can execute that recording for the next 9 lines.
To start recording, type 'q' and then a number such as '1'. This will record your next keystrokes into memory bank 1. When you're done typing, just hit 'q' to finish recording.
So, if you had
<h1>Foo</h1>
<h1>Foo</h1>
<h1>Foo</h1>
<h1>Foo</h1>
and you wanted
<h1>Foo Bar</h1>
<h1>Foo Bar</h1>
<h1>Foo Bar</h1>
<h1>Foo Bar</h1>
you would hit 'q1' to begin recording in bank 1, '^' to start at the beginning, 'l' (as in ell) 6 times to move six characters to the right, 'a' to insert after the last 'o' of "Foo", ' Bar' to insert the string, 'esc' 'j' to finish and move down one line, 'q' to finish recording, then '3@1' to execute the operation in bank 1 three times.
I can't count the number of times I have used this functionality! Makes coding immensely easier.