keepcalculator

06 Numbers and Powers

Vim Advanced Editing on a VPS: A How-To

Card No. 06·39721 · Updated · Standard formula, shown below

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 vim on Debian/Ubuntu)
  • A basic understanding of Vim modes and commands

Step-by-Step

  1. Open a file in Vim – Run vim /path/to/file. For a new file, just specify a new path. Use vim -R file for read-only mode.
  2. Master motion commands – Beyond h/j/k/l, use w (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: 5w moves five words.
  3. Use visual mode for precise selection – Press v for character-wise, V for line-wise, and Ctrl+v for block-wise selection. Then use motions to expand the selection. After selecting, you can delete, yank (copy), or change.
  4. Leverage the quickfix list for code errors – If you’re using a compiler or linter that outputs errors in Vim’s error format, run :make or :lmake. Vim populates the quickfix list. Navigate with :cnext, :cprev, and :copen to see the list. This is invaluable for debugging code on the server.
  5. Work with multiple files using buffers – Open multiple files with vim file1 file2. Use :ls to list buffers, :bnext/:bprev to switch, and :bd to delete a buffer. This avoids opening new sessions for each file.
  6. Split windows for side-by-side editing – Use :split or :vsplit to divide the screen. Navigate between windows with Ctrl+w followed by a direction key (h/j/k/l). Resize with Ctrl+w +/- or Ctrl+w >/<.
  7. Use tabs for separate workspaces – Tabs are like virtual desktops. Open a new tab with :tabnew file, switch with gt or :tabnext, and close with :tabclose. Useful for grouping related files.
  8. Record macros for repetitive edits – Press q followed by a register letter (e.g., qa) to start recording. Perform your edits, then press q again to stop. Replay with @a. For example, to add a semicolon to the end of every line, record a macro that does A; then j, and replay it over a visual selection.
  9. Use registers to store text snippets – Yank or delete text into a named register with "ay or "ad. Paste with "ap. This is great for moving code blocks between files.
  10. Search and replace with regex – Use :%s/old/new/g for global replace. Add c at the end to confirm each change. Use capture groups: :%s/\(foo\)\(bar\)/\2\1/ to swap. Use \v for very magic mode to reduce escaping.
  11. Customize Vim with your .vimrc – Create ~/.vimrc to 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), and set ignorecase / set smartcase for case-insensitive search unless you use uppercase.
  12. 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).
  13. Use netrw for file browsing – Vim’s built-in file explorer. Open with :Ex or :Explore. Navigate with standard motions, press % to create a new file, d to create a directory, and R to rename.
  14. 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 :wq or ZZ instead of just :q.
  • Mixing up d (delete) and x (delete character under cursor). dd deletes a line, dw deletes a word.
  • Using Ctrl+z to suspend Vim instead of exiting—it leaves Vim running in the background. Use :q or :wq.
  • Assuming :w saves and exits; it only saves. Use :wq or :x to save and quit.
  • Not using set hidden in .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+n or Ctrl+p in insert mode for keyword autocompletion.
  • Use gg=G to auto-indent the entire file (if your filetype has indent settings).
  • Check your Vim version with vim --version; some features like +clipboard may be missing on minimal VPS installs.
  • Use :set paste before 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) or sudo 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_ansi or use h/j/k/l instead. Also ensure your SSH client is a proper terminal emulator.
  • Backspace doesn’t work – Add set backspace=indent,eol,start to 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 errorformat or 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

  1. How do I exit Vim? – Press Esc to enter normal mode, then type :q to quit (if no changes), :q! to quit discarding changes, or :wq to save and quit.
  2. 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.
  3. 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 "*y to yank to the system clipboard if Vim supports +clipboard.
  4. 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.
  5. 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.
  6. How can I make Vim my default editor for git and other tools? – Set export EDITOR=vim in your shell profile (e.g., ~/.bashrc). For git specifically, run git 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

The whole Numbers and Powers drawer →