How to get started with vim đź’»

How to get started with vim đź’»

·

4 min read

  • Working in Linux mostly happens from the command line, so you need a command line text editor

  • nano is easy to use and available on many Linux installations by default

  • vim is vi improved, a very powerful editor with advanced programming features

  • vim is more difficult to use, but you should learn how to use it

1. It is always available, even on very old UNIX systems

2. It is powerful and programmable to perform test file operations in an easy way

  • To set your favorite editor as the default, use export Editor=$(which vim)

What Is Vim?

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems.

Vim is often called a “programmer’s editor,” and is so useful for programming that many consider it an entire IDE. It’s not just for programmers, though. Vim is perfect for all kinds of text editing, from composing emails to editing configuration files.

Despite what the above comic suggests, Vim can be configured to work in a very simple (Notepad-like) way, called evim or Easy Vim.

What Vim Is Not?

Vim isn’t an editor designed to hold its users’ hands. It is a tool, the use of which must be learned.

Vim isn’t a word processor. Although it can display text with various forms of highlighting and formatting, it isn’t there to provide WYSIWYG editing of typeset documents. (It is great for editing TeX, though.)

How to get started with Vim

— After starting vim, it opens in command mode

vim filename.txt

vim editor looks like the above picture. you can’t type anything when it is in command mode. use I or o to get into insert mode and then you can start typing. use Esc to get back to command mode.

After you are done typing. now how to exit, save and get back to the terminal again? it is very easy when you are typing you are in insert mode and to exit from the terminal you need to be in command mode so press Esc to get into command mode and then type :wq! which is for Write, quit, and don’t complain.

:wq! Write, quit, and don’t complain

Useful vim commands

  • U — undo the last change

  • dd — deletes the current line

  • :%s/old/newg — replace all occurrences of old with new

  • :q! — Quit without saving

  • u — Undo the last modification

  • Ctrl-r — Redo

  • v — Enter visual mode, use arrow keys to select a block

  • d — delete the current selection

  • y — Copy(yank) the current selection

  • p — to paste the copied data

  • 3dd — Delete 3 lines

  • /text — Search for text

  • gg — Move the cursor to the top of the document

  • w — Go to the next word

  • b — go to the previous world

  • ^ — Move to the start of the current line

  • $ — Move to the end of the current line

  • dw — delete the current word

  • O — open the line above the cursor position

  • o — Open the line after the cursor position

  • :se number — show line number

  • r — replace the current character

Last but not least vimtutor provides an easy-to-use on-system tutor that guides you through the essentials of using vi and vim.

Now let’s discover the option to display the content of the file in the terminal.

Using cat and tac

  • cat is used to display file content in terminal

1. cat has some nice options

  • -A shows all non-printable characters

-A shows all non-printable characters

  • -b numbers lines

-b numbers lines

  • -n numbers lines, but not an empty line

-n numbers lines, but not an empty line

2. Tac is like cat but in reversed order

Using head and tail to see file start and end

tail testfile.txt

prints the last 10 lines

head testfile.txt

prints first 10 lines

  • tail -3 testfile.txt — prints last 3 lines

  • head -3 testfile.txt | tail -1 — head -3 producing 3 lines, sends it into the pipe, and after the pipe, tail is filtering out the last one.

  • sudo tail -f /var/log/messages — to open a file in real-time

This was just a basic overview of vim editor to get started. I hope you found it useful and don’t forget to practice with different options and commands 💯.

Â