Vim the ultimate Code Editor
How Vim's unique features can enhance your coding workflow
Understand the meme? didn't? don't click away just yet! Stay tuned; you might get some programming humor along!
Did you realize that creating code and producing English prose are two very different things? It would be like comparing apples and oranges, or perhaps Shakespeare and Bill Gates. When writing code, switching between files, reading, navigating, and editing the code take up more time than actually typing it out. It's like a puzzle that must be put together correctly in order to produce something spectacular.
The fact is that we programmers spend the majority of our time tweaking code. Yes, we're like computer-aided designers who paint with code. And we need to master our editor just like a painter needs to master their brush. It's similar to having a superpower tool that can make coding seem simple.
Take these few steps to become a coding superhero:
Start by learning the fundamentals through a lesson (akin to an origin story).
Even if using the editor at first appears challenging, keep utilizing it for all of your text editing needs. L
Look into ways to make your workflow and productivity better.
The time frame for mastering a complex text editor looks like this if you use the strategy outlined above and completely commit to utilizing the new tool for all text editing needs. You'll discover the fundamental editor features in an hour or two, including opening and editing files, saving and quitting, and traversing buffers. After 20 hours, you ought to be working as quickly as you did with your old editor. After then, the advantages begin: you'll have enough information and muscle memory to use the new editor more efficiently. Modern text editors are sophisticated and potent tools, so learning never ends and you'll grow quicker the more you know.
What editor should I learn?
Which editors are in demand right now? As said in this StackOverflow survey The most utilized editor is Visual Studio Code. The most widely used command-line-based editor is Vim.
The Vi editor gave birth to Vim in 1976, developed by Bram Moolenaar, and it is still being improved upon today. Many tools offer a Vim emulation mode because Vim has some really innovative ideas. For instance, 4 million people have installed the Vim emulator for Visual Studio Code. Even if you wind up moving to another text editor in the end, Vim is definitely still worthwhile knowing.
What makes Vim Different?
Vim stands out from other text editors thanks to its modal interface, what does this mean? This means it can distinguish between inserting text and manipulating text.
Vim is more akin to a programming language than a conventional editor due to its programmability via Vimscript and other languages like Python and its use of keystrokes with mnemonic names as commands. Vim's avoidance of using the mouse and even the arrow keys makes editing quicker and more effective, keeping up with the speed of thinking.
Overall, Vim's distinct functionality and style produce an editor that may actually correspond to the way you operate.
Modes in Vim
The architecture of Vim is predicated on the concept that programmers spend more time reading, navigating, and making minor modifications than they do create lengthy passages of text. Vim has many operating modes as a result.
Insert: to insert text
Replace: to replace text
Normal: for relocating files and editing them
Visual: for selecting blocks of text
Keystrokes have different meanings in different operating modes. For example, the letter x
in Insert mode will just insert a literal character ‘x’, but in Normal mode, it will delete the character under the cursor, and in Visual mode, it will delete the selection.
In its default configuration, Vim shows the current mode in the bottom left. The initial/default mode is Normal mode. You’ll generally spend most of your time between Normal mode and Insert mode.
You change modes by pressing <ESC>
(the escape key) to switch from any mode back to Normal mode. From Normal mode, enter Insert mode with i
, Replace mode with R
, Visual mode with v
, Visual Line mode with V
, Visual Block mode with <C-v>
(Ctrl-V, sometimes also written ^V
), and Command-line mode with :
.
Vim Motions
Vim allows you to swiftly and accurately move the pointer across a page by using gestures. Motions, like melodies, are commands that you input using one or more keys. You may carry out a variety of actions with differing durations and levels of accuracy thanks to them.
I love how Vim combines such motion melodies to make it simple and quick to complete daily activities. You can rapidly and accurately move the pointer about the document using some of the fundamental Vim gestures.
Command Line
You may enter command mode by typing: in Normal mode. By pressing:, your cursor will move to the command line at the bottom of the screen. Numerous functions are available in this mode, including quitting Vim and opening, saving, and closing files.
:help {topic} open help
:help :w opens help for the :w command
:help w opens help for the w movement
:ls show open buffers
:e {name of file} open file for editing
:w save (“write”)
:wq save and quit
:q quit (close window)
Buffers, tabs, and windows
The "buffers" that Vim keeps are a collection of open files. Each tab in a Vim session has a number of windows (also known as split panes). Each window only displays one buffer. Windows are only views; unlike other applications you are acquainted with, such web browsers, there is no 1:1 relationship between buffers and windows. Even inside the same tab, one buffer might be viewed in numerous windows. For instance, it can be quite useful to simultaneously see two separate portions of a file.
Vim launches by default with a single tab and a single window.
Vim itself is a programming language!
The most crucial concept of Vim is that its user interface is a programming language in and of itself. Commands are what keystrokes (with mnemonic names) are, and these commands combine. This makes movement and editing efficient, especially if the commands are ingrained in your muscle memory.
Movement
Basic movement:
hjkl
(left, down, up, right)Words:
w
(next word),b
(beginning of word),e
(end of word)Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line)Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen)Scroll:
Ctrl-u
(up),Ctrl-d
(down)File:
gg
(beginning of file),G
(end of file)Line numbers:
:{number}<CR>
or{number}G
(line {number})Misc:
%
(corresponding item)Find:
f{character}
,t{character}
,F{character}
,T{character}
find/to forward/backward {character} on the current line
,
/;
for navigating matches
Edits
With Vim, you can accomplish all the tasks that you would typically perform with a mouse using keyboard-based editing commands that can be combined with movement commands. This aspect of Vim's interface resembles a programming language, where editing commands are known as "verbs" because they act on "nouns", which represent the text being edited.
i
enter Insert mode- but for manipulating/deleting text, want to use something more than backspace
o
/O
insert line below / aboved{motion}
delete {motion}- e.g.
dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
- e.g.
c{motion}
change {motion}e.g.
cw
is change wordlike
d{motion}
followed byi
x
delete character (equal dodl
)s
substitute character (equal tocl
)Visual mode + manipulation
- select text,
d
to delete it orc
to change it
- select text,
u
to undo,<C-r>
to redoy
to copy / “yank” (some other commands liked
also copy)p
to paste
Customizing Vim
Vim may be altered by adding instructions for Vimscript to a plain-text configuration file located in /.vimrc. You probably want to enable a lot of the default options.
Vim has many customization options, so it's worthwhile to take the time to go at them. On GitHub, you can view people's dotfiles. Listing two of them that are quite famous.
Avoid copying and pasting files as a whole instead, study it, comprehend it, and take what you need.
Exiting Vim
Approach 1: Using the :q! command
Press the Esc key to ensure you are in command mode in Vim.
Type :q! to force quit Vim and discard any unsaved changes.
Press Enter to execute the command.
You will exit Vim and return to your command line prompt.
Approach 2: Using the :wq command
Press the Esc key to ensure you are in command mode in Vim.
Type :wq to save and quit Vim.
Press Enter to execute the command.
If there are unsaved changes, Vim will prompt you to save them by typing :w before executing :wq.
Once you have saved your changes, Vim will exit and you will return to your command line prompt.
That's it! These are the two basic approaches to exit Vim. It's important to note that Vim can be a bit tricky to use at first, but with practice and patience, it can become a powerful tool for coding and text editing.
Resources
vimtutor
is a tutorial that comes installed with Vim - if Vim is installed, you should be able to runvimtutor
from your shellVim Adventures is a game to learn Vim
Vim Advent Calendar has various Vim tips
Vim Golf is code golf, but where the programming language is Vim’s UI
Practical Vim (book)
Commands Sheet - There are a lot of commands we can't list all of them here . Refer to this list to see all of them.
Exercises
So it's time for some DIY exercises follow and practice all of them .
Complete Vimtutor: Vimtutor is an interactive tutorial that comes with Vim. It covers all the basic commands and motions in Vim. Completing Vimtutor is a great way to get started with Vim and practice the fundamental skills.
Practice moving around in a document using Vim's motion commands. Start with simple movements like
h
,j
,k
, andl
, and then move on to more complex movements likew
,e
,b
,%
,f
, andt
Practice installing and using Vim plugins to extend Vim's functionality. There are many great plugins available for Vim, including ones for syntax highlighting, file browsing, and code completion. Find a plugin that interests you and install it, then practice using its features.
Practice customizing Vim to fit your personal preferences. Vim is highly customizable, so experiment with changing settings like the color scheme, key mappings, and status line to create a Vim environment that works best for you.
Conclusion
So you've learned Vim is a powerful text editor that stands out from other text editors due to its modal interface, which distinguishes between inserting text and manipulating text. Vim's avoidance of using the mouse and even the arrow keys makes editing quicker and more effective, keeping up with the speed of thinking. Vim allows you to swiftly and accurately move the pointer across a page by using gestures. Vim's different modes allow you to insert, replace, relocate, edit files, select blocks of text, and enter command mode.
Connect with me
If you enjoyed this post and would like to stay updated on my work, feel free to connect with me on social media.
I'm always open to new connections and networking opportunities, so don't hesitate to reach out and say hello. Thank you for reading! Stay tuned for more🚀🙌