avatar_url

Sandesh Rana | Python

I'm a developer from Himalayas, with a passion for crafting efficient and elegant code, with creativity and precision.

Learning Vim Commands

15 Feb 2022 » linux, unix, vi

Vim

Vim - is Vi Improved editor for programmers.

The Ultimate Vim Cheat Sheet: Boost Your Productivity with Vim

Vim is a powerful and popular text editor known for its efficiency, speed, and extensive customization options. Whether you’re a seasoned Vim user or just starting your journey with this versatile editor, having a cheat sheet handy can greatly enhance your productivity. In this blog post, we present the ultimate Vim cheat sheet that will help you navigate Vim’s vast array of commands and features.

Vim Modes

ModeDescription
Normal ModeThe default mode for navigating and executing commands.
Insert ModeUsed for inserting and editing text.
Visual ModeAllows selecting and manipulating blocks of text.
Command-Line ModeEnables entering commands and searching.

Basic Movements

CommandDescription
h/j/k/lMove left/down/up/right.
0Move to the beginning of the line.
$Move to the end of the line.
ggMove to the start of the file.
GMove to the end of the file.
wMove to the beginning of the next word.
bMove to the beginning of the previous word.
Ctrl+fScroll forward one page.
Ctrl+bScroll backward one page.

Editing and Manipulating Text

CommandDescription
iEnter Insert Mode at the cursor.
aEnter Insert Mode after the cursor.
oInsert a new line below the current line.
OInsert a new line above the current line.
xDelete the character under the cursor.
ddDelete the current line.
yyYank (copy) the current line.
pPaste the yanked or deleted text.
uUndo the last command.
Ctrl+rRedo the last undone command.

Searching and Replacing

CommandDescription
/patternSearch forward for a pattern.
?patternSearch backward for a pattern.
nMove to the next occurrence of the search pattern.
NMove to the previous occurrence of the search pattern.
:%s/old/new/gReplace all occurrences of “old” with “new” in the entire file.
:s/old/new/gReplace all occurrences of “old” with “new” in the current line.
:s/old/new/gcReplace with confirmation for each occurrence.

Advanced Editing and Navigation

CommandDescription
Ctrl+wSwitch between Vim’s split windows.
:vsp filenameOpen a new vertical split.
:sp filenameOpen a new horizontal split.
Ctrl+wwCycle between open windows.
Ctrl+oJump back to the previous cursor position.
Ctrl+iJump forward to the next cursor position.
:set numberDisplay line numbers.
:set hlsearchHighlight search results.
:set ignorecasePerform case-insensitive searches.

Customizing Vim

CommandDescription
  
.vimrcCreate or modify the .vimrc file to define custom settings and mappings.
:mapCreate custom key mappings for frequently used commands.
:setConfigure various options, such as colorscheme, tab width, and indentation.
:syntaxEnable syntax highlighting for different programming languages.
:abbrevCreate abbreviations for frequently used phrases.

You could find other important vim commands from the command below:

# while editing file
:help
# to route between helpful commands that vim provides
# help ranges from tutorial to more deep vim specific
# manuals.

# shows manual page for vim and their arguments for 
# opening file
man vim

Conclusion

This ultimate Vim cheat sheet covers a wide range of essential commands and shortcuts to enhance your productivity and efficiency while working with Vim. Remember, practice makes perfect, so don’t hesitate to experiment and explore Vim’s extensive features. By incorporating these commands into your daily workflow, you’ll be well on your way to becoming a Vim power user.

Happy Vimming!

Reference