Editing Files Flashcards
How do I load a file in Vim for editing?
:e
1. The simplest is to use the “:e” (:edit) command:
:e
- :n or :args also work.
How do I save the current file in another name (save as) and edit a new file?
:saveas
To save the current file in another name:
:saveas
Alternatively, you can also use the following commands:
:w :edit #
You can also use the “:file” command, followed by “:w” command:
:file :w
How do I change the current directory to the directory of the current file?
:cd %:p:h
To automatically change the current directory to the directory of the
current file, simply set the option
:set autochdir
How do I edit a file whose name is under the cursor?
gf
Open a new tab page that contains the
file name under the cursor.
CTRL-W gf
How do I reload/re-edit the current file?
:edit
You can use the “:edit” command, without specifying a file name, to reload
the current file. If you have made modifications to the file, you can use
“:edit!” to force the reload of the current file (you will lose your
modifications, but depending on your ‘undoreload’ settings, those
changes might be saved into the undo history).
How do I open a file in read-only mode?
:view filename
How do I open multiple files at once from within Vim?
:next f1.txt f2.txt
:next *.c
After loading the files, you can use the “:next” and “:prev” command to switch between the files.
Difference between args, buffers, tabs and windows
Make a difference between args, buffers, tabs and windows. They are all
different things in VIM.
args is a list of arguments. Buffers are place to edit text, almost
always attached to a file but not necessarily. Window is a place for a
buffer and tab is set of windows, better name would be ‘layout’.
Open several files in Vim, with each file in a separate
window/tab.
$ vim -o3 f1.txt f2.txt f3.txt
Will open the files f1.txt, f2.txt and f3.txt in three separate horizontally split Vim windows.
$ vim -p f1.txt f2.txt f3.txt
Will open the files f1.txt, f2.txt and f3.txt in three separate tab windows.
How do I browse/explore directories from within Vim?
Start the file explorer using one of the following commands:
:Explore
From the file explorer, you can browse through directories, rename, delete and edit files.
How do I configure Vim to auto-save a modified buffer when switching to another buffer?
:set autowrite
Ways other than :Explore for browsing/exploring directories from within Vim
\:e :Explore :Sexplore :Vexplore :Texplore
From the file explorer, you can browse through directories, rename, delete
and edit files.
8.3. How do I replace the buffer in the current window with a blank buffer?
:enew