Skip to main content

vi tips and tricks

    Command Summary

*    find cursor under word
STARTING vi

     vi filename    edit a file named "filename"
     vi newfile     create a new file named "newfile"
ENTERING TEXT

     i            insert text left of cursor
     a            append text right of cursor
MOVING THE CURSOR

     h            left one space
     j            down one line
     k            up one line
     l            right one space
BASIC EDITING

     :line1,line2d delete between two
     x         delete character
     nx        delete n characters
     X         delete character before cursor
     dw        delete word
     ndw       delete n words
     dd        delete line
     ndd       delete n lines
     D         delete characters from cursor to end of line
     r         replace character under cursor
     cw        replace a word
     ncw       replace n words
     C         change text from cursor to end of line
     o         insert blank line below cursor
                  (ready for insertion)
     O         insert blank line above cursor
                  (ready for insertion)
     J         join succeeding line to current cursor line
     nJ        join n succeeding lines to current cursor line
     u         undo last change
     U         restore current line
MOVING AROUND IN A FILE

     w            forward word by word
     b            backward word by word
     $            to end of line
     0 (zero)     to beginning of line
     H            to top line of screen
     M            to middle line of screen
     L            to last line of screen
     G            to last line of file
     1G           to first line of file
     <Control>f   scroll forward one screen
     <Control>b   scroll backward one screen
     <Control>d   scroll down one-half screen
     <Control>u   scroll up one-half screen
     n            repeat last search in same direction
     N            repeat last search in opposite direction
CLOSING AND SAVING A FILE

     ZZ            save file and then quit
     :w            save file
     :q!            discard changes and quit file


:0
Shift G

ggVG select all



"+y
"+gP
3yl
yE


yiw     yank inner word (copy word under cursor, say "first").
viwp     select "second", then replace it with "first".
viw"0p     select "third", then replace it with "first".
Copy a line and paste it over other lines:
yy     yank current line (say "first line").
Yp dublicate line
Vp     select "second line", then replace it with "first line".
V"0p     select "third line", then replace it with "first line".

TAB
vim -p first.txt second.txtd
gt            go to next tab
gT            go to previous tab
{i}gt         go to tab in position i
Ctrl-PgDn     go to next tab
Ctrl-PgUp     go to previous tab



search
%s/search/replace/gc  


undo/redo
u    undo   
Ctrl+r    redo   
.    repeat   
navigation
gg    Goto start of file   
G    Goto end of file   
:54    Goto line 54   
80|    Goto column 80   
Ctrl+g    Show file info    including your position in the file
ga    Show character info    g8 shows UTF8 encoding
Ctrl+e    scroll up    Ctrl+x needed first for insert mode
Ctrl+y    scroll down    Ctrl+x needed first for insert mode
zt    scroll current line to top of window   
w    Goto next word    Note Ctrl+{right} in newer vims (which work also in insert mode)
b    Goto previous word    Note Ctrl+{left} in newer vims
[{    Goto previous { of current scope   
%    Goto matching #if #else,{},(),[],/* */    must be one on line
zi    toggle folds on/off


How to copy yanked text to VI command prompt :    v (visual mode) select copy area with -> (left arrow)  y (copy)      Shift ins (paste prompt)


Save as :w abc.txt



:21,32y   yanked 11 lines


higlight cursor line
:color desert
:set cursorline
:hi CursorLine term=bold cterm=bold guibg=Grey40


"*y    copy a line in a file
"*p    paste copied line an other file



Listing

To see a list of current buffers, I use:
:ls
Opening
To open a new file, I use
:e ../myFile.pl
with enhanced tab completion (put set wildmenu in your .vimrc).
Note: you can also use :find which will search a set of paths for you, but you need to customize those paths first.

Switching
To switch between all open files, I use
:b myfile
with enhanced tab completion (still set wildmenu).
Note: :b# choses the last visited file, so you can use it to switch quickly between two files.


Global command
:g/one\|two/     : list lines containing "one" or "two"
:g/^\s*$/d       : delete all blank lines
:g/green/d       : delete all lines containing "green"
:v/green/d       : delete all lines not containing "green"
:g/one/,/two/d   : not line based
:v/./.,/./-1join : compress empty lines





Multiple files
:wn           : write file and move to next (SUPER)
:bd           : remove file from buffer list (SUPER)
:sav php.html : Save current file as php.html and "move" to php.html
:sp fred.txt  : open fred.txt into a split
:e!           : return to unmodified file
:w /some/path/%:r   : save file in another directory, but with the same name
:e #          : edit alternative file
:args         : display argument list
:n            : next file in argument list
:prev         : previous file in argument list
:rew          : rewind to first file in argument list
:ls           : display buffer list
:bn           : next buffer
:bp           : previous buffer
:brew         : rewind to first buffer in buffer list
:tabe         : open new tab page (Ctrl-PgUp, Ctrl-PgDown for next/previous tab)
:tabm n       : move tab to position n (0=leftmost position)



guu     : lowercase line
gUU     : uppercase line



Where grep came from (RE being Regular Expression):
:g/RE/p

Delete lines 10 to 20 inclusive:
:10,20d
or with marks a and b:
:'a,'bd

Delete lines that contain pattern:
:g/pattern/d

Delete all empty lines:
:g/^$/d

Delete lines in range that contain pattern:
:20,30/pattern/d
or with marks a and b:
:'a,'b/pattern/d

Substitute all lines for first occurance of pattern:
:%s/pattern/new/
:1,$s/pattern/new/

Substitute all lines for pattern globally (more than once on the line):
:%s/pattern/new/g
:1,$s/pattern/new/g

Find all lines containing pattern and then append -new to the end of each line:
:%s/\(.*pattern.*\)/\1-new/g

Substitute range:
:20,30s/pattern/new/g
with marks a and b:
:'a,'bs/pattern/new/g

Swap two patterns on a line:
:s/\(pattern1\)\(pattern2\)/\2\1/

Capitalize the first lowercase character on a line:
:s/\([a-z]\)/\u\1/
more concisely:
:s/[a-z]/\u&/

Capitalize all lowercase characters on a line:
:s/\([a-z]\)/\u\1/g
more concisely:
:s/[a-z]/\u&/g

Capitalize all characters on a line:
:s/\(.*\)/\U\1\E/

Capitalize the first character of all words on a line:
:s/\<[a-z]/\u&/g

Uncapitalize the first character of all words on a line:
:s/\<[A-Z]/\l&/g

Change case of character under cursor:
~

Change case of all characters on line:
g~~

Change case of remaining word from cursor:
g~w

Increment the number under the cursor:
<Ctrl-A>

Decrement the number under the cursor:
<Ctrl-X>

redraw:
<Ctrl-L>

Turn on line numbering:
:set nu
Turn it off:
:set nonu

Number lines (filter the file through a unix command and replace with output):
:%!cat -n

Sort lines:
:%!sort

Sort on column #69:
:sort /.*\%69v/

Sort and uniq:
:%!sort -u

Read output of command into buffer:
:r !ls -l

Refresh file from version on disk:
:e!

Open a new window:
<Ctrl-W>n

Open a new window with the same file (split):
<Ctrl-W>s

Split window vertically:
<Ctrl-W>v

Close current window:
<Ctrl-W>c
:q

Make current window the only window:
<Ctrl-W>o

Cycle to next window:
<Ctrl-W>w

Move to window below current window:
<Ctrl-W>j

Move to window above current window:
<Ctrl-W>k

Move to window left of current window:
<Ctrl-W>h

Move to window right of current window:
<Ctrl-W>l

Set textwidth for automatic line-wrapping as you type:
:set textwidth=80

Turn on syntax highlighting
:syn on
Turn it off:
:syn off

Force the filetype for syntax highlighting:
:set filetype=python
:set filetype=c
:set filetype=php

Use lighter coloring scheme for a dark background:
:set background=dark


Htmlize a file using the current syntax highlighting:
:so $VIMRUNTIME/syntax/2html.vim

Or, htmlize from a command prompt:
in 2html.sh put:

#!/bin/sh
vim -n -c ':so $VIMRUNTIME/syntax/2html.vim' -c ':wqa' $1 > /dev/null 2> /dev/null

Now just run:  shell> 2html.sh foo.py

Comments

Popular posts from this blog

Offline AI Coding in VSCode: Continue + Ollama Complete Setup Guide

Want GitHub Copilot-level code assistance — but fully offline, air-gapped, and private? This guide walks you through setting up Continue (a VSCode extension) with Ollama (a local model runner) so you get chat, autocomplete, and inline editing powered entirely by models running on your own machine. No internet required at runtime. Table of Contents What Are Continue and Ollama? Prerequisites & Hardware Requirements Choosing the Right Models Step 1 — Installing Ollama & Pulling Models Step 2 — Installing Continue in VSCode (Offline) Step 3 — Configuring config.yaml Step 4 — Disabling Telemetry Step 5 — Usage & Shortcuts Troubleshooting What Are Continue and Ollama? Continue is an open-source VSCode (and JetBrains) extension that adds AI-powered coding features directly into your editor — think chat, inline edits, and tab autocomplete. By default it can connect to Claude, GPT-4, or any API. But it also supports local providers , which is exactly what we want here. Ollama i...

Local LLM on Ubuntu (CPU-Only): Ollama + Qwen Coder + Open WebUI + RAG Setup

Local LLM on Ubuntu (CPU-Only): Ollama + Qwen Coder + Open WebUI + RAG Run a private, high-performance internal AI coding assistant on your own hardware—no GPU required. Feb 13, 2026 Qwen 2.5 Coder RAG Enabled Enterprise Ready Keeping your code and documents on your own infrastructure is the gold standard for enterprise security. By combining Ollama , Qwen Coder , and Open WebUI , we can build a production-grade AI assistant that respects your privacy. Environment: CPU-only Ubuntu server (e.g., i9/Threadripper) with 64GB+ RAM for smooth 14B model inference. 1. System Preparation First, update your local environment and install necessary dependencies: sudo apt update && sudo apt upgrade -y sudo apt install -y curl git ca-certificates lscpu 2. Ollama & Model Tuning Install Ollama and optimize the configuration for CPU-only inference by setting the thread count: ...

Defining tomcat and mysql server Amazon EC2 instance

-- Create new amazon ec2 instance http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EC2_GetStarted.html -- Give permisson to pem file chmod 400 mycertificate.pem -- Connect to EC2 instance ssh -i mycertificate.pem ubuntu@ec2-51-51-51-51.eu-west-1.compute.amazonaws.com -- Installation Oracle Java 1.8 $ wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u92-b14/jdk-8u92-linux-x64.tar.gz $ tar -xzvf jdk-8u92-linux-x64.tar.gz $ sudo mkdir -p /usr/java/ $ sudo mv jdk1.8.0_92/ /usr/java/ $ sudo vim  /etc/profile         JAVA_HOME=/usr/java/jdk1.8.0_92         PATH=$PATH:$HOME/bin:$JAVA_HOME/bin         JRE_HOME=/usr/java/jdk1.8.0_92/jre         PATH=$PATH:$HOME/bin:$JRE_HOME/bin         export JAVA_HOME...