(Useful) Stupid Vim Tricks?
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?"
← Back to Stories (view on slashdot.org)
%s/FROM/TO/g
replaces every instance of FROM to TO in the document.
% = every line. Drop that to just affect the current line
g = every instance within the line. Drop that to change only the first occurrence in the line.
also - use CTRL-v CTRL-m to get a newline - will look like ^M but match newlines.
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.
Sometimes it's best to just let stupid people be stupid.
First edit the file in binary mode
:%!xxd
vim -b datafile
Now convert the file to a hex dump with xxd
Well, I thought it was cool.
CheeseMonger
1. Tags. No learning curve. Generate tags with !ctags -R *.c *.h (if you are looking at a C code base). Ctrl-] on any function name, structure name/field, etc. to jump to the declaration point. Ctrl-T to come back. Tag stack is maintained. Then, :ta to jump to the tag. Auto completion for the tag name makes it a breeze. Ctrl-], Ctrl-T, and :ta are enough to save 50% of your time.
2. Ctrl-P and Ctrl-N for very smart auto-completion can save another 30-50% of your time.
3. Record a sequence of key strokes and replay them. Hit 'q', , 'q', , 'q' . Then, hit @f to repeat those actions. @@ to repeat the previous @ command. So one can do: @f, then, @@, @@, wherever they want to apply the changes.
4. Hitting 'gf' on a header file will take you to the header file (not to your girlfriend). Hit Ctrl-O to come back.
5. Use j,k,l,; to move up, down, left, and right. Learn touch typing.
main utility of vi is that I know it's going to be there in any Linux enviroment (and I suspect Unix in general).
vi is part of the Single UNIX Specification, so anything passing itself off as UNIX must include vi. Even without the spec, it's much, much more universal than emacs, and more powerful than pico/nano.
"I think an etch-a-sketch with an ethernet port would beat IE7 in web standards compliance."
I've used vim for years... to the point that it is forever ingrained in the way I think and code. I can't even think of the last thing I edited *not* in vim (besides this post :). I consider myself to be *kinda okay* at using it...
Here are three of my *cannot live without* vim features...
1. Highlight text with visual mode and 'gq' to format it according to the currently set formatting rules. I know full-fledged IDEs have this feature, but I can't think of anything outside of vim or emacs that can do this from a thousand miles away in a terminal.
2. "smarttab" mode and the 'retab' command. I've worked in lots of places with very disparate IDEs and environments and everyone seems to produce code with slightly different tabstops and tab consistency. 'smarttab' and 'expandtab' tell vim to output spaces instead of tabs for consistency in viewing across editors, but to treat consecutive spaces as virtual tabs for the purposes of deleting or selecting. 'retab' changes tabs across the entire file to match the currently set convention. *Very Useful* for editing python :)
3. This is the biggest one... macro recording and programmatic repeat. 'q' starts recording a keystroke macro, which thanks to vim can be something as complicated as... "go to start of line, replace text up to first ( with blah, go to second ',' and capitalize the first letter of that word" etc... Then, you can either use the :global command to search a file for lines matching a pattern and execute that macro on each line, or even easier, use V (visual line select mode) to select groups of lines, then execute the macro on all of those, or a subset of those with global. I always do this via the :normal command, which allows you to execute a keystroke based command through command-line mode. So, 'qa^f(cbfoobarf)a const' would replace a function call name with 'foobar' and add a const to the declaration. Select a group of lines with V, then type :normal @a to call the macro on every line. I think there might be a better way to call macros from command-line mode, but that's the only one that I know.
I have actually whined and cried and begged to put vim on everything larger than a toaster where I work so that I don't go crazy. Vim has saved me countless hours of programming time over the years, and I highly recommend trying to learn some of its deeper features.
Plus, all the other editors I use have this weird habit of putting ':wq's and 'dd's in my text...
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 to visually distinguishable tabs. I still want a bit "whiter" whitespace though, something that settles a little more easily into the background. I've lately settled on the following, in combination with DejaVu Sans Mono. Unicode support mileage with other fonts varies.
Sigh. I'm sure I'm not the only one here... I distinctly remember purchasing my first 9600bps modem. (A real Hayes, no less! I sent them a large manilla SASE and they shipped me the AT command manual for no charge.) I spent a few months mowing every lawn I could to raise the funds for it. Exactly a week after I got it installed and found a couple local BBSes I could connect to at 9600, Hayes shipped the very first 14400bps modem.
Apart from the nature and amount of labor involved in raising funds, that's been a pattern for so many equipment purchases since. That was the very first time I bought something so close to the release of the new shiny, though :-)
.sig: file not found
By the way, you can use any character instead of the '/'. Handy for replacing path names in a file:
Because..drum roll.. Vim has a syntax file for your favorite language..
You can customize if for your coding style... especially for things that you make wiked mistakes on..
for example:
if (x=y){do some code;}
any c programmer will see the mistake pretty quick.
but once it's in thousands of lines of code,, its a bear to find.
with clown colors you set the color of the = to bright purple, and the parens to yellow. set == to orange.. so that your eye catches a painful clash when you type something stupid..
for the common typos that I make.. I set it to INVERSE... so retrun x; get fixed before get to the next line.
some of the common variable names I use get colorized so that I can recognize the specific variables...
So a common for loop that you code without blinking has a "color signature" that lets you know it's your normal.
fantastic crutch..
Storm