Vim
Love it or hate it, most distros have it installed. It's best to know at least how to edit files, and exit. Knowing how to exit vi is one of life's greatest mysteries.
Last updated
Was this helpful?
Love it or hate it, most distros have it installed. It's best to know at least how to edit files, and exit. Knowing how to exit vi is one of life's greatest mysteries.
Last updated
Was this helpful?
Not much here yet...please feel free to contribute at my GitHub page.
First, the most important thing to learn about vim...how to get out of it:
Exiting vi, vim, and similar editors is actually quite simple. Press [ESC]
and type :q
— if that does not work try adding a bang (!
) to it, that should work nearly every time. Understanding why it might not... well, that’s a whole different story.
If you need to save any changes you made to a file use [esc] :w
or [esc] :wq
to save changes and exit.
If you need to exit without making changes (for example when you open a read-only file by accident) type [esc] :q!
All commands must be run from Command Mode (unless otherwise specified).
[ESC]
Return to Command Mode.
i
Enter insert (normal text edit) mode.
x
Delete a character. Type a number first to delete that many characters.
dd
Delete a whole line. Type a number first to delete that many lines.
yy
Yank (copy) a whole line. Type a number first to yank that many lines.
p
Put (paste) contents of clipboard.
u
Undo the last action.
Ctrl + r
Redo the last undone action.
/text
Search for "text" in the document. Use n
to jump to the next occurrence.
?text
Search backwards for "text" in the document. Use n
to jump to the next occurrence.
:w
Save changes to the file.
:q
Quit Vim. Use :q!
to quit without saving.
:wq
or ZZ
Save changes and quit Vim.
V
Enter visual mode for selecting text.
Ctrl + v
Enter visual block mode for selecting columns of text.
:s/old/new/g
Replace "old" text with "new" in the current line.
:%s/old/new/g
Replace "old" text with "new" in the entire file.
gg
Move cursor to the beginning of the file.
G
Move cursor to the end of the file.
w
Move cursor forward to the next word.
b
Move cursor backward to the previous word.
e
Move cursor to the end of the current word.
{
Move cursor to the beginning of the previous paragraph.
}
Move cursor to the beginning of the next paragraph.
H
Move cursor to the top of the screen.
M
Move cursor to the middle of the screen.
L
Move cursor to the bottom of the screen.
Ctrl + o
Jump to the previous cursor position.
Ctrl + i
Jump to the next cursor position.
Ctrl + d
Scroll down half a screen.
Ctrl + u
Scroll up half a screen.
J
Join the current line with the next line.
>>
Indent the current line. Use num>>
to indent multiple lines.
<<
Un-indent the current line. Use num<<
to un-indent multiple lines.
:set tabstop=4
Change tab width to 4 spaces.
:set expandtab
Convert tabs to spaces automatically.
:%s/foo/bar/g
Replace all occurrences of "foo" with "bar" in the entire document.
:%s/foo/bar/gc
Replace all occurrences but ask for confirmation before replacing.
:%s/^/NEW /g
Add "NEW" to the beginning of every line in the document.
:%s/$/ END/g
Add "END" to the end of every line in the document.
:e filename
Open a new file.
:tabnew filename
Open a file in a new tab.
gt
Switch to the next tab.
gT
Switch to the previous tab.
:vsp filename
Open a file in a vertical split.
:sp filename
Open a file in a horizontal split.
Ctrl + w w
Switch between split windows.
Ctrl + w q
Close the current split window.
:r filename
Insert the contents of "filename" into the current buffer.
:w filename
Save the current buffer to "filename".
"*y
Yank (copy) text to the system clipboard (some versions of vim do not support this).
"*p
Paste text from the system clipboard (some versions of vim do not support this).
The .vimrc
file is Vim's configuration file, allowing users to customize their editing experience by defining settings, key mappings, plugins, and behaviors. It’s essentially a way to tailor Vim to individual preferences, enabling automation, efficiency, and an improved workflow. It is typically stored in the user's home directory alongside .bashrc, .profile, and other so-called "dot files".
.vimrc
Customizing Settings: Modify indentation, syntax highlighting, line numbering, and more.
Defining Shortcuts: Set custom key mappings for repetitive tasks.
Enabling Plugins: Load plugins to extend Vim’s functionality.
Optimizing Performance: Adjust behavior for smoother operation, such as disabling swap files or tweaking search parameters.
Here are some common .vimrc
customizations to enhance your Vim experience:
Mouse Usage: set mouse+=a
turns mouse usage on in all vim modes (normal, visual, insert, and command-line). This allows you to use the mouse for various actions like selecting text, resizing windows, and scrolling.
Line Numbers: set number
displays absolute line numbers, while set relativenumber
makes navigation easier by showing relative numbers.
Cursor Highlighting: set cursorline
makes it easy to see where your cursor is.
Indentation Control: tabstop
, shiftwidth
, and expandtab
ensure consistent spacing and indentation.
Autoindent: Helps maintain indentation consistency when writing code.
Text Wrapping: set nowrap
prevents long lines from wrapping, keeping code readable.
Case Handling: ignorecase
makes searches case-insensitive, while smartcase
ensures uppercase queries remain case-sensitive.
Incremental Search: incsearch
highlights matches as you type, providing instant feedback.
Highlighting Matches: hlsearch
ensures all found results are highlighted for better visibility.
Leader Key Shortcuts: These mappings allow quick execution of common tasks using a single key combination.
<leader>w
saves the file.
<leader>q
quits Vim.
<leader>x
saves and quits.
<leader>n
and <leader>p
navigate buffers efficiently.
In Vim, the leader key is a customizable key that serves as a prefix for user-defined shortcuts, making commands faster and more intuitive. By default, the leader key is set to \
(backslash), but it can be changed to another key, such as the comma or space, to improve usability.
Simplifies Commands: Instead of typing long Vim commands, you can assign shortcuts to them.
Improves Speed: Reduces keystrokes for common actions.
Personalized Workflow: Tailor Vim to your habits and needs.
You can redefine the leader key in your .vimrc
file:
Once defined, the leader key can be used to create custom shortcuts:
This means pressing ,
followed by w
will save the file instead of typing :w<CR>
manually.
Vim plugins are add-ons that extend Vim’s functionality, providing features that aren’t available by default. They can enhance workflow, improve navigation, add syntax highlighting, and integrate with external tools. By leveraging plugins, Vim transforms into a highly customizable editor tailored to individual needs.
Boost Efficiency: Automate repetitive tasks and improve editing speed.
Enhance Navigation: Plugins like FZF provide powerful search capabilities.
Improve UI & Aesthetics: Status bar enhancements (e.g., Vim-Airline) make Vim more user-friendly.
Extend Language Support: Syntax highlighting and auto-completion for various programming languages.
File Management: Plugins like NERDTree simplify working with directories.
Installing and managing Vim plugins can be done using plugin managers or native package management.
Popular plugin managers include:
vim-plug – Simple and efficient.
Vundle – Handles dependencies well.
Pathogen – Organizes plugins neatly.
Run this command to install vim-plug:
Edit your .vimrc
file to add the following lines:
Plugin Management: plug#begin
and plug#end
set up plugins using Vim-Plug.
NERDTree: Provides a file explorer.
Vim-Airline: Enhances the status bar with useful information.
Vim-Surround: Allows easy manipulation of surrounding characters.
FZF: Adds powerful fuzzy search capabilities.
Then, install plugins in Vim with:
Update plugins: :PlugUpdate
Remove unused plugins: :PlugClean
Check plugin status: :PlugStatus
Vim 8 introduced native package management:
Create a plugin directory: mkdir -p ~/.vim/pack/plugins/start
Clone a plugin:
Restart Vim; plugins load automatically.
Using a plugin manager is easier and more scalable, but native management works well for minimal setups.
Status Line Customization: Displays the file name, type, modification status, and cursor position.
These configurations optimize Vim for usability, efficiency, and a better coding experience.
Learn vim: vimtutor
vim plugins: fuzzy finder plugin ctrlp /// surround.vim
If you like this content and would like to see more, please consider buying me a coffee!