Vim Basics
OStatic
Home
Blog
Questions
Software
Members
 
 
 
 
Follow Us:
Follow us on Twitter
Subscribe to our RSS
About
Contact
Vim Basics
by Jon Buys - Jan. 28, 2013Comments (10)
Related Blog PostsThe Year of DockerProfiles of Linux ProfessionalsFlashbacks to CVSExamining Linux Load AveragesDeciphering Top
No server, desktop, or laptop install is complete without Vim, and yet, there are times when I still see questions pop up on IRC about how to do basic editing of config files with vi. I remember, years ago, asking some of the same questions of an older Unix guru, and asking why I should bother learning such an eccentric and “outdated” text editor. His answer has stuck with me, he said “Because it is the only text editor guaranteed to be on every server, and some day you will need it, and have no other alternatives.” Vim, short for “vi improved” is ubiquitous, but it is also so much more, and the time you spend learning it will be repaid to you tenfold in productivity. Vim is designed to be run from the command line, without a mouse. To get around in a file, Vim uses three different “modes”, where the same keys might do very different things, depending on which mode you are in. From my experience, this is the biggest obstacle of learning Vim, wrapping your head around what mode you are in, and how to navigate the text. When you first launch Vim, you are in normal mode. Normal mode does not allow you to edit the text directly, but allows you to move the cursor around the text using the keyboard. The basics of what you need to know are: j - Move the cursor down one line k - Move the cursor up one line l - Move the cursor to the right one character h - Move the cursor to the left one character Once you have the cursor where you want it, you can press “i” to enter the next mode, “insert”. Insert mode behaves more like other text editors, where the keys you press show up as text characters in the file. When you are done editing, you return to normal mode by pressing “Esc”. This is a good start for getting used to how Vim works, and over time, those keys will become ingrained as muscle memory, you might find yourself inadvertently reaching for the escape key after typing in a web form. Moving around one line or one character at a time is fine, but time consuming. What if you would like to move the cursor down five lines? In that case, you would enter “5j” in normal mode. What if you know exactly what line number you would like to jump to, say line 42? Enter “42G” in normal mode, and you will jump down to that line. Or, if you would like to jump straight to the end of the document, you could just enter “G” by itself. I do this quite a bit, jumping between “1G”, the first line of the document, and “G”, the last. If you would like to move 10 characters forward, you would enter “10l” in normal mode. Same to move back, “10h” What if you just moved down ten lines, and would like to move down another ten lines? You could enter “10j” again, but it would be faster to use another Vim shortcut, the humble period. Hitting “.” in normal mode repeats the previous command. So, whatever you did last, hitting “.” will tell Vim to do it again. Hitting “i” in insert mode will allow you to start inserting text, however, there are a few other options as well. a - Append, or, start adding text right after where the cursor is o - Create a blank line on the line below the cursor, move the cursor to this line, and enter insert mode. O - Same as above, but the new line goes above the cursor, not below Once you have the basics of when and how to insert text, moving around the text becomes more important. Luckily, Vim knows text well, and has a few tricks here as well. w - Move the cursor ahead one word b - Move the cursor back one word ( - Move the cursor back one sentence ) - Move the cursor forward one sentence What about copy and paste? Vim has that covered as well, but instead of “copying” text, you “yank” text with the “y” command. To yank the current line, which means to copy the current line, you press “yy”. To paste the line, move the cursor to where you would like it and press “p” in normal mode. So, now that you can fly around your text with ease, and insert new text at will, what are you going to do about the plethora of typos? A few more normal mode commands to commit to memory are: d - Delete one character, word, sentence, or the entire file if you like r - Replace one character R - Replace all the characters till you hit escape, just write right over them To use the delete command, you can either place the cursor over the text you would like to delete and press “d”, or you position the cursor at the beginning of a string of characters you would like to delete, for example, to delete one word, you press “dw”. To delete five words, you press “5dw”. To delete a sentence , you press “d)”. To delete the entire file, you could jump to the beginning of the file: “1G”, and delete to the end: “dG”. And if you make a mistake? No problem, Vim keeps a detailed record so you can undo the last command with “u” in normal mode. So, you can jump around the file, insert text, and delete text. How do you save the text? To do this, we need to enter a third mode, command mode. To enter command mode, you must first be in normal mode, and then press “:”. You should see the cursor move to the very bottom of the window next to a colon, waiting for you. To save the file in the current working directory, you would enter: :w FileName.txt If your file already has a name, then simply entering “:w” will work. That’s most of what you need to know to become proficient with Vim, but of course, there is so much more. I’ve barely scratched the surface of what Vim can do on its own, not to mention Vim’s vibrant community of users and developers who have extended Vim through plugins. It is through the plugins that some real magic can start happening. However, to avoid asking on IRC how to edit a text file, this should have you covered. Finally, when you are finished editing, jump into command mode and enter “wq!”.
sysadmin vim Vi
Previous: Hortonworks Signs on...Next: Mageia 3 Beta 2 Rele... Browse Blog
Gerard Braad uses OStatic to support Open Source, ask and answer questions and stay informed. What about you?
 
10 Comments
 
by Innocent Bystander on Jan. 29, 2013Oh wow, count the number of lines, type 5j to move cursor down 5 lines. Count the number of characters and type 10h to move cursor 10 chars left. Oh no, that was 12 chars that I wanted, no problem, type 2h.
VIM is so powerful it can even copy and paste one line at a time. However it must be an entire line. For a block of text, we must probably wait for another 3 centuries. With all these obscur navigation formulas, one day, VIM users will be able to call for rain or warm weather.
"the time you spend learning it will be repaid to you tenfold in productivity": I suppose VIM will ba a gient hit once porte to Android. In the mean time, thanks goodness, the people who design LibreOffice, Eclipse IDE, Google Docs, etc. are not aware of VIM. Because if they made the navigation of their tools the VIM way, that would drive people crazy.
Well I am going to give you a simple solution. Just go out and buy yourself a "mouse". A "mouse" is a computer device either with wire or wireless. It costs around $10 to $20. Once plugged in to your computer, you hold it and move it around. Then you will see something moving on the screen which reflect the movement of your hand. The best for you would be to go to a computer shop and ask the sale person to show you how to use a mouse. You will get it in 5 minutes.
Like Farenheit who invented his crazy temperature scale. The man who invented VIM had no clues about ergonomy. It is possible to write a text editor with simple navigation rules and even using a mouse. Microsoft Word text version did it 20 years ago. VIM is a secte, not a text editor.
0 Votes
by an anonymous user on Jan. 29, 2013Vim can use a mouse.
Vim has a gui interface when launched via gvim.
You can select text with a mouse.
You can use the visual mode to select any text with the keyboard.
Word has had a GUI since 1985, and it is a word processor. I'm not sure how that's relevant to a discussion about Vim. Do you use Word to edit text files?
0 Votes
by Linux is the answer on Jan. 29, 2013@Innocent Bystander: You have no clue about vim, do you? (try Ctrl-V and v for block editing)
Vim is like a Formula One car - not everyone can learn to drive it, but if you learn it you'll be really fast.
0 Votes
by Innocent Bystander on Jan. 29, 2013I mentionned Word just to give an idea about the texte navigation. If you want a text editor, it's Multi-Edit, the DOS version. If you know Word you know the basic navigation. The point is that these editors know how to use mouse, arrows keys, Function keys, common key shortcuts.
Nothing prevent you from living backward in time. But please don't boast that the prehistoric VIM skill is productive. An ancient man carving text in stone would argue he writes his texte 10x better than you writing your text with VIM. This ancient man would not understand why people would need other tools than a chisel and stone slab to write text.
The VIM users are the neanderthal remnants of the computer age. It is very possible that some IT jobs still requires the knowledge of VIM. But it is delusional to think that learning VIM would give you the upper edge in IT skills.
"Because it is the only text editor guaranteed to be on every server". Likewise, there are also rocks and dirt everywhere. Strangely enough, paved roads are the most convenient.
0 Votes
by an anonymous user on Jan. 29, 2013Innocent,
If Vim is not the tool for you, you should probably not try to ridicule it uninformedly.
> count the number of lines [...] count the number of characters
If you're used to text editing (monospaced!), then you don't have to count. You know the number of lines and characters.
> VIM is so powerful it can even copy and paste one line at a time. However it must be an entire line.
Not true. You can also C&P a defined number of characters and words(!).
> For a block of text, we must probably wait for another 3 centuries.
I'm not a power user of Vim. For a block of text, I'd usually select it with my mouse and drop it via middle-click. Maybe power users can tell me how to do it without a mouse. But then, what you visually called "a block of text" is in most cases a paragraph, and a paragraph is exactly one line. You've never used Vim?
The rest of your comment is, indeed, uninformed dirt.
Vim interacts nicely with a mouse, as has already been said (and I myself use the mouse from time to time). But if the mouse lies next to your keyboard - why reach over there if you can type what you want right there where your fingers are resting?
Critique from a power user would be valid, but not yours.
That said, I agree with your remarks on the Fahrenheit scale.
0 Votes
by an anonymous user on Jan. 29, 2013Wow! When those who have half a clue spew sarcasm, it can be very annoying, but when someone comes along spewing sarcasm who has no clue at all and acts as if they not only have a clue but try to sound educated at the same time, it is simply hilarious.
0 Votes
by Innocent Bystander on Jan. 29, 2013@anonymous I believe I have already answered to your comment and the answer was listed as comment #4. Look like there was a data restore somewhere.
I mentioned Word as an exemple, there was other text editor. The one I know well was MultiEdit, DOS version. It has the same basic navigation than Word, but there are more configurable keystrokes. The point is that these programs know how to use the hardware: mouse, arrow keys, function keys, Ctrl, Alt keys AND they have a menu.
As a strange coincidence, modern softwares, let's take Firefox as an exemple, it uses scrolling menu like in the text version of Word and MultiEdit. Instead of the silly command mode as in VIM. Clearly the VIM way of navigation is not the best. The people who had invested too much time learning this archaic VIM interface try to self justify their investment. The same way as some delusional people who claim to be kidnapped by aliens rather than admidting they are in bad shape.
A caveman would claim he carves a text in stone 10x faster than the VIM editor. And he is certain he is right, for the simple reason that chisel and stone are his only tool. Likewise, the VIM users are the neanderthal remnants of the computer age.
It's not b/c VIM is found in all distro that you must learn it. Would you elect me as president if I tell you that: from now on, we won't spend a single dollar on road maintenance. There are rocks and dirt roads everywhere, learn to use them, you will gain better productivity.
0 Votes
by an anonymous user on Jan. 31, 2013Sounds like you tried using it and failed miserably. It's kind of bizarre that you're trying to tell other people what programming tools they should be most productive with. It doesn't really work like that.
Block editing (Ctrl-V) is probably my favorite feature in the base vim install; I always miss it when working with other editors. But the plugins are what make it awesome. For most C/C++ editing (I suppose Innocent Bystander would chastise me for using such an archaic language) clang_complete and superTab is amazing. I'm not a fan of the things that pack everything but the kitchen sink into vim (file explorers and the like), but there are a ton of useful ones. It's my editor of choice for most other languages as well, e.g. bash, Lua, Python, Javascript.
Obviously it's not a silver bullet: when I'm developing GUI applications I use a C# or Java IDE since the GUI designers are too useful to pass up, and I'm not good enough to do Android development without the Eclipse integration. But that doesn't make vim any less awesome.
0 Votes
by qoonik on Feb. 01, 2013http://www.openvim.com/
1 Votes
by eebrah on Feb. 03, 2013@innocent Bystander
I do not understand your point, you *can* use a mouse and GUI ..... it is called GVIM and you can navigate using your standard arrow keys, you can also manipulate multi-line blocks of text.
What exactly is your problem with VIM? that you would spout falsehoods about it?
To each his own and if it is not your cup of tea, please, move along and use whatever suits you best. It is called freedom of choice.
0 Votes
Share Your Comments
If you are a member, Sign in to have your comment attributed to you. If you are not yet a member, Join OStatic and help the Open Source community by sharing your thoughts, answering user questions and providing reviews and alternatives for projects.
Your Name
Email Address (kept hidden)
Your Comment *
Promote Open Source Knowledge by sharing your thoughts, listing Alternatives and Answering Questions!
 
Explore Software in this Blog Post
1
2
3
4
5
Vim has 2 reviews17 users
1
2
3
4
5
vi has 1 review4 users
1
2
3
4
5
sysadmin has 0 reviews1 user
Featured MembersViewCraig HarrisI'm a tech analyst at a local firm here in Cambrid...
ViewGerard BraadSoftware/Hardware engineer and Visuals Artist. Maintains Unix and Windows systems/networks. Fanatic user of Linux and Unices.
Related Questions
Browse
Get answers and share your expertise.
Have a question? Ask the community
editing using AWK
By Paul Sands - Jul 01, 2008
2 answers
Partner Center
Happening Now on OStatic
GOKI commented on Monitoring Web Services With Nagios
anonymous commented on Dell and Red Hat's Latest OpenStack Alliance To Open More Enterprise Doors
Ashish commented on 2015 Red Hat Summit Announced
Home
Blog
Software
Questions
About OStatic
Contact
Terms of Service
Privacy Policy
Send Feedback
Powered by Vox Holdings
© 2015 OStatic. Built on fine Open Source Software from projects like
Apache,
Drupal,
Java,
Linux,
MySQL and
PHP.
Sign in to OStatic
close
Username: *
Password: *
Not a member? Join NowI forgot my password