How do I paste non-ASCII characters into vim? - vim

My terminal emulator is configured for Unicode character encoding and my .vimrc contains the line
set encoding=utf-8
but when I try pasting the word "café" into vim, it comes out as "café".
I can make an "é" in vim by typing Ctrl-vu followed by the 4-character code point (00e9) but I'd like the ability to paste it in.

The solution was to uncheck the "Escape non-ASCII input" checkbox in the Advanced tab of the Terminal.app settings.

Instead of copying the character from the clipboard and pasting it into the text editor, you can add it to the file using vim's digraph feature.
So, with default settings, using your example, you could enter insert mode, then hit Control-k, and type e'. The result will be é.
You can see the list of defined digraphs by running
:digraphs
and general help by running
:help digraph
which also explains how to customize your mappings.
If you are truly set on pasting the characters in, Shift-Insert has always worked for me.

How to type a non ASCII code into vi
You'll have to know what your encoding is to enter the proper codes, I assume you left VI at the default.
In VI, go into insert mode, while in insert mode, press
Ctrl-v 172
That should insert the Logical Not (sideways and inverted L) character defined here:
http://www.htmlhelp.com/reference/charset/iso160-191.html

From where are you trying to paste it? If it is opera, then since opera-10.5* paste is broken in both directions. Before pasting anything you need to use xclip -o|xclip -i workaround (add -sel clip for clipboard).
What command do you use?

Related

SLES12 vim - background changes scrolling + copy paste issues

I am using gnome-terminal with SLES12 and I encounter an issue where I am scrolling down during showing file in vim, the background color is changing.
Using some exploration during the internet I got the following solution :
if &term =~ '256color'
" Disable Background Color Erase (BCE) so that color schemes
" work properly when Vim is used inside tmux and GNU screen.
set t_ut=
endif
But using this solution, it creates a new one.
When I copy paste a line from vim and paste it on other vim , the copy consider also the blank lines in as characters , and creates really long lines.
To emphasize, lets say I have the following line which contain 11 char (including the space)
the copy paste consider also the rest of the line (the blank ones) as characters.
I would like that the copy paste would stop at char 'd '
hello world
Any idea how to combine a solution for these two issues?
The two issues are completely unrelated.
When you "copy paste a line from vim and paste it on other vim", you are presumably using your terminal emulator's or system's copy/paste feature which have no idea about where what you consider as a line starts and where it ends. The terminal emulator's window is n characters wide so a line is n characters and that's all they care about.
A much better approach would be to use Vim's built-in :help y and :help p, which have the same idea of what a line is as you.
But this creates a second problem: the default Vim is generally not built with clipboard support so you can't really use yy to yank a line in one Vim and p to put it in another one. You will have to install a proper Vim for that: use your package manager for that.
Once you are set, you can yank to clipboard with "+y and put from clipboard with "+p. See :help registers for "+ and :help 'clipboard' if you would like to synchronise Vim's default register with the system clipboard.
That said, why don't you simply open those two files in a single Vim instance?

Why is vim so slow with copy/paste?

I find myself copy-pasting text between editors a lot. Often with the result of SQL outputs that are usually about 10,000+ lines. I have never had a problem doing this -- usually it takes all of a second or two to paste it in. However, when I use vim, it takes about a minute to copy-paste in all that text, this is after settings :set no paste.
Is this supposed to happen in vim or is there some sort of setting I can change so this doesn't occur? I've tried TextMate, TextEdit, VSCode, etc, and vim is the only one with this issue related to copy-paste.
I think you should use :set paste instead.
Because :set no paste paste character per character and check if whether these character is matched with some key binding, some shortcut.

Why does Vim always manage to clip off at least a couple of characters when copying and pasting?

Vim always manages to clip off at least of couple of characters when I copy something from a text file, such as a public rsa key for example.
Generally, I open a text file in my computer, highlight the text, copy it to my clipboard. I go into my terminal (with Vim already open) and then:
i (for insert)
ctrl+shift+v
Am I doing something wrong?
I've double checked, and triple checked, this, and I am absolutely sure that I highlighted all of the text that I needed to copy. Other times it works fine. There does not seem to be any consistency in regards to when it happens.
Anyone experience this?
This usually happens because something in the pasted text triggers a key mapping unexpectedly. This is what paste mode is for.
Do this before pasting:
:set paste
And turn it off after:
:set nopaste
This can be annoying to type frequently, so there is a way to map it to a key combination. I use ,p for this, and have this in my .vimrc:
set pastetoggle=,p
(Yes, I use an actual comma here, not <Leader>. That's because <Leader> won't work here, as this isn't technically a mapping.)
This is a toggle, so you can use it to turn the mode on and off. It works in both normal mode and insert mode.
You do want to turn it off after the paste. Since paste mode disables your insert mappings, you don't want to leave it on all the time.
You can read more about the paste option with :help 'paste.
The annoyance of toggling to and form paste mode can be avoided by using "+p or "*p. (You're putting the quoteplus register).
More often than not, I prefer "+P, since this leaves my cursor at the end of properly formatted put. see :help quoteplus for details. So the whole workflow is:
outside of vim: copy to clipboard
inside of vim: "+P

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

How do I repeatedly search & replace a long string of text in vim?

I'm aware of the vim replace command, which is of the form, eg:
:%s/old/new/gc
But what if either of these strings is long? How can I use something like visual selection mode, the clipboard or vim registers instead of having to type the old/new text in?
You can use q: to bring up a command-line window. This lets you use all the vim editing commands to edit the vim command line, including p to paste. So, you could copy the text into a register, paste it into the command line window, and execute it that way.
I recently discovered this feature via vimcasts.
According to the manual, you can use Ctrl+R to insert the contents of a register into the current position in the command line. The manual also claims that Ctrl+Y inserts the text highlighted with the mouse into the command line. Remember that in X11 and some other systems, you can also paste text into a program from the system clipboard using the middle mouse button or a menu command in your terminal emulator.
I think to avoid have your command line be huge you can use this to solve your issue
:%s/foo/\=#a/g
That replaces "foo" with whatever is in register a.
If you're trying to do a substitute with a long complicated search pattern, here's a good way of going about it:
Try out the search pattern using some test cases and refine it until you have the pattern you want. I find incsearch really helps, especially with complicated regular expressions.
You can then use :%s//new to replace all instances of the last searched for pattern.
If you've entered a pattern and want to copy it out of the search history, you can use q/ to bring up a command line window containing recent search patterns very similar to the q: one that contains recent command history.
On the other hand, if you're asking about how to copy and paste text into the substitute command:
I'd write the pattern out in insert mode and yank the search and replacement into two distinct registers using, say, "ay and "by and then use :%s/<C-R>a/<C-R>b/gc to do the substitute. There are lots of variations of the yank command, but this one should also work automatically when using a visual selection.
If you're copying in text from the clipboard, you can use <C-R>* to paste it's contents in insert mode.
I have the following mapping in my .vimrc
vnoremap <leader>r "ry:%s/^Rr/
So I visually select the thing I want to replace, and hit ,r, type the replacement and hit return. If I want to paste the replacement, I yank it before selecting the text to replace, and then use <C-r>" to paste it as the replacement before hitting return.
Note: to insert ^R in your .vimrc, you actually type <C-v><C-r>.

Resources