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
| What | Command |
|---|
| 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 cursor | gf |
| Back to the previous file | Ctrl+^ (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.
| What | Command |
|---|
| 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
| What | Keys |
|---|
| Split horizontally (+ open file) | :sp file |
| Split vertically | :vsp file |
| Move between windows | Ctrl+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 size | Ctrl+W = |
Tabs
| What | Command |
|---|
| Open a file in a new tab | :tabe file |
| Next / previous tab | gt / gT |
| Open files in tabs from the shell | vim -p a.conf b.conf |
Compare two files
vimdiff /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
| Keys | Does |
|---|
]c / [c | next / previous difference |
do | take the other file’s version (“diff obtain”) |
dp | push this version to the other file (“diff put”) |
:qa | quit all |
Save and quit all
| What | Command |
|---|
| Save all files | :wa |
| Quit all | :qa |
| Save all and quit | :wqa |
| Quit all, discard changes | :qa! |