Most vim introductions on the web assault you with a ton of plugins and environment configuration settings to get your vim environment like the author's. This approach is acceptible for the somewhat experienced vim user but not for someone coming to vim for the first time.
I'm just going to discuss the absolute minimum commands I feel are necessary to become instantly productive in this editor. You'll be surprised - there's not many. I assume a vanilla vim installation which you'll find on most Linuxes by default; similar configurations are available in Sublime Text's Vintage Mode and the IntelliJ/AppCode suite of IDEs through the excellent IdeaVIM plugin.
When you first open a file in vim you are placed in command mode. You know this because if you type chances are nothing appears on the screen and is probably the most frustrating thing for the neophyte vim user. Command mode allows you to enter a string of characters from the keyboard to perform certain tasks; if you hit
i you'll enter insert mode which will allow you to begin typing text into what is called the buffer. If you hit
ESC you'll leave insert mode and re-enter command mode.
Here is the list of command mode commands you need to know to begin to use vim productively:
|Getting Out of Command Mode|
|i||enter insert mode at the cursor position|
|a||enter insert mode just after the cursor position (append)|
|enter insert mode at the end of the line which the cursor is on|
|Getting Out of Insert Mode|
|Back into command mode so you can use everything that appears below|
|f ||jump the cursor to the next occurance of
|jump the cursor to the previous occurance of
|t ||jump the cursor to the character before the next occurance of
|w||move the cursor forward 1 word|
|move the cursor backward 1 word|
|gg||jump to the first line in the file|
|jump to the last line in the file|
|jump to line number
|cw||delete the rest of the word from where the cursor is and enter insert mode (change word)|
|ciw||delete the entire word under the cursor and enter insert mode (change inner word)|
|cf||delete until the next occurance of
|ct||delete until the character just before the next occurance of
|diw||delete the entire word under the cursor but remain in command mode (delete word)|
|dd||delete the entire line under the cursor|
When you look at all of the commands above you start to see a pattern emerge and realize its not just a bunch of random archaic and hard to remember key sequences.
There's a whole other class of command you can perform in insert mode: when you hit the
: character during command mode you can type a command (or shorthand command). The most important commands to know when you begin are:
|:w||write to disk|
|:wq||write changes and quit|
|:q!||exit without saving, bypassing the warning message for not saving|
|:e!||reload the current file from the last time it was saved, dumping all your mistakes|
Mostly you'll encounter vim configurations in the wild (your hosted Linux box) where you may not want to spend any time configuring the environment because its so ephemeral. In those cases here's some great tricks to keep in mind:
Like any good Unix process you can suspend vim by using
CTRL+Z. So a decent workflow is to maybe:
$ sudo vim /etc/nginx/conf.d/default.conf
... make some changes, issue the
:w command, then hit
CTRL+Z to suspend vim. You'll be returned to your shell prompt, from which you could issue:
$ sudo service nginx reload
Now if you get any errors you just type:
and the vim program will be foregrounded where you can use
SHIFT+G to jump to the line number that nginx complained about and add your missing semicolon (
SHIFT+A to go to insert mode,
; to type a semicolon).
My other favorite commands while working in a server environment that isn't configured to my liking, i.e., the defaults:
|:set nu||turn on line numbers|
|:set paste||put vim into paste mode so you can paste the
|:set tabstop=4||set the tab stop to 4 characters instead of the ridiculous default of 8|
|:set expandtab||spaces instead of tabs|
Where to go from here
The commands above are the ones I cut my teeth on when I first started using vim. I've been using vim for the past six years with great success and I have a slew of plugins I like and naturally a ton more commands I use to improve my productivity. Doubtless someone will say "you forgot
ZZ to save a file!" or
:%s/">\([^<]*\)\(.*\)$/ "> \1 \2<CR>:se sw=8<CR>qq19|dt"32|dt<>>-email@example.comL.jqq>>-q11@qZZ
for HTML formatting. Nonsense. Be pragmatic. Ignore these people and actually use vim. As you get comfortable you will find more efficient ways to navigate, find patterns in a file, create macros, and best of all customize your environment so your vim will never ever function like the person you are pairing with.
If you start trying to manage your plugins manually by downloading
.tar.gz files and unzipping and moving files around, stop. Its time for you to use Vundle which is the natural replacement [in 2014] for the excellent Pathogen (which it improves upon).