Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to edit your files. I prefer using vim editor.
After doing some experiments and knowing some important commands, I though to share with you. So i have written few commands point wise which may be useful for you while editing multiple files using vim (not GVIM, although these commands are valid there too.)
Tested on Linux box under vim v6.3 and vim v7.1
Open multiple files in vim altogether
Vim file1 file2 file3 ….
Vim dirPath/pattern for files like vim perlfiles/*.pl open multiple files in vim
It will load in buffer simultaneously so that you can go back and forth to navigate files
Open files in vim one by one
After opening it you can type :e file2 . This command will be used only to open one file in buffer at one time. use of :e command in vim
If you try to open more it will throw error. For ex: :e file2 file3 error while using :e in vim for multiple files
It will not load instantly in buffer.
You need to load it manually by browsing that file for the first time. Use command :b filename(now it will be loaded in buffer) ex: :b file2
In vim v7 we can use
:tabe filename too (to see filename in tab)
:b filename (tab completion will not work in earlier version. In Vim7 it will work.)
set wildmenu in your .vimrc profile to use enhanced tab completion while navigating files through buffer.
To know how many files have been opened and you are currently in which files
use command :ls
Or : args args command in vim
or :ar to list file namesar command in vim
% refers to the file currently visible and # refers to the alternate file. (When you are using: ls to list files in buffer). You can easily toggle between these two files by pressing <ctrl>-shift-6:ls command in multiple files -vim
To Navigate files use following options:
use :bn for next file in buffer :bp for previous file in buffer.Menu command in vim :menu
Use :bn or :nb to browse nth file (n being a positive integer). :bn or :bnext is just an alias. You can use any one of them. Ex: 2b or b2 would open file which is at second buffer
:e filename - Edit a file in a new buffer
:bnext (or :bn) - go to next buffer
:bprev (of :bp) - go to previous buffer
:bd - delete a buffer (close a file)
:sp filename - Open a file in a new buffer and split window
ctrl+ws - Split windows (my vim gets hung, don’t know why). If you find the reason please let meknow. Ctrl+s doesn’t work in my vim editor under putty.
ctrl+ww - switch between windows
ctrl+wq - Quit a window
ctrl+wv - Split windows vertically
You can use :n also for browsing next file and :prev for previous file
By default vim will not allow you to switch to another buffer unless and until you save the current buffer. It will throw an error message like this: "E37: No write since last change (add ! to override) error on unsaved files in buffer -vim"
So to avoid this just write set hidden in your .vimrc profile so that it will let you switch buffers even if you have unsaved changes in the one you are leaving. Use this option if you know what changes you have made. (Be careful).
You can use tab for multiple files in vim by using tabe filename (but only one file name will be allowed at one time). Then browse opened files in tab using gt (for next file) and gT (for previous file). Even if file is not saved you can move from one tab to other tab.
You can split your screen by entering the number with command sb instead of just b. ex: 2sb means split the current window with the second buffer at the top horizontally. And ctrl+w o will bring back into single file edit mode
Use :ctrl+d to see the list of all available option under vim.
Use :menu to see available menu to use.
I am still experimenting vim features. If I will come to know anything else apart from these features, I will update this post with latest stuff.
If you know anything related to this topic or if you find any problem while using those commands, please revert here as comment with detailed issues.
Source: Alien Coders