How do I copy the current file to clipboard? I am not looking to copy the file contents itself. I want to copy the file and attach it in a different application.
An example of my use case
Open file using FzF
Make edits and save
Shortcut to copy the actual file to clipboard.
Attach it to Outlook or in Teams.
How do I achieve #3?
Thanks!
I achieved this in gVim using:
:noremap cf :silent !powershell Set-Clipboard -Path %
Related
For example, I open a file which includes
FATAL ERROR: Simulation failed. Please read /path/to/the/error/file.error
What I want to do is copy the path of the error file, which I've already done that by "v" into the visual mode and "y" to copy it, and open that file by splitting the window by ":sp /path/to/the/error/file.error".
My problem is: how to paste the copied path in command mode?
Vim also has a feature to open the filename under the cursor directly.
If you want to open it in a new split, then you can use Ctrl+W, f (or the equivalent
Ctrl+W, Ctrl+F).
See :help CTRL-W_f for more details.
The mapping to open the file under the cursor in the current window (not in a new split) is gf.
In command mode, use Ctrl-R, which is paste register, then " to paste the last yanked thing from the unnamed register.
to see your current registers, execute the following command :reg
I use the following command to copy the content of a text file (located in remote server) opened with VIM (Linux OS):
gg v shift+g
The text is selected and greyd out as seen in the screenshot
But I don't know how to copy it in clipboard in order to paste it later in another vim file (in my host machine)
If I understand you right, you are going in the wrong way.
I guess you opened the remote file via ssh. You cannot copy to your local clipboard directly over a remote ssh.
I would suggest that, you open the file from your local vim, via :e scp://server//path/file or open it in a split window in your local vim. Then you are free to yank/copy content from your remote file to your local vim file(s).
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.
Whenever I use the :sav command, it saves the file with a new name and opens the new file open in Vim.
Is it possible to save the file with a new name but keep the original one open for editing?
Use the :w command with a filename:
:w other_filename
Thanks for the answers. Now I know that there are two ways of "SAVE AS" in Vim.
Assumed that I'm editing hello.txt.
:w world.txt will write hello.txt's content to the file world.txt while keeping hello.txt as the opened buffer in vim.
:sav world.txt will first write hello.txt's content to the file world.txt, then close buffer hello.txt, finally open world.txt as the current buffer.
After save new file press
Ctrl-6
This is shortcut to alternate file
The following command will create a copy in a new window. So you can continue see both original file and the new file.
:w {newfilename} | sp #
how to copy from other file with ctrl-c and in vi editor with p yank (paste), i have no idea!!
thinks
You can also do: :r filename
This will pull in the file. It can also be used for output from a command:
:r! grep some_text file
:r! which perl
The other way is to use buffers.
:e other_file.txt
Once you yank, you can :bn to switch to the other buffer and paste
If you are just concerned about pasting, ctrl-v or shift+insert also work to paste the contents of the clipboard.
Once you have yanked (copied) text in vi, you can type <ESC>:e filename to open another file for editing. Your yank buffer will still be the same, letting you paste into the other file. You must be copying text from one file in vi into another file in vi.
If you want to paste text from outside of vi, you need to setup your terminal and vi specially to allow that.
You can't. The yank buffer is private to vim, not shared with the system clipboard.