ubuntu, updated

Vim: working with several files

Open other files from inside vim, switch between them, split the screen, compare files and browse folders.

Open another file

WhatCommand
Open a file (replaces the current view):e /etc/hosts
Tab completion for paths:e /etc/ne then Tab
Reload the file from disk (discard changes):e!
Browse a folder and pick a file:e . or :e /etc/nginx/ (Enter opens, - goes up)
Open the file name under the cursorgf
Back to the previous fileCtrl+^ (or :e #)
Insert another file’s content here:r /path/file
Insert a command’s output here:r !date

:e refuses if the current file has unsaved changes. Save with :w first, or add set hidden to your settings.

Several files at once

vim file1.conf file2.conf      # open both
vim -O a.conf b.conf           # side by side
vim -o a.conf b.conf           # one above the other

Buffers: the open files

Every open file is a buffer. They stay open in the background.

WhatCommand
List open files:ls
Next / previous file:bn / :bp
Go to buffer 3:b3
Go to a buffer by name:b host then Tab
Close the current buffer:bd

Split windows

WhatKeys
Split horizontally (+ open file):sp file
Split vertically:vsp file
Move between windowsCtrl+W, then h j k l (or Ctrl+W w to cycle)
Close the current window:q
Close all other windows:only
Make windows equal sizeCtrl+W =

Tabs

WhatCommand
Open a file in a new tab:tabe file
Next / previous tabgt / gT
Open files in tabs from the shellvim -p a.conf b.conf

Compare two files

vimdiff /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
KeysDoes
]c / [cnext / previous difference
dotake the other file’s version (“diff obtain”)
dppush this version to the other file (“diff put”)
:qaquit all

Save and quit all

WhatCommand
Save all files:wa
Quit all:qa
Save all and quit:wqa
Quit all, discard changes:qa!
§ home
~/knubertus/home/
1,1 Top