Useful Linux commands: How to use vi editor

man page: man vi

vi is a shell based editor which is available on nearly every linux installation. It's very useful to have a basic understanding how it works because sometimes it is the only option to change something. This post describes the basics plus some additional hints which I find useful.

Checkout also the general overview post: Useful Linux commands

Summary

CommandExplanation
vi filename.txtOpen filename.txt in the editor, can also be used to create new file with that name
:qquit without saving
:q!quit without saving and do not save changed (if any)
:wqquit with saving
:wq!quit with saving (although it is maybe a read only file)
igo into edit mode
Escleave mode
dddelete line where the cursor is positioned
Gjump to last line in file
ggjump to first line in file

Start and quit edit session

To open a file with vi use

vi filename.txt

In this example we use a docker-compose.yaml

version: '3'
services:
  web:
    build: btapp
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt

To end the edit session press Escape and type :q (quit). The Escape is needed when you are edit mode. I got used to press it always first so that i do not have to think about in which mode I am.

version: '3'
services:
  web:
    build: btapp
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt
~                                                                               
~                                                                               
~                                                                                                                                                                                                                            
:q

When something was changed vi will warn you with the message

E37: No write since last change (add ! to override)

To quit without saving just enter :q!

To save your changes type :wq (write quit). Sometimes you change a file which is marked as read only, in that case you can still save the changes with :wq!

Modify file content

To modify the file move the cursor on a line and press i. Now you can change the file as usual and move cursor with arrows.

You can also paste content via terminal on cursor position.

To leave the edit mode just press escape and you can leave with or without saving (see above).

To delete a full line you can press dd when the cursor is in the correct line in non edit mode.

Find and replace

vi offers powerful search capabilities via commands. To enter search mode type Esc and provide /{searchString} to jump to the next occurrence of that string.

version: '3'
services:
  web:
    build: btapp
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"  # <- cursor jumps here
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt
~                                                                               
~                                                                               
~                                                                                                                                                                                                                            
/443

You can also search the whole file directly for all occurrences of your search string with an additional :g in front.

version: '3'
services:
  web:
    build: btapp
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt
~                                                                               
~                                                                               
~                                                                                                                                                                                                                            
:g/btapp
    build: btapp
    container_name: btapp
      - ./btapp/log:/log
      - ./btapp/crt:/crt
Press ENTER or type command to continue

The search command can also deal with regex search patterns. In the next example we search for all lines where character number 11 is a blank.

version: '3'
services:
  web:
    build: btapp
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt
~                                                                               
~                                                                               
~            
:g/^.\{10}\s
    build: btapp
Press ENTER or type command to continue                                                   

Finally we can also delete all the lines where our regex matches by adding directly /d at the end.

version: '3'
services:
  web:
    build: btapp
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt
~                                                                               
~                                                                               
~            
:g/^.\{10}\s/d

which gives:

version: '3'
services:
  web:
    network_mode: host
    container_name: btapp
    environment:
      JAVA_OPTS: "-XX:+UseContainerSupport -XX:+UseG1GC -XX:+PrintGC -Xms1024m -Xmx1024m"
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - ./btapp/log:/log
      - ./btapp/crt:/crt
~
~                                                                               
~                                                                               
~     

Problem: vi adds characters on cursor move

On some systems you could face the issue that cursor movement with arrow keys in edit mode causes vi to add characters A,B,C or D. To fix that add a file in the home directory of your user like this:

user1@pi:~ $ cat .vimrc 
set nocompatible

Leave a Reply

Your email address will not be published. Required fields are marked *