DEV Community

Cover image for Vim for starters - the minimum you need to know
Hamza Tamenaoul
Hamza Tamenaoul

Posted on • Edited on

Vim for starters - the minimum you need to know

Originally posted in my portfolio.

The first time I heard about Vim, it was 5 years ago. And my natural reaction was : " Why in God's name, will I sacrifice the comfort of a modern text editor like sublime text, for an outdated, ugly, and complicated text editor ? " (You can't even use the mouse !).

Last year, I immersed in the fabulous world of Linux customization, this fixed the ugly part. So I decided that maybe I should give Vim another shot. And now, I am a happy Vim user for six months.

In this article I will not try to convince you to switch to using Vim, I will only give you the basics, the "what you need to know as beginner" to dive in the fabulous world of Vim.

Let's start !

Installation

Windows:

Well you need to download the installer from the vim website, install it, and you are good to go !

Linux ( Debian based distributions ) :

It's as simple as it seems !

sudo apt-get install vim
Enter fullscreen mode Exit fullscreen mode

Mac :

You can use Homebrew.

brew install vim
Enter fullscreen mode Exit fullscreen mode

A little bit of theory (just kidding)

Before you start using Vim, you should be aware that vim has a different philosophy. For starters, your mouse in the beginning is useless. Secondly it interprets what you type on your keyboard based on which mode you are in. Depending on the mode, your input will trigger different Vim behaviors. The default mode, the one you'll find yourself using when you open vim is the normal mode. it's in this mode that you'll be navigating in your file or executing vim commands. The second one is the insert mode, the most friendly, since it makes vim do what you expect a text editor to do, write text. There are other modes that I won't cover in this article since they are more advanced. As a reminder, if you find yourself lost in a mode, then the escape key is your friend, it will put you in the normal mode.

The fun part !

Opening or creating a new file:

Now open your favorite terminal (or CMD/PowerShell), and type the command :

vim name_of_your_file
Enter fullscreen mode Exit fullscreen mode

This will open the file you specified, or the create a new one if it doesn't exist. Once you do this you'll find yourself in the normal mode. We are not writing text right away, first you need to know some basic Vim commands. A Vim command usually start by typing ':' then the command, and the first one is exiting Vim since it's the most asked question about Vim.

  • ":q": To quit Vim. Note here, Vim won't allow you to quit if you didn't save your file.
  • ":w": To save (write) a file.
  • ":wq": To save and quit.
  • ":q!": To force quit if you want to force quit without saving the file.

Writing some text (Finally ...):

Well if you survived until here, you'll find your reward by typing "i". It will put you in the insert mode, and now you can type text as you please. And when you are done, you can use your escape key to go back to the normal mode to save your changes or quit.

Some other useful shortcuts (In the normal mode) :

  • "dd": To cut the line where the cursor is.
  • "yy": To copy the line where the cursor is.
  • number_of_lines_you_want_to_cut + "dd": You guess it, it will cut the number of lines you specifies starting from the current line of the cursor.
  • number_of_lines_you_want_to_copy + "yy": Again the number of the lines you want to copy starting from the line with the cursor.
  • "p": To paste starting from the next line of the cursor.
  • "/" + term_you_want_to_search: it will make the cursor go to the next occurrence of the term.
  • "n": To go to the next occurrence of the term.
  • shift + "n": Go to the previous occurrence of the term.
  • "u": If you messed up something, this will undo the last change. You can press it multiple times to go older in the changes' history.
  • ctrl + "r": To redo the changes.

Bonus (Showing line numbers):

:set number
Enter fullscreen mode Exit fullscreen mode




Conclusion

Vim, or not Vim, this is not the question. A text editor is ,first of all, just a tool to achieve bigger things. But a great tool, makes them easier. I hope that, with this article, I have helped you in your quest of finding the adequate text editor for you. And that this quick introduction to Vim, have opened your appetite to learn more about Vim, or a least have expended a little bit the horizon of your knowledge.

PS: This is my first blog post ever, and english is my third language, so I am open to feedback and good criticism, and thanks for reading !

Top comments (50)

Collapse
 
ben profile image
Ben Halpern

Why in God's name, will I sacrifice the comfort of a modern text editor like sublime text, for an outdated, ugly, and complicated text editor?

Definitely my first reaction too πŸ˜„

Collapse
 
amorgaut profile image
Alexandre Morgaut

Sorry but this comment looks anything but constructive.

Not only vi is an important historical editor (as emacs), but it is also available on almost any linux/bsd server or embedded os which becomes even more important in current IoT area.

Oh I forgot... it's also natively on windows now
blogs.windows.com/buildingapps/201...

Sure, Sublime, WebStorm, VS Code, plenty IDE or modern text editor can be preferred on day to day basis development. But such simple tutorial should not be denigrated as you sure will be happy to know those basis one day.

Collapse
 
richjdsmith profile image
Rich Smith

Eh, when I get stuck working on something remotely and am forced to use something for a quick patch, I'll just use nano.

Heck, I even got a workflow set up where I can edit remote files locally using VS Code (using this solution: codepen.io/ginfuru/post/remote-edi...)

Between these two methods, I really don't need to learn Vim or Emacs. I've given Emacs (via spacemacs) a fair trial, but honestly, some of us just prefer a GUI Text Editor and the ability to use a mouse on occasion :/

Thread Thread
 
amorgaut profile image
Alexandre Morgaut • Edited

Saying that you can also use nano, or or scp to edit locally, can be a constructive comment. :-)

But even with that, it doesn't means that:

  • knowing about vim is useless
  • writing a vim tuto is a waste of time

I don't get the point to go reading let's say a Webstorm tutorial and comment "Hey! this tuto is shit, just use VS Code it's free..."
The title clearly states what the content is about, read it if your interested, add hints about other solutions if you want, but DON'T DENIGRATE, otherwise your just trolling

I'm more comfortable with vim than with nano...
I occasionally use SCP to edit remote JSON configs locally
I like very much VS Code
I mainly use Webstorm at work.

That's me, I won't denigrate nano or any editor, neither tutorial about any of them

Any tuto about any editor is interesting

Thread Thread
 
neeljsomani profile image
Neel Somani

Are we reading the same comment here? I don't see anything that indicates the tutorial is useless. It's just a joke.

Thread Thread
 
amorgaut profile image
Alexandre Morgaut

My reply was initially against that reaction:

Why in God's name, will I sacrifice the comfort of a modern text editor like sublime text, for an outdated, ugly, and complicated text editor?

The comment looks to have been deleted but the quote from Ben Halpern on which I replied remains here.

In short it is saying: "Vim sucks (outdated, ugly, complicated). Hey everyone use other things (like sublime), so don't bother with this tuto..."

The blog post isn't asking anyone to change its editor, it is just providing a tutorial.

Maybe I over-reacted a little, but way too much often we see such comments with for main effect to discourage people to share their experiences and bring their contributions (tuto, tools, bug reports) to the community.

So no for me, it is not that much a "joke"

Thread Thread
 
stringnamematt profile image
Matt

That was the beginning of the article.....

Collapse
 
voyeg3r profile image
SΓ©rgio AraΓΊjo

Asking such question to those who use vim for a couple of years does not make any sense. Why? Knowing vim enough will completely change your mind, believe me! To me, it sounds like someone who uses java arguing why people like using python. After becoming used to modal editing and some powerful features like global commands and text-objects I can say that I will never use another editor.

Collapse
 
gravityflower profile image
gravityFlower

As soon as you have Git installed on Windows and are required to make changes not supported by your GUI, as soon as you commit your changes, you will be confronted with vim, because it is the default editor. Yes, you can change the default editor or you can then commit on the GUI, but that would be, im my opinion, counterproductive.
Also, as soon as you start with Containers (like Dockers), you have no GUI. If you don't use volumes, you probably can work by copying files back and forth, but that is also not really useful. So therefore, a basic knowledge of vim is always helpful.

Collapse
 
bensandeen profile image
BenSandeen

Vim's plugins are what make it useful for me. I usually work in an IDE, but for a few things, I'll open a terminal, navigate to the file, and then use vim for a small change. One such plugin that is invaluable is vimawesome.com/plugin/vim-easy-align. With a handful of keystrokes, I can align an enormous block of lines with, say, the = sign on each line.

Collapse
 
rasputino profile image
P. LeΓ³n

Maybe. One day you'll be sysadmin, and you will need to edit a config file in a terminal. This could happen to you. Expect the unexpected.

Collapse
 
rommik profile image
Roman Mikhailov

This day is here for any developer working with docker and needs to troubleshoot a running container :)

Collapse
 
iggredible profile image
Igor Irianto

Mine was "How in Torvalds' name, do I exit this thing?"

4 years later, happy to report I am a Vimmer.

Collapse
 
saramon profile image
saramon

also my first reaction.
and also the last. :))

Collapse
 
eljayadobe profile image
Eljay-Adobe

Vim is my text editor of choice. It is my workhorse text editor, I use it day-in and day-out. It is available on every platform I work on -- and I've worked on a lot of different platforms. According to StackOverflow polls, it is one of the more popular text editors by developers.

Whenever I hear anyone asking how to get started with Vim or vi, my first reaction is "It's too late for me, save yourself!" (It's a "ha-ha, only serious" comment.)

My fingers tacitly know Vim. It gets out of my way. I can become one with my keyboard; it is a zen experience.

I used emacs for 2 years before trying to use vi. It was on a bet, and I had to use vi for 6 months; the other developer had to use emacs for 6 months. After the 6 months was up, I never went back to emacs, and the other developer couldn't wait to get back to vi.

Bill Joy, the creator of vi, has expressed that vi was created at a time of 300 baud modems, and optimized for minimal bandwidth. And with todays modern machines, the reason for vi to exist has passed.

Yet, it remains quite popular.

The reward for the steep learning curve is a highly efficient, powerful, and elegant text editor. Beautiful in its simplicity. When I have to use an IDE (these days Visual Studio and Xcode), I feel hobbled until I can get back to Vim to do serious editing on the source code I work on.

Collapse
 
pmcgowan profile image
p-mcgowan • Edited

The first few months of vim were trying, and the next 2 years were mostly amazing except for never really understanding how I triggered and could exit macro recording. I'll use a graphical editor for big, heavy projects, but nothing beats vim for regexes and ssh'ing into the production server for a hotfix.

Collapse
 
edwin_r_c profile image
Edwin Ramirez

That's what I tell all my colleagues: you never know when you're connecting to a remote server through SSH and you'll have to make due with Vim or similar.
All Dev should at least know the basics to Vim. At least opening a file, editing and saving.

Collapse
 
mosteo profile image
Álex RM

Also with the syntastic plugin you get syntax check for many languages (including bash), which is also a big help when in a limited tty environment.

Collapse
 
joegaudet profile image
Joe Gaudet

You missed the truly best feature of vim, the i directive.

Say you have this code:

function(foo, bar) {
  // some stuff
}

To select the entire params list, with the cursor in command mode and inside of the params type:

vi(

Now you can copy it or what ever

To select the entire function body

vi{

Better yet, you may want to redefine the param list:

ci(

Which deletes the params and switches you into insert mode

And finally say you are editing some HTML,

<p>Delete this message</p>

You can use:

cit

To clear everything inside of a tag.

Collapse
 
thor574 profile image
Thor Hovden

Ah, thanks!!

Collapse
 
devinamarsh profile image
Devin

I used to say that I love Vim, but I've started to realize that I actually just love Vim key bindings. Vim as a program is great, but I like to have something my coworkers can put their hands on and not feel scared. It's gotten to the point that I've even tracked down a tamper monkey script to enable Vim keys on glitch.com!

The first thing I do in any new sublime set up is enable "vintage" mode. This allows me to let someone take over the reigns if they're helping me debug, but still use most of the vim comforts.

When I learned it back in college, I remember reading an interesting analogy. It was something like, "Learning Vim is like learning an instrument. At first it feels awkward and frustrating, but after much practice it'll be like an extension of your body." I think almost everyone would love to learn it, but it can be a daunting task. An interactive vim training program/playground could be a cool side project πŸ€”

Collapse
 
sfteague profile image
Teague Ashburn

I've also noticed that my interest in Vim has mostly to do with the key bindings. What I don't want to have to do is dive into the Vim configuration rabbit-hole. Right now I'm enjoying Atom paired with the vim-mode-plus package.

Collapse
 
merijnv profile image
Merijn Vogel • Edited

As a 20+year vi/vim-user I think I'd like to agree about the key-bindings.

Vim to me is not an IDE, it is an editor with a certain mental model. I use the vim key-bindings in IntelliJ, and indeed do not want to go into the deep configuration rabbithole of vim itself.

My use started at university terminals in the nineties; other students were navigating all across the screen without moving their hands :)

Collapse
 
moopet profile image
Ben Sinclair

You mean like vim-tutor or... vim-adventures.com/ ? :P

Collapse
 
daemoen profile image
Marc Mercer

there is a slight interactive training program -- vim-tutor. It doesn't cover everything, but it is enough to get folks started.

Collapse
 
albfan profile image
Alberto Fanjul

set mouse=a

Boom!

Collapse
 
strredwolf profile image
STrRedWolf

You forgot to mention some history of Vim... and it's predecessor, vi.

You see, vi was built when terminals themselves weren't quite good enough and the good ones you could run Emacs on were expensive. Yes, I'm talking the DEC VT52's. The ones with good cursor addressing and dedicated commands to do non-destructive movement. Expensive buggers.

No, vi was built for brain-damaged terminals like the Wyse 50. The ones where up and down were defined but left was BACKSPACE and right was SPACE. So putting in a dedicated non-edit mode was worth while back then.

I'm not old enough to have regularly used a Wyse 50. But I'm old enough that I had to work through their legacy at a government transit agency. Yes, I had to deal with a terminal emulation of a Wyse 50 because all the old software was specific to the Wyse 50, and one of the old coders was adamant on using a custom TERMCAP even if the Linux version was even better. The text editors you had to use got cut down to two because of the mandate to use Wyse 50: Vim, or ed.

Needless to say, I'm in private industry now, and I hear that said agency is moving to a different payroll system which would cut out the need for the Linux setup.

Collapse
 
lilyz622 profile image
Lily Zhang

Thanks for the article! I'm a beginner in vim and I learned some new commands from your article! I used to think the same too, but then I had so ssh into my amazon ecs server and all I could use was command line and hence vim.

Collapse
 
noblebe4st profile image
Jeff Hall

I'm an Emacs guy, but I've been experimenting with Vim. I'm not leaving Emacs, but I would like an editor in the terminal that can be made pretty darn useful with a portable dot file. Terminal Emacs is fine for a quick visit, but I wouldn't want to live there. So I'd like to have just some basic Vim literacy. I don't think I'm going to get too crazy with it. And with a few plugins I've already got a decent looking editor in my terminal window. So there you go, a hat tip to Vim.

Collapse
 
themattyg profile image
Matt Graham

If you need a command line editor, nano is much more user friendly. I make sure all the command line tools that open a text editor for any reason (like when git wants a merge comment) it opens nano. Vim needs to be left where it came from: the past. #myopinion

Collapse
 
daemoen profile image
Marc Mercer

nano is great in that it is easy to learn, but it also not nearly as powerful.

An example as a sysadmin modifying/working on a log file (columnar data) -- If you want to work on lets say, the first 8 characters of a specific block of text across multiple lines, how would you select only those 8 characters on each of the lines without going one by one in nano? With vim, you have visual block mode, which makes selecting this columnar group easy, and then you can run your command operations on that selection only.