Can you use vi with multiple file names specified on command line? - vim

Is it possible to specify multiple file names on the command line when starting vi?

Yep just use :n to go to the next file in the list, and :N to go to the previous.

You can open multiple files by using globbing E.g., vi *.html will open all the HTML files in your current directory. And, as Alex says in his answer, you can navigate back and forth through the files with :n and :N.
If you want to open multiple files at the same time, you can also use the split command.
Open the first file as usual, then, then use the command :split yourfile.ext
You should now see both files at the same time in a split-screen view. You can do this with more than 2 files (but I'm not sure what the limit is).
Now, you can navigate between the windows with ctrl-w and the arrow keys. So, if you're in the bottom pane, and you want your cursor in the upper pane, you'd first press ctrl-w, then press the up-arrow key.
Also, you can resize one of the panes by adding or subtracting rows/lines in that view. So if you're in the upper pane and you want it to be 5 lines larger, you'd press 5, then ctrl-w, then +. Same for reducing with the - key.
I'm sure that there are many other commands you can use, but these are the ones I use.
Good luck!

Also convenient:
vim -p file1 file2 file3
That will open up vim with tabs containing each file specified. You can jump between files with
gt
and
gT
If you do
:set mouse=a
you can also click on the tabs to open or drag them (although hardcore vim users would frown on this :) )

Related

Yank text from VIM editor from one session to another session

I use Putty to connect to a server and I use 2 sessions, because I want to compare 2 .sh files and I find it easier to have both files on different windows.
I am using VIM as a text editor and want to yank a line from the file of the first session to the file of the second session.
I am using V"+y to yank and then p to paste, but it only works if I close the file in the current session and open the other file in the same session.
Is it even possible to yank text from one session and paste it in another?
You can have two windows in one single Vim "session":
# two windows stacked vertically
$ vim -o file1 file2
# two windows stacked horizontally
$ vim -O file1 file2
And you can even diff them:
$ vim -d file1 file2
So it seems to me that your initial goal, as described, doesn't warrant the use of two separate Vim "sessions" at all.
See :help -o, :help -O, :help diff.
To yank between two concurrent Vim "sessions" or one Vim "session" and another program, the bare minimum you need is a clipboard-enabled Vim but it is not clear what you call "session" (is it a Vim session or a shell session?) so I doubt that it will be enough in your case.
It turned out that I had mouse mode enabled and when I turn it off I can simply Ctrl + C the needed section and add it to the other session. Didn't know the mouse mode makes such a difference.

VIM: How to open another file from same location as an already open file

Let's say I am in my home directory. I open a file that is present in some deep nested directory structure:
vim /some/really/long/path/file.txt
Now, within vim, I want to start a vertical split with another file from that same long location (but I don't want to change the current directory to that path, I still want to be in the home directory).
A layman (like me) will do something like:
:vsp /some/really/long/path/file2.txt
But I want to find out from all you VIM geniuses out there, is there an easier way to do this?
On the same note, if I had multiple files already open residing in different paths, can VIM automatically assign some sort of internal variables to all the locations? And then when I want to start a new VSP or SP with one of the files from one of those locations, I simply use those internal variables?
Try this:
:vs %:p:h/otherfile
as %:p:h gives you the path of the current file.
See :help %:p for more info.
Edit another file in the same directory:
:vs %<Tab><C-w><C-w><C-w>file2<Tab>
With a mapping:
nnoremap <key> :vs <C-R>=expand('%:p:h')<CR>
If you like more navigation than typing the commands, a convenient option could be the use of the embedded Explore command, which opens a window with the list files in the directory of the file in current buffer. Then you can navigate with the cursors and press v when over the file you want to open in a vertical split.
A convenient way is to map the Explore command, like:
" open embedded file system navigator
map <leader>\ :Explore<cr>
Press <leader>\ and navigate to the file, then press v.
With some versions of vim, the Explore window stays open after pressing v -- in that case, if you want to avoid extra burden to close the Explore window, you can do first the vertical split with :vsp, invoke :Expore and open the desired file by pressing Enter.

vim tab completion for directories

In vim using a command that expects a file I can use tab to cycle through the files in a directory.
If it is currently showing a directory in the "cycle", is there a way to make it so that the next time I tab it will instead cycle through the currently shown directory? What I am currently doing is just pressing space and then backspace but is there a better way to do this?
For example, if I type
:e ~
then when I press tab I will cycle through the directories in my home directory, e.g. several presses of tab may give me
:e ~/Desktop/
:e ~/Documents/
:e ~/Downloads/
Now for example I may actually want to open a file in ~/Documents, so I'm wondering if there's a way to make it start cycling from ~/Documents/ instead of ~/
Thanks!
Simply hit Down or / when you've reached ~/Documents.
Note that using / will result in ~/Documents//, but completion will still work (i.e show items in ~/Documents).
Edit
You can also use Ctrl - E, which doesn't add the trailing / (and might be easier to type)

How to switch to previous window in another tabpage quickly?

I really like the Ctrl-O and Ctrl-I to jump backward and forward.
If I open fileA, then use :tabe fileB to open another file in a new tabpage.
In the 2nd tabpage, I type Ctrl-O to jump back to the previous location.
Vim simply shows fileA in current window instead of switching to the 1st tabpage.
Now I have two tabpages display the same files. It is not I want.
I can use :drop fileA, but I need to provide full name. It's not good enough.
Is there some good way to switch between windows cross tabpages?
Yes, the switchbuf setting:
set switchbuf=useopen,usetab
I know you know where to get more info on the possible values. ;-)
By the way, along with set hidden, this is the key to using buffers and windows and tabs efficiently in Vim.

How to clear the line number in Vim when copying?

I copy some code from one part of one file to another part in vim, I find that, there are line numbers in each line and the format is gone, how to set correct format as origin ?
like this:
40 root /opt/release/current/public;
67 41 passenger_enabled on;
68 42
If you have line numbers, I'm quite sure you are not using Vim's yank/put operations (these will never copy the linenumbers, foldcolumn, icons etc) because in terms of the edit buffer, they don't exist.
My guess is you are working in a terminal emulator and using the mouse to copy stuff to the clipboard, which possibly selects the 'extraneous' room of the screen (including virtual spaces at the end, line numbers, fold markers etc)
You might have luck setting
:se mouse+=a
in order to get the behaviour of the mouse like you expect it. Otherwise, do the selection with V<movement>...y (y for yank, which corresponds to 'copy')
Then on the destination use p (put at cursor), or P (put before cursor)
Let me know if that helped or you need more info
In case anyone wants a quicker way (on Linux anyways), I have noticed in vim you can hold down ctrl and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.
Steps:
ctrl and drag over area
release ctrl
copy (either keyboard shortcut or right click)
In normal mode, type :se nonu
This is the easiest way to remove the line numbers and you will be able to copy the text without the line numbers.
A permanent solution for this is to add the below code at the end of your .vimrc file located in your home directory.
se mouse+=a
By adding this you will be able to select only text and not the line numbers as shown in below image:
If you are not getting your .vimrc file in your home directory (i faced this problem), type the command :scriptnames in vi editor, it will display the location of your .vimrc file. Reference
On Mac: I found out that you can select the desired area with Option+Command and copy paste it to another editor.
All the previously entered solution are very good one. However, sometimes your are using vim on a remote server (so you cant copy to your clipboard using "+y). Often terminals support copy paste operation.
I use vim to output visual selection to a new shell where I can copy text using terminal feature:
:'<,'>w ! bash -c cat
Then I can easily copy the output.
Same pattern for pasting in vim:
:r ! bash -c cat
Then I paste and send EOF to cat using Ctrl+d. This method also avoid reindenting the text you paste (Note: you can disable automatic indentation using :set pi!).
Have a look at the pastetoggle option sometimes set to F11.
As an alternative you could always write the section you want to copy into a temporary file
(ma, goto end line then use :'a,.w tempfile) then read it into the second file.
For further investigation you might want to look at the autoindent option.
On Windows VIM GUI: :set nu and then hold down Ctrl-Shift while
highlighting the desired text with the mouse. This yanks the line numbers
into the buffer.
On Windows using Putty, I have noticed in vim you can hold down ALT and drag over the region you want to copy and you'll avoid the line numbers and select the part you want.
Steps:
Press and Hold ALT
Drag over area
Release ALT
Copy using CTRL+Shift+C (or right click if that is turned on)
Variations:
The specific key may be different based on your specific set up.
If ALT does not work, try the following keys instead:
CTRL
CTRL+Shift
ALT+Shift
Note:
If anyone knows of any other key combinations that have worked for them let me know and I will update this answer with them.
I mapped the below command to a key.
It strips the whitespace around the copied line numbers.
It ignores the line text and any blank lines behind.
:1,$s/^\s*[0-9]\+\s//\|1,$s/^\s*[0-9]\+\n/\r/<cr>
You can also consider using sed and pbcopy to copy the lines to a clipboard, where you can paste to another terminal or apps outside of vim.
sed -n <line start #>,<line end #>p <file name> | pbcopy
simplest way just: $ cat "your file"
and then just copy it from your terminal without numbers

Resources