DEV Community

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

Posted on • Updated 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 !

Latest comments (50)

Collapse
 
fannyvieira profile image
Fanny

Really short article and very useful! Thanks!

Collapse
 
ezzaimsoufiane profile image
ezzaimsoufiane

Rak ghaya 😂❤

Collapse
 
rupankarghosh profile image
RupankarGhosh

The most irritating thing about vim in the first time was 'hell,how do I get out of this programme' . I bet even today if you introduce a new web developer with vim his first reaction will be "how do I close this shit?"

Collapse
 
outerrim profile image
Stephane ⭐ ⭐

I use ":zz" to save and quit. That is my daily tribute to Zinedine Zidane accomplishments!

Collapse
 
erebos-manannan profile image
Erebos Manannán

The minimum you need to know is

<Esc><Esc><Esc><Esc><Esc><Esc><Esc><Esc><Esc>:q!

or alternatively killall -9 vim

and

export EDITOR=<any other editor>

:)

Collapse
 
sfarias051 profile image
Sebastián Farías

Not forget the ":x" in case you want to exit and save the changes.

Collapse
 
rossijonas profile image
Jonas B. R.

I've used Sublime Text and Atom for a while... and then tried VSCode. That was the final point that made me switch to VIM!

Vim is awesome, the learning proccess is what makes me more excited to use it and I never get bored of it. There's always new tricks, you can program scripts to better your workflow... It's more than my text editor of choice, it's my videogame 😂.

After using Vim for a while, I decided to write quick tips and tutorials for Vim beginners, to help them deal with specific configs and get excited with tricks you can use.

You can check it on medium.com/vim-drops !

Cheers!✌️👨‍💻⌨️👩‍💻✌️

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
 
luismilanese profile image
Luis Milanese • Edited

No way I'm installing this on my Windows. I use Windows mostly because I want to be the farther the possible from vim. LOL

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
 
jaquino94 profile image
Justin

You can also do

:set number relative number

to have line numbers and relative numbers at the same time so you can copy/delete lines of code easier without losing which line you are on in your code :)

Collapse
 
vlasterx profile image
Vladimir Jovanović

If you NEED to torture yourself in linux console, just use nano

Collapse
 
bugsenpai profile image
bugsenpai

I love vim but I only use it my my server (remote through SSH), with big file vim doesnt have any advantages like visual code and sublime text

Collapse
 
moopet profile image
Ben Sinclair

Off the top of my head:

With big files, it usually opens faster and can be told to run a script on that file such as a saved bit of command history. It runs in the context of your terminal and has access to all your local scripts.

It's obvious advantage vs Sublime Text regardless of file size is that it's free software.

Collapse
 
thor574 profile image
Thor Hovden

Nice to see someone discovering the pleasures of VI!
I've used it extensively during 1990-2000 and only very sporadic since.

Here are some movement commands that are still in my fingers;

WBhjklHML0$
(while pressing [ctrl]) fbud

all these works with numbers in front, like this: 9L

Collapse
 
fengshangwuqi profile image
枫上雾棋 • Edited

I used vim for few mouths, and I deeply falling in love with it, this is my vim config, as well as tmux and zsh, welcome to reference and exchange.
to-vim-tmux-zsh