Beta
×

Welcome to the Slashdot Beta site -- learn more here. Use the link in the footer or click here to return to the Classic version of Slashdot.

Thank you!

Before you choose to head back to the Classic look of the site, we'd appreciate it if you share your thoughts on the Beta; your feedback is what drives our ongoing development.

Beta is different and we value you taking the time to try it out. Please take a look at the changes we've made in Beta and  learn more about it. Thanks for reading, and for making the site better!

(Useful) Stupid Vim Tricks?

timothy posted more than 5 years ago | from the make-it-roll-over dept.

Software 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?"

Sorry! There are no comments related to the filter you selected.

best one ever (4, Funny)

shawn(at)fsu (447153) | more than 5 years ago | (#25666113)

:r! emacs /I partly kid I like Vi

Just using VIM (-1, Troll)

Anonymous Coward | more than 5 years ago | (#25666199)

is itself a stupid trick. Emacs sucks too. Please welcome you to 21 century - use textedit!

Re:Just using VIM (3, Funny)

jDeepbeep (913892) | more than 5 years ago | (#25666363)

is itself a stupid trick. Emacs sucks too. Please welcome you to 21 century - use textedit!

No thank you. edlin works just great for me.

Re:Just using VIM (0)

Anonymous Coward | more than 5 years ago | (#25666551)

that would be quite a trick in general since its not CLI....

Re:Just using VIM (0)

Anonymous Coward | more than 5 years ago | (#25666555)

depends what you're doing. i find textedit useful for some things but for most of my work I use vim and find it much more efficient. too bad there's a rather large learning curve.

Re:Just using VIM (0)

Anonymous Coward | more than 5 years ago | (#25666563)

Oops I meant textmate.

Re:Just using VIM (1, Informative)

Anonymous Coward | more than 5 years ago | (#25666633)

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:Just using VIM (5, Insightful)

tchuladdiass (174342) | more than 5 years ago | (#25666839)

If you are on a single box, that is fine. But when you have to admin about 500 servers, spread out across the country, and sometimes over a dial-up link, you often don't have a graphical environment available. Even on the local network, I often ssh from one box to the next, and forget to forward my X11 connections. Since vi is always available, that is what I use.

The other thing is that I appreciate having only minimal hand movement to get around a file and make changes. Much like people used to love the Word Star diamond, the same thing with vi's ctrl-f, ctrl-b, h, j, k, l, etc. And since I've been using it for about 20 years, these commands are second nature to me. Not to mention the search/replace supporting regular expressions (something a lot of gui word processors don't have).

Re:Just using VIM (5, Informative)

el momia (1400135) | more than 5 years ago | (#25667363)

:%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.

Re:Just using VIM (5, Insightful)

tolan-b (230077) | more than 5 years ago | (#25666851)

Try using most GUI editors on a remote server over ssh. Kate may be an exception with KDE's nice network abstraction I don't know (I use Gnome), but to be honest for me the main utility of vi is that I know it's going to be there in any Linux enviroment (and I suspect Unix in general).

Re:Just using VIM (1)

eosp (885380) | more than 5 years ago | (#25666991)

I learned Vim at a young age, and it has grown on me. I just prefer it now. Call it curmudgeonliness if you wish.

Re:Just using VIM (5, Funny)

eln (21727) | more than 5 years ago | (#25667113)

Young whippersnappers. When I was "at a young age", it was called vi, and it didn't have any of this fruity syntax highlighting, and if you wanted to navigate around a document you had to use h,j,k,l, not those hand-holding arrow keys.

Remember the old dig at emacs, "Eight Megs and Constantly Swapping?" Well back then, an 8 MB program actually did mean constant swapping!

I've been in this business for too damn long.

Re:Just using VIM (2, Insightful)

jaredmauch (633928) | more than 5 years ago | (#25667281)

Yes, please tell me about your years spent on Ultrix, Altos and other boxes. You modern kidz and your GUI. The main reason why I use OSX is because I can ssh *and* run Microsoft office at the same time. If you don't understand syntax like

run *kermit

load * (Oh, the magical keys on the c64)

and having to spend time at bps rates lower than 9600 when performing tasks, you clearly don't appreciate the speed increases over the years the same as some of us. I remember going from 300->1200->2400bps. Now I spent my time tracking IEEE HSSG for 100Gb/s interfaces, because I will likely have a need to bundle them.

I get really sad when I see wasteful memory and cpu bloat. Perhaps these will become scarce resources again with the mobile trend, and people will do a better job watching their consumption. Oh well.

Re:Just using VIM (1)

eln (21727) | more than 5 years ago | (#25667385)

9600 bps was especially thrilling, since it was the first time I had a modem where the modem actually displayed text faster than I could read it. With 2400 (and especially 1200 and 300) I would have to read, pause waiting for more text to come through, read, pause, etc.

When 14.4k came along, I could actually MUD without buffering a screen of commands at once while the modem caught up to me.

Re:Just using VIM (2, Funny)

careysb (566113) | more than 5 years ago | (#25667215)

When I was your age I wrote thousands of lines of C code using EDLIN. (P.s. this was on a *nix box not DOS) (P.p.s Also walked to school 5 miles, up-hill, barefoot, in the snow, both ways)

Re:Just using VIM (1)

32771 (906153) | more than 5 years ago | (#25667237)

Well it is not just editing some text anymore for me. It is more software development where you would want something like eclipse.

For some odd reason I'm still using XEmacs. You don't have to use the mouse if you don't want to. Indentation works nicer under XEmacs than with eclipses Emacs mode. It is just not the same, even though eclipses project management is nice.
Lets not start with search and replace - I don't need a silly window for that.

Fortunately there is still ecb and cedet to improve emacs code browsing a bit.

Now if somebody could tell me whether there is a ddd plugin for eclipse I might give up on emacs and switch over.

Re:Just using VIM (4, Insightful)

Sancho (17056) | more than 5 years ago | (#25667289)

I use Vim primarily because I can perform almost any task without moving my fingers from the standard typing position. I certainly feel much more efficient being able to (for example) use j/k/l/h for movement than moving my hand over to the arrow keys, or worst, to the mouse. That's one of many tasks which simply becomes natural over time (in fact, when I'm typing in a web form, I frequently find myself trying to use Vi shortcuts) and which really make things go more smoothly for me. Even when I'm on a full desktop, I prefer using Vim. I only wish the OS X port of gvim worked better.

The reason that I don't use EMACS is because of the finger gymnastics you have to perform for even the simplest of tasks. Of course, one could complain similarly about Vi--having to switch to command mode is something that gets just about every single newbie.

Re:Just using VIM (0)

Anonymous Coward | more than 5 years ago | (#25667341)

The reason I started using Emacs is simply ESS. It provides a common interface and interactive use to the most common statistical analysis programs. From there, I saw the light. I now use it for just about everything, I actually get excited about using it. I could never work at a job that didn't allow me to use Emacs. The power is simply amazing (I love that I can do EVERYTHING in it). Give it a try, a real try... you'll never go back.

Ha Ha Ha (1)

Greyfox (87712) | more than 5 years ago | (#25666889)

Back in the mid 90s I had a job auditing source code for Data General. I mostly did the C standard library, but a co-worker of mine got the vi source. The original AT&T vi source. He found a comment in there, dated sometime in the 70s, saying that the programmer didn't like the way the terminal was handled in this particular chunk of code and making a note to fix it one of these days. But my co-worker was also quite impressed with vi and started using it after that. I, being an emacs proponent (To this very day) suggested to him that he could run emacs with the emacs vi emulation and get the best of both worlds.

Amusingly he did check out the emacs vi emulation to see how well it conformed to the operation of actual vi. I don't recall what his verdict was, though. And for a few years after that I was quite sensitive to the places where vim behaves differently from vi. I still prefer a no-frills vi when I'm using vi, even though I use emacs from most heavy editing tasks.

Re:best one ever (2, Funny)

Anonymous Coward | more than 5 years ago | (#25667159)

/bin/bash: emacs: command not found

Replacement (2, Interesting)

lunchlady55 (471982) | more than 5 years ago | (#25666127)

%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.

Re:Replacement (1)

acreman (745270) | more than 5 years ago | (#25666229)

In addition to %s/FROM/TO/g you can do X,Ys/FROM/TO/g This will do the same but only between line number X and line number Y.

Re:Replacement (5, Informative)

iggya (1401047) | more than 5 years ago | (#25666479)

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:

# add 'gronk' to the end of every line
# 1 is line 1, $ is the last line
:1,$ s/$/gronk/
# put 'bing' at the start of lines 5-10
:5,10 s/^/bing/
# change foo to bar for all occurrences in the rest of
# the file from where the cursor is
:s/foo/bar/g

Re:Replacement (1)

emerald_glitter (600948) | more than 5 years ago | (#25667253)

... where X,Ys/FROM/TO/g can be something like -10,.s/FROM/TO/g (previous 10 lines & current) or .,+5s/FROM/TO/g (this line and next 5). Also, $ = last line so you can do something line .,$s/FROM/TO/g for everything from the current line to the end of the file.

Re:Replacement (1)

Sancho (17056) | more than 5 years ago | (#25667319)

You can leave off the first number to, in order to start from the current line:

:,Ys/FROM/TO/g

Or, to replace until the end of the file:

:,$s/FROM/TO/g

Re:Replacement (2, Informative)

Abcd1234 (188840) | more than 5 years ago | (#25667365)

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:Replacement (4, Informative)

Reality Master 101 (179095) | more than 5 years ago | (#25666299)

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

:'a,'bs/FROM/TO/g

Re:Replacement (5, Informative)

pluther (647209) | more than 5 years ago | (#25667095)

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.

Re:Replacement (3, Interesting)

MrKaos (858439) | more than 5 years ago | (#25667195)

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 if you add a c (confirm) to the end

:'a,'bs/FROM/TO/gc

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.

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.

Re:Replacement (1, Informative)

Anonymous Coward | more than 5 years ago | (#25666511)

Don't forget you can precede your search-replace statement with a line range, like

%10,20s/From/To/

%.,$s/F/T/

Re:Replacement (1)

pluther (647209) | more than 5 years ago | (#25666891)

CTRL-v CTRL-m will match Windows-style newline characters.

This is useful when you have a file created on a Windows machine and transferred to Unix, so: :%s/CTRL-v CTRL-m//g will strip out all the Windows newlines.

To match a Unix newline, use $ instead.

Re:Replacement (2, Informative)

mrons (2769) | more than 5 years ago | (#25667249)

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

:Sex (1)

dyingtolive (1393037) | more than 5 years ago | (#25666129)

I don't get it, whats this one do? :)

Re::Sex (5, Funny)

Anonymous Coward | more than 5 years ago | (#25666149)

You're asking on the wrong site. No one on Slashdot has ever tried it.

Re::Sex (5, Funny)

TrekkieTechie (1265532) | more than 5 years ago | (#25666209)

I'll tell you when you're older.

Re::Sex (1)

tsalmark (1265778) | more than 5 years ago | (#25666595)

Try it, You'll like it.

Re::Sex (4, Informative)

Noksagt (69097) | more than 5 years ago | (#25667061)

:help sex

:Sexplore[!] [dir]... Split&Explore directory of current file *:Sexplore*

e.g. it give you a file exploring pane above the buffer you are currently using.

Filter Lines (4, Interesting)

saberworks (267163) | more than 5 years ago | (#25666243)

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.

Re:Filter Lines (1)

TheRaven64 (641858) | more than 5 years ago | (#25666353)

I've not done this with perltidy, but a visual mode selection followed by !sort is really useful for sorting lines. xxd instead of sort is great if you find yourself looking at some binary data too - you can use vim as your hex editor.

Re:Filter Lines (1)

oever (233119) | more than 5 years ago | (#25667001)

!cat
!uniq
!sort
!sort -n
!ls
!bash

It all works. This is brilliant! I've using vim for years and did not know this.

Re:Filter Lines (2, Interesting)

Jameson Burt (33679) | more than 5 years ago | (#25667391)

In vim, I often enter
:set paste
then highlight text from a browser
and paste that text (middle mouse button)
into my vim session.
The "set paste" prevents the lines from indenting further and further to the right.

After pasting, many lines are too long.
In particular, a whole page gets pasted as one line.
So, I enter
:1,$!fmt
or like the author (shift-v or ctrl-v) then

      !fmt

Couple off-hand (4, Interesting)

Reality Master 101 (179095) | more than 5 years ago | (#25666245)

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.

Vim tips (5, Informative)

icsEater (1093717) | more than 5 years ago | (#25666271)

Why bother asking slashdot when all the best Vim tips have been collected and compiled? http://vim.wikia.com/wiki/Best_Vim_Tips [wikia.com]

very useful (especially for noobs) (5, Funny)

gEvil (beta) (945888) | more than 5 years ago | (#25666303)

:q

Re:very useful (especially for noobs) (4, Funny)

Anonymous Coward | more than 5 years ago | (#25666527)

surely:

:q!
nano

Re:very useful (especially for noobs) (1)

gEvil (beta) (945888) | more than 5 years ago | (#25666837)

You're assuming they managed to get into Insert mode first. : p (that's a smiley, not a command)

Re:very useful (especially for noobs) (0)

Anonymous Coward | more than 5 years ago | (#25666645)

very useful (especially for noobs) :q

I tried it a few times but couldn't get it to work. I was getting frustrated and yelled it, and whaddya know, it worked... :q!

= and * (3, Informative)

Lalakis (308990) | more than 5 years ago | (#25666323)

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 (4, Informative)

DigitalCrackPipe (626884) | more than 5 years ago | (#25666337)

: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

Re:retab (1)

jakel2k (736582) | more than 5 years ago | (#25667369)

==
indents the line. Add a number in front to indent the next N lines. Works well in C/C++... sucks in HTML files.

Whitespace + Searching (1)

kevin_conaway (585204) | more than 5 years ago | (#25666349)

" Add >> to tab spaces, . to trailing whitespace

set list listchars=tab:,trail:.

" Use incremental search

set incsearch

" Highlight search matches

set hlsearch

When dealing with XML (0)

Anonymous Coward | more than 5 years ago | (#25666355)

Quick way to verify XML in vim.

cat >> ~/.vimrc <<EOF
noremap <F2> :set syntax=xml<CR>:%!xmllint --format - <CR>
EOF

Then, when in vim, hit F2 to syntax highlight and run through the xmllint formatter.

my most frequent vi trick (1)

wdr1 (31310) | more than 5 years ago | (#25666379)

:q!emacs !^

Need a way to un-highlight (4, Informative)

bugnuts (94678) | more than 5 years ago | (#25666385)

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.

: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.

Re:Need a way to un-highlight (1)

Lalakis (308990) | more than 5 years ago | (#25666419)

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"?

:nohl

Re:Need a way to un-highlight (2, Informative)

Mad Merlin (837387) | more than 5 years ago | (#25666499)

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.

:noh

Re:Need a way to un-highlight (1)

Cristofori42 (1001206) | more than 5 years ago | (#25666703)

while :noh is the "correct" way to do it, I find it a lot faster personally to type "/asfe". I suppose the best solution would be to just map it to an F key or something.

Re:Need a way to un-highlight (2, Informative)

Hatta (162192) | more than 5 years ago | (#25666673)

If you're pasting something, and don't want Vim's autoindent to screw up the formatting use: :set paste

Re:Need a way to un-highlight (1)

Mad Merlin (837387) | more than 5 years ago | (#25667311)

If you're pasting something, and don't want Vim's autoindent to screw up the formatting use: :set paste

Much easier is to use :a! (or :i!), paste your text and then end the paste with a line containing just .

Re:Need a way to un-highlight (1)

hedronist (233240) | more than 5 years ago | (#25667067)

I have the following in my .vimrc file:

map <F4> :let &hlsearch=!&hlsearch<CR>/<BS>

This allows <F4> to toggle highlighting on and off. The <CR> ends the :let command, the / starts a new search (and therefore clears the command line), and the <BS> makes the / go away.

A little circumlocutious, but it works for me.

The following are also useful for search highlighting:

set hls
highlight Search term=standout ctermfg=4 ctermbg=7
highlight MatchParen ctermfg=1 ctermbg=8

Re:Need a way to un-highlight (1)

theokayplus (1331743) | more than 5 years ago | (#25667251)

To unhighlight a search term, use :nohl. I have it bound to a key in my .vimrc:
map <F1> :nohl <CR>

% - jump between braces, ifdefs, etc (2, Informative)

tippe (1136385) | more than 5 years ago | (#25666427)

Also:

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)

So many (1)

dedazo (737510) | more than 5 years ago | (#25666429)

gF: Open the file under the cursor, for #include statements and even classes for languages like C# or Python (if they're in the same directory). Very nifty.

%: Match the brace under the cursor.

Some of my favs are the text object commands in visual mode, like placing the cursor inside a string literal and doing vi" to select the inner portion of the string and then c to start changing it without having to re-type the quotes. viB selects the current inner brace block, vaB the outer block (including braces), good for refactoring and so on.

* Start an auto search for the word under the cursor.

~: Swap the case of the char under the cursor.

So many...

split (1)

gsn (989808) | more than 5 years ago | (#25666433)

:sp to split the screen
  to toggle between
made my life a lot easier

Re:split (1)

gsn (989808) | more than 5 years ago | (#25666453)

er ctrl-w-w
(forgot about tags)

Props to Douglas Adams (0)

Anonymous Coward | more than 5 years ago | (#25666467)

:help 42

WTF?? (5, Funny)

monkeySauce (562927) | more than 5 years ago | (#25666473)

I typed :Sex and it opened up a HUGE list of folders choc full of porn!

Re:WTF?? (4, Funny)

snspdaarf (1314399) | more than 5 years ago | (#25667201)

Congratulations! You found the Easter egg!

:q (0)

Anonymous Coward | more than 5 years ago | (#25666491)

:q

every first time user needs to know how to get out of it when nothing he tries works. reminds me of that bash.org post.

Why asking... (1)

GerardAtJob (1245980) | more than 5 years ago | (#25666521)

RTFM : http://www.vim.org/htmldoc/help.html [vim.org] Damn... :q!kill -9 !^ ty.. now that the frustration is gone I'm feeling so much better... pls post interesting news :)

Viewing hexdumps of binary files in vim. (2, Interesting)

CheeseMonger (562322) | more than 5 years ago | (#25666545)

First edit the file in binary mode
vim -b datafile
Now convert the file to a hex dump with xxd
:%!xxd
Well, I thought it was cool.

let counter= (0)

Anonymous Coward | more than 5 years ago | (#25666577)

counters in vi...Perfect for DNS edits.

let counter=108|149,%g/con11\-70/let counter=counter+1|s/109/\=counter.''

Vim 7's vimgrep feature (0)

Anonymous Coward | more than 5 years ago | (#25666621)

I really like the vimgrep feature in Vim 7. Once you execute it, use :cope to open the results in a split window.

Deleting lines (0)

Anonymous Coward | more than 5 years ago | (#25666643)

One of my favorite Vi commands... dunno where I even found it: :.,$d

Delete everything from this line down... Comes in handy when chopping a file down to size.

keyword completion (0)

Anonymous Coward | more than 5 years ago | (#25666667)

While in insert mode and typing a very long word/variable that you typed a few lines before, press: CTL+p

delete a block of lines larger than the screen (3, Informative)

prgrmr (568806) | more than 5 years ago | (#25666723)

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)

Re:delete a block of lines larger than the screen (1)

oever (233119) | more than 5 years ago | (#25667077)

Or go to visual mode with 'v' or 'V', scroll down and press 'd'.

Most time-saving features (1, Interesting)

Anonymous Coward | more than 5 years ago | (#25666763)

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.

Stupid vi tricks? (2, Funny)

argent (18001) | more than 5 years ago | (#25666771)

VIM is a "stupid vi trick".

Every time I update OS X no I have to dike it out and put the REAL "vi" back.

MOD PARENT UP (-1, Flamebait)

Anonymous Coward | more than 5 years ago | (#25667169)

vim sucks.

Re:Stupid vi tricks? (4, Insightful)

mario_grgic (515333) | more than 5 years ago | (#25667213)

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

Why would you use plain vi, when vim is so much better?

best trick (1)

WillAffleckUW (858324) | more than 5 years ago | (#25666779)

use vi raw instead.

Don't yank my line!

Use '=' and 'gq' together... (1)

wanerious (712877) | more than 5 years ago | (#25666813)

A good partner to the indenting keystroke "=" is "gq", to automatically format highlighted text that is messy from copy/pastes. I use the pair all the time to save headaches.

Re:Use '=' and 'gq' together... (1)

Qzukk (229616) | more than 5 years ago | (#25667345)

:set paste! to avoid the headache in the first place. :set pt=<f1> to avoid the headache of typing :set paste! every time you want to switch in and out of paste mode

Buy the book (1)

pauljlucas (529435) | more than 5 years ago | (#25666843)

There's lots of good stuff in here [amazon.com] .

Re:Buy the book (1)

game kid (805301) | more than 5 years ago | (#25666987)

Anyone else misread the URL as "...Steve Ovaltine..."?

I need more sleep :-(

1,.d (1)

TheCarp (96830) | more than 5 years ago | (#25666945)

Thats right, first post, just delete all the lines before this one.

The ability to do any operation on a subset of lines, itself, is so useful. I was working with a n00b showing him some tips to polish up a script that he wrote. Of course he used the same string like 20 different times, and it contained a version number.

So I showed him how to break it out into a variable at the top then moved to the next line and :.,$s/foobar35/$dirname/g

Or when dealing with a log file with lots of useless information using :g/pattern/d to remove all of the lines that don't have any use can really make the problem just pop right out at you

-Steve

some useful things (0)

Anonymous Coward | more than 5 years ago | (#25666995)

Ctrl-v ...for block select.

Ctrl-n ...(or Ctrl-p) to autocomplete words. /string\c ...to search case insensitive (because of \c).

U | u ...with text selected to (de)capitalize text.

u ...to undo last changes

Ctrl-R ...to redo last changes

Also, after selecting text (with V): copy (yy), paste (p), delete (d).

Tabs! (1)

solipsist0x01 (887281) | more than 5 years ago | (#25667013)

auto-completion: Ctrl + n

especially with this in my .bashrc...

alias gvim="gvim -p --remote-tab-silent"

then formatting the tab title with this in .vimrc...

" Customize the information in the tab lables
function! GuiTabLabel()
    " add the tab number
    let label = '['.tabpagenr()

    " modified since the last save?
    let buflist = tabpagebuflist(v:lnum)
    for bufnr in buflist
        if getbufvar(bufnr, '&modified')
            let label .= '*'
            break
        endif
    endfor

    " count number of open windows in the tab
    let wincount = tabpagewinnr(v:lnum, '$')
    if wincount > 1
        let label .= ', '.wincount
    endif
    let label .= '] '

    " add the file name without path information
    let n = bufname(buflist[tabpagewinnr(v:lnum) - 1])
    let label .= fnamemodify(n, ':t')

    return label
endfunction

set guitablabel=%{GuiTabLabel()}

One tip: (1)

dealmaster00 (904299) | more than 5 years ago | (#25667097)

Shift + ZZ will save and quit.

Some of my favorites (1)

Vornzog (409419) | more than 5 years ago | (#25667111)

In your .vimrc:

Reformat the current paragraph (great for editing text):
nnoremap Q gqap
vnoremap Q gq

Incremental searching:
set incsearch

Insert a comment character on multiple rows - use this to do a visual select and comment out a code block:
(ctrl-v) -> visually select a column
(shift-i) -> go to insert mode
(your favorite character here - I like #)
esc, esc

Find a good set of fold rules and syntax highlighting for your favorite programming language. This has become an essential part of the way I work.

Those are some of my favorites. I discover something new about once a week, despite having used vim for years now.

Most powerful of them all... (1)

javab0y (708376) | more than 5 years ago | (#25667121)

:!sudo rm -rf /*

DOH! No really - don't do that.

gg=G (1)

MonoSynth (323007) | more than 5 years ago | (#25667151)

  • gg=G to reindent a whole file
  • Ctrl-L to redraw the screen (to fix broken syntax highlighting)
  • Fortunately Esc doesn't do strange things in Firefox

Stack Overflow (4, Insightful)

JPLemme (106723) | more than 5 years ago | (#25667185)

When did Slashdot become Stack Overflow?

Vimperator (1, Informative)

Anonymous Coward | more than 5 years ago | (#25667245)

Not directly related to VIM itself but, Firefox plugin called Vimperator...gives web browsing vimlike commands

Perhaps two of the weirdest: Ctrl-A and Ctrl-X (2, Informative)

sdt (7606) | more than 5 years ago | (#25667295)

(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"...

Often ignored: ex mode (1)

kyrmit09 (1401821) | more than 5 years ago | (#25667301)

Hi, the ex-mode isn't as straight forward as the commands for vi but comes in handy at times. A rough and tumble way to view methods in a perl module ":g/sub/p", or public java methods in a java class ":g/^public/p". Also helpful to filter all the stack dumps from the jboss (or other java related ) logfile to get some non generic error messages ":v/\tat/p".

dos2unix equivalent (1)

CyberSnyder (8122) | more than 5 years ago | (#25667305)

%s/(then press ctrl-v crtl-m)//

I never knew how to get the ^M on the command line and found it a week or two ago after doing unix since '93.

Especially useful for PERL (2, Informative)

ickyb0d (975453) | more than 5 years ago | (#25667317)

To create a comment block:

move cursor to the start of the comment block
ma - creates a mark called "a"
scroll down to the end of the comment block

:'a,.s/^/#/ - to create comment block
:'a,.s/^#// - to remove 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.

beep beep (1)

maharg (182366) | more than 5 years ago | (#25667357)

to make vi[m] beep, just press Esc Esc ;o) Also :e! restores to the previously saved version
Load More Comments
Slashdot Login

Need an Account?

Forgot your password?