tags:

views:

1696

answers:

31

What are the essential vim commands? What does a new-user need to know to keep themselves from getting into trouble? One command per comment, please.

+2  A: 

First of all you need to know how to close vi: ctrl-c : q!

Rest can be found from vimtutor. Launch vimtutor by typing vimtutor at your command line

Control-C, then `:q!`? Why Control-C? Why not plain ESC?
ΤΖΩΤΖΙΟΥ
A: 

One of my favourite commands is %G which takes to directly to the end of a file. Especially useful in log-files.

Actually you can drop the '%' and just type 'G'. The '%' is meaningless.
Nathan Fellman
% is not meaningless, since it takes you to a matching bracket. If you're not inside brackets (square, curly or parentheses), then it's a no-op.
ΤΖΩΤΖΙΟΥ
+15  A: 

I have been keeping a Vi Reference Card with me for years. It is very useful.

hongster
I printed it and pinned it on my board. It's so great!! Thanks
Luc M
So, what's the single vi command to fetch the vi reference card pdf?-)
ΤΖΩΤΖΙΟΥ
A: 

How to switch between modes (i to enter insert mode (one of many ways), esc to exit insert mode, colon for command mode) and how to save and exit. (:wq)

D.J. Capelis
A: 

I use vi very lightly, and I only use the following commands:
a - switch to insert mode
esc - return to command mode
:wq - save and quit
:q - quit (no save)
x - delete one character
dd - delete the whole line

I know there are many many more, but those are enough to get you by.

perimosocordiae
if you're only using these, then a scintilla based editor (e.g. notepad++) is much much better.
hasen j
A: 

Another useful command is to search something: / e.g. /Mon will search (and in case of vim highlight) any occurences of Mon in your file.

+3  A: 
:q -> quit
:w -> save
:q! -> quit and don't save
:x -> save and quit
:[number] -> go to line number
G -> go to end of file
dd -> delete line
p -> "put" line
yy -> "copy" line
:s/[searchfor] -> search

I guess those are the basic one to start from

kementeus
A: 

As a couple of other people have already mentioned, vimtutor is the way to go. It will teach you everything you need to know in vim. The one piece of general advice I would give you is to stay out of insert mode as much as possible. There is enormous power in the other modes, it just takes a little bit of practice to get used to it.

Paul Wicks
+3  A: 

Use the 'J' (J for Join; upper-case) command to delete the newline at the end of a line. You'll find it tricky otherwise.

Vulcan Eager
+4  A: 

When you have some repetitive action to take Macros are usually faster than regex. Just type

q[0-9a-z] in normal mode

Many people use

qq

because it's fast. Press q in normal mode again to stop recording. Then just type

@[0-9a-z] in normal mode

to repeat what you just recorded.

@q

for the example like above.

Corporal Touchy
+1  A: 

alias vi nedit :)

all humor aside.. for vi WHEN NOT using nedit..

  • i (switch to insert mode)
  • a (append = move to end of line and switch to insert mode)
  • esc (exit insert mode)
  • dd delete a line
  • x delete a character
  • :wq (save and quit)
  • / start a search
  • n find Next
  • ? search backwards..
  • yy (yank) copy a line to the buffer
  • pp (paste) paste it here
  • r (replace a character)
  • <N> <command> this is a neat - but aggravating feature that lets you type digits and then a command so
  • 5dd will delete 5 lines

but at this point you might as well - man vi and refresh your memory

While there are LOTS more, I switched from Vi to nedit several years ago, which I find has more features I can use on a regular basis more easily. Tabbed editing, incremental search bar, column select, copy and paste. sort selected lines, search and destroy within selection, whole doc or all open docs.. tear-off drop down menus.. and it supports syntax highlighting for all the languages I use.. (with pattern files I've used a long time over the years. VIM many now be equivalent, but It has to introduce a feature that Nedit doesn't and an easy way to migrate my pattern files before I switch again.

jbdavid
A: 

It's also good to run the vimtutor when learning these commands

epatel
A: 

i - insert mode (escape to exit) dd - delete line shift-y - 'Yank' (copy) line p - 'Put' (paste) line(s) shift-v - Visual mode used to select text (tryin 'yanking' this text and 'putting' it somewhere. ctrl-w n - create new window (you can open a file or start new file here) ctrl-w v - split existing window vertically ctrl-n (in insert mode) - autocomplete (if supported) :! to run a shell command, usually with standard in as the file or a selection (shift-V)

Useful plugins to look at: * Buffer Explorer - use \be to view files in the buffer (and select to re-open)

jonfm
+4  A: 

I like this QRC!

http://www.fsckin.com/wp-content/uploads/2007/10/vi-vim_cheat_sheet.gif

metao
A: 

My biggest tip: ctrl+q saves the day when you accidentally hit ctrl+s to save the file you are working on

iAn
A: 

NB vi is not vim! vim is rapidly turning into the emacs of the new century. nvi is probably the closest thing to the original vi. Here's a nice hint: "xp" will exchange two characters (try it).

"vim is rapidly turning into the emacs of the new century."I am honestly not certain about how to interpret this.
Jeff
+4  A: 

This recent Vim tutorial from IBM is pretty good

gnobal
A: 

replace 'foo' with 'bar' everywhere in the file :%s/foo/bar/gc

nikolay
+8  A: 

What I find irreplaceable (because it works in vi also, unlike vim's visual mode) are marks. You can mark various spots with m (lower case) and then a letter of your choice (eg x). Then you go elsewhere, and can go back with "backquote x" (I don't know how to enter a literal backquote here) to the exact spot, or with 'x (apostrophe letter) to go to the line.

These movements can be used as arguments to commands (yank, delete, etc). For example, you want to delete 10 lines; instead of counting and then moving to the topmost line and entering 10dd, you go to either the start or the end of the block, press mm (mark m), then go to the other end of the block, and press d'm (delete apostrophe m). If you use backquote instead of apostrophe in this example, then the deletion will work character-wise, not line-wise. Try marking in the middle of the line with "mark m", moving to the middle of another line, then entering "d backquote m" and you will see what I mean.

ΤΖΩΤΖΙΟΥ
A backtick goes to where a bookmark is. Like this: `ma` to bookmark at the cursor position, and ``a` to scroll to it.
Yktula
You say to-ma-to, I say back-quote. :)
ΤΖΩΤΖΙΟΥ
+1  A: 
" ~/.vimrc
" Turn on line numbering
set nu
" Turn on syntax highlighting
syntax on    
" Set 4 space expanding tabs
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
"turn off line wrapping
set nowrap
" Map CTRL-N to create a new tab
:map <C-n> <ESC>:tabnew<RETURN>
" Map Tab and CTRL-Tab to move between tabs
:map <Tab> <ESC>:tabn<RETURN>
:map <C-Tab> <ESC>:tabp<RETURN>
+4  A: 

I was very happy the day I learned about using * or # to search, up or down respsectively, for the word under the cursor. Make sure to :set incsearch and :set hlsearch first.

Dominic Dos Santos
+1  A: 

If you're using vim, the 'u' command (in command mode) will Undo the last command you typed. You can use this command repeatedly to undo mistakes you may have made before saving the file.

+2  A: 

See http://www.rayninfo.co.uk/vimtips.html for a great collection of Vim tips, from the basic can't-live-without to very sophisticated stuff that you might never have thought of trying.

JP Lodine
A: 

Although this is a matter of personal preference I've found that one of the essential things to do is to remap Esc to something else.

I find it very uncomfortable to reach for the Esc key to exit insert mode, but the beautiful thing about Vim is that allows key mappings.

I'm currently using the following mapping using Control + S :

inoremap <C-s> <Esc>:w<CR>

This has the advantage of being a key mapping I have already commited to memory and has the added value of saving my work every time I go to normal mode. Yeah, I know it is crazy but I would be hitting the save command that frequently anyway. It's like a bad habit, you know.

Sergio Acosta
ctrl-[ is equivelant to Esc
hasen j
ctrl-[ is even harder to hit than Esc if you are not using a US standard keyboard layout (my case). But the idea is that you are not restricted to Esc, as many non Vim users think. Thanks for the comment.
Sergio Acosta
+1  A: 

I like the Vim 5.6 Reference Guide, by Bram Moolenaar and Oleg Raisky.
You can directly print it in booklet form, easy to read, I always have it laying around.
It's a tad old, but what are 8 years in Vi's lifespan ?

Berzemus
what other software has withstood the tests of time better than vim?
ojblass
+2  A: 

Lots of great commands are listed at the Vim Tips Wiki.

Jonas
+1  A: 
:set ignorecase smartcase

Makes searching case-insensitive, unless your search includes a capital letter. Not the most indispensable perhaps, but I find myself setting this option any time I'm editing in a new place. It's in any vimrc file I own.

Jeff
I didn't know about that one, thanks!
Jason Day
A: 

The real power is in the searching. Here are the essential commands:

/Steve will find the first instance of "Steve" in the text. n will find the next "Steve" in the text. :%s//Stephen/g will replace all those instances of "Steve" you just searched for with "Stephen".

Not to promote myself, but I wrote a blog post on this subject. It focuses on the critical parts of Vim for a beginner.

Steve Rowe
+1  A: 

:%!xxd

View the contents of a buffer in HEX.

Cannonade
A: 

My favorites:

%  find matching bracket/brace 
* and #  next/previous match
gg top of page
G end of the page
<Ctrl-v> Change to visual mode and select column
<Ctrl-a> increase current number by 1
<Ctrl-x> decrease current number by 1
Running macros
Shashikant Kore
A: 

Nobody mentioned exuberant ctags? Google and install it; much better than the default ctags you probably have. To use it, cd to your project root and type

:!ctags -R .

Builds a database of everything in your project...java, c++, python, ruby, javascript, anything, in a file called tags.

:help ctags for a host of commands, too many to summarize, for using the generated tags. Put the cursor on a function name, type CMD ], to open the file that defines it. Many more commands like that. Soon becomes second nature...almost as nice as an IDE (and VIM never lets you down they way eclipse often does.

Brad Cox