This guide walks you through advanced Vim techniques for editing code and configuration files directly on a VPS. You’ll learn to navigate faster, use buffers and tabs, leverage macros and registers, and customize Vim to your workflow—all without leaving the terminal. It assumes you have basic Vim knowledge (insert mode, normal mode, saving, quitting) and SSH access to your server.
What You’ll Need
- SSH access to a VPS (any Linux distribution)
- Vim installed (usually pre-installed; if not, install via your package manager, e.g.,
sudo apt install vimon Debian/Ubuntu) - A basic understanding of Vim modes and commands
Step-by-Step
- Open a file in Vim – Run
vim /path/to/file. For a new file, just specify a new path. Usevim -R filefor read-only mode. - Master motion commands – Beyond
h/j/k/l, usew(word),b(back),e(end of word),0(line start),$(line end),gg(file start),G(file end), and{/}to jump between paragraphs. Combine with numbers:5wmoves five words. - Use visual mode for precise selection – Press
vfor character-wise,Vfor line-wise, andCtrl+vfor block-wise selection. Then use motions to expand the selection. After selecting, you candelete,yank (copy), orchange. - Leverage the quickfix list for code errors – If you’re using a compiler or linter that outputs errors in Vim’s error format, run
:makeor:lmake. Vim populates the quickfix list. Navigate with:cnext,:cprev, and:copento see the list. This is invaluable for debugging code on the server. - Work with multiple files using buffers – Open multiple files with
vim file1 file2. Use:lsto list buffers,:bnext/:bprevto switch, and:bdto delete a buffer. This avoids opening new sessions for each file. - Split windows for side-by-side editing – Use
:splitor:vsplitto divide the screen. Navigate between windows withCtrl+wfollowed by a direction key (h/j/k/l). Resize withCtrl+w +/-orCtrl+w >/<. - Use tabs for separate workspaces – Tabs are like virtual desktops. Open a new tab with
:tabnew file, switch withgtor:tabnext, and close with:tabclose. Useful for grouping related files. - Record macros for repetitive edits – Press
qfollowed by a register letter (e.g.,qa) to start recording. Perform your edits, then pressqagain to stop. Replay with@a. For example, to add a semicolon to the end of every line, record a macro that doesA;thenj, and replay it over a visual selection. - Use registers to store text snippets – Yank or delete text into a named register with
"ayor"ad. Paste with"ap. This is great for moving code blocks between files. - Search and replace with regex – Use
:%s/old/new/gfor global replace. Addcat the end to confirm each change. Use capture groups::%s/\(foo\)\(bar\)/\2\1/to swap. Use\vfor very magic mode to reduce escaping. - Customize Vim with your .vimrc – Create
~/.vimrcto set options. Common ones:set number(line numbers),set relativenumber(relative line numbers),set tabstop=4,set shiftwidth=4,set expandtab(spaces instead of tabs),set autoindent,set hlsearch(highlight search),set incsearch(incremental search), andset ignorecase/set smartcasefor case-insensitive search unless you use uppercase. - Install plugins for advanced features – Use a plugin manager like vim-plug. Add
call plug#begin('~/.vim/plugged')to your .vimrc, then list plugins, and run:PlugInstall. Popular plugins include NERDTree (file explorer), fzf (fuzzy finder), and vim-airline (status line). - Use netrw for file browsing – Vim’s built-in file explorer. Open with
:Exor:Explore. Navigate with standard motions, press%to create a new file,dto create a directory, andRto rename. - Perform remote editing over SSH – If you have a local Vim, you can edit files on your VPS using
vim scp://user@host/path/to/file. This requires the netrw plugin (built-in). It’s a quick way to edit without a full SSH session.
Common Mistakes
- Forgetting to save before quitting: use
:wqorZZinstead of just:q. - Mixing up
d(delete) andx(delete character under cursor).dddeletes a line,dwdeletes a word. - Using
Ctrl+zto suspend Vim instead of exiting—it leaves Vim running in the background. Use:qor:wq. - Assuming
:wsaves and exits; it only saves. Use:wqor:xto save and quit. - Not using
set hiddenin .vimrc, which allows switching buffers without saving first—avoiding annoying prompts. - Overusing plugins that slow down Vim startup on a VPS with limited resources.
Tips
- Learn
ciw(change inner word) to quickly replace a word under the cursor. - Use
.to repeat the last change—great for repetitive edits. - Use
Ctrl+norCtrl+pin insert mode for keyword autocompletion. - Use
gg=Gto auto-indent the entire file (if your filetype has indent settings). - Check your Vim version with
vim --version; some features like+clipboardmay be missing on minimal VPS installs. - Use
:set pastebefore pasting large text to avoid auto-indent messing up formatting, then:set nopaste.
Troubleshooting
- Vim is not installed – Run
sudo apt update && sudo apt install vim(Debian/Ubuntu) orsudo yum install vim(CentOS/RHEL). If you get a command not found, your package manager may be different. - Arrow keys produce letters – This happens in insert mode if the terminal is not set correctly. Try
:set term=builtin_ansior useh/j/k/linstead. Also ensure your SSH client is a proper terminal emulator. - Backspace doesn’t work – Add
set backspace=indent,eol,startto your .vimrc. - Macro not working as expected – Make sure you stopped recording with
q. If the macro includes a motion that depends on cursor position, it may not replay correctly—try using relative motions or search-based navigation inside the macro. - Quickfix list is empty – Ensure your compiler/linter outputs errors in a format Vim understands. You may need to set
errorformator use a plugin like syntastic or ale. - Plugin installation fails – Check your internet connection on the VPS. If vim-plug fails, manually clone the plugin repo into
~/.vim/pack/plugins/start/.
Frequently Asked Questions
- How do I exit Vim? – Press
Escto enter normal mode, then type:qto quit (if no changes),:q!to quit discarding changes, or:wqto save and quit. - Can I use Vim to edit files on a remote server without SSH? – No, you need SSH (or another remote access method) to connect to the VPS. Once connected, you can use Vim directly.
- How do I copy text from Vim to my local machine? – If Vim is running inside a terminal, you can usually select text with the mouse and copy it with Ctrl+Shift+C (Linux) or Cmd+C (Mac). Alternatively, use
"*yto yank to the system clipboard if Vim supports+clipboard. - What’s the difference between buffers and tabs? – Buffers are open files in memory; tabs are like separate workspaces that can hold multiple buffers. You can have multiple tabs, each with its own set of windows and buffers.
- Is it safe to edit system configuration files with Vim? – Yes, but be careful. Make a backup first (
cp file file.bak). Use:w !sudo tee %to write with sudo if you forgot to open as root. - How can I make Vim my default editor for git and other tools? – Set
export EDITOR=vimin your shell profile (e.g.,~/.bashrc). For git specifically, rungit config --global core.editor vim.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors
- Mouse Moving on Its Own? Causes & Fixes