DEV Community

Cover image for Learn Vim Quickly: Essential Commands for Editing Text

Posted on

Learn Vim Quickly: Essential Commands for Editing Text

Vim is one of the most common text editors and is available on nearly every system. Most IDEs have vim-mode like Visual Studio Code and the Jetbrains IDEs.

Vim is an excellent tool for quickly writing and editing code and other text files. Once you learn how to use it, you’ll love it. It helps to have a basic understanding of the Unix command line, but if you don’t, that’s OK, too.

why vim?

The (Vim) editor is a fundamental tool for developers, programmers, and sysadmins.

Here are some reasons to use vim:

  • Vim is the only text editor you will ever need.
  • Vim is free, and always will be.
  • Vim can be used with any programming language.
  • Vim can edit remotely over a TCP/IP connection.
  • Vim runs on every operating system, but no other text editor runs on all of them.
  • Vim runs in most IDE's as plugin
  • Vim is the ultimate weapon in your programming arsenal, it should be your first choice for any code related tasks.

However, it takes some time to learn vim

new file

To create a new file: type the command

vim filename
Enter fullscreen mode Exit fullscreen mode

where filename is the name of the file you want to edit. This creates a new file and opens it using Vim.

save file

The ESC :w (write) command writes the file to disk.
To save and write the file when you exit: type :w.

open file

To open a file, press ESC :e followed by your file name. This can include the path to the filename, so you can type :e /home/you/filename.txt

quit vim

To quit vim, type the command ESC :q!. If you want to save the file and exit type ESC :wq or if you are lazy just type ZZ.

write text

To write text, you need to enter insertion mode. You can do that by pressing i or insert. Then you can write whatever you want. In vim, you can use the cursor keys to navigate around the text.

run command and insert output

You can use the command ESC :r !cmd↡ to execute a command and insert the results in vim.

If you are familiar with Unix commands, it means you can directly get the output inside your editor with commands like :r !ls /

many more commands

There are many more commands in vim. You can do what any editor can do, but with only a few keystrokes.

So why work inefficiently when you can use vim?

Check the cheatsheet to see an overview of commands

Discussion (0)