How does one copy an arbitrary block of text in Sublime Text 3's Vintage mode? - vim

I'm using Vintage mode (i.e. vi-style interface) in Sublime Text 3.
I'm trying to copy an arbitrary block of text. In regular Vim, the way I do this is:
Position the cursor at one end of the block.
Set a vi mark at that position, e.g. with mx ("set mark x here").
Position the cursor at the other end of the block.
Yank all text between the current position and mark x into the vi clipboard with y`x.
However, when I do this, it only seems to yank the current line into the clipboard, as if I had typed yy instead of y`x.
`x by itself works as normal (i.e. moves the cursor to mark x). But y`x does not.
As a result I find myself frequently using shift-arrow keys to highlight arbitrary text, which is OK, but is a mental shift I'd like to avoid.
Ideas?

Sublime's Vintage mode simply doesn't support it, unfortunately.
If you'd like a better Vim support, try the Vintageous package. It indeed supports the y`x you are looking for (just tried it).

Related

vi replace visual mode selection with clipboard

I know that this question has probably already been answered but I can't find it.
Here is what I want to do, imagine I have world in my clipboard.
And my file looks like :
Hello guys
I want to replace guys by world.
I know that :%s/guys/world/g would work but my section is actually huge.
I would like to select my selection with v (visual mode) then when I have the proper selection highlighted, paste my clipboard. I tried p it's not working.
Any help ?
I just did this on gvim 8.0.2. This post explains how to do it: https://www.reddit.com/r/vim/comments/337cxj/yankpaste_tofrom_system_clipboard/
From that post:
Firstly, execute the command
:echo has('clipboard')
If it returns 1 you're good to go, otherwise check if your distro has a more full-featured vim package (or compile it yourself)
To copy to the clipboard, enter either visual, visual-block , or visual-line mode, select your text and press the sequence (not combo):
"+y
and your text is ready to be pasted into any other program or window.
Additionally you can execute that sequence in normal mode, however its behavior is not ideal (in my opinion). It yanks the remaining characters on the current line, after the cursor plus the characters on the next line up until the column of the cursor. Decent if your cursor is at column 0, otherwise kind of annoying.
By replacing the 'y' with a 'p' you get the same behavior you would expect from just 'p' in normal mode, however pasted from the system clipboard. On a modern setup, this is probably not as useful, as we have Ctrl-Shift-v, but I can certainly think of some use-cases for this.
Have you tried the normal paste command of terminal ctrl+shift+v? It works for me after selecting content through v.

Community Wiki: "Vim: Advanced usage of the yanking mechanism"

Community Wiki
As the documentation of the yank system shows (thanks Michal), The Vim yank system seems to be more intricate then a standard clipboard. I therefore think it beneficial if vim veterans could perhaps show us some different styles of making use of this mechanism. particularly with the usage of vim for complicated projects without the use of a heavyweight IDE (say C++ ?).
Original Question
Now that I am using vim for everything I type, rather then just for configuring servers, I wan't to sort out the following trivialities. I tried to formulate Google search queries but the results didn't address my questions :D.
Question one: How do I yank and replace multiple times ?
Once I have something in the yank history (if that is what its called) and then highlight and use the 'p' char in command mode the replaced text is put at the front of the yank history; therefore subsequent replace operations do not use the the text I intended. I imagine this to be a usefull feature under certain circumstances but I do not have a need for it in my workflow.
Question two: How do I type text without causing the line to ripple forward ?
I use hard-tab stops to allign my code in a certain way -- e.g.,
FunctionNameX ( lala * land );
FunctionNameProto ( );
When I figure out what needs to go into the second function, how do I insert it without move the text up ?
Question three Is there a way of having a uniform yank history across gvim instances on the same machine ? I have > 1 monitors. Just wondering, atm I am using highlight + mouse middle click.
Answer one: A relevant, if not particularly encouraging, qoute from the Vim docs (see :help put-Visual-mode):
When using a put command like |p| or |P| in Visual mode, Vim will try to
replace the selected text with the contents of the register. Whether this
works well depends on the type of selection and the type of the text in the
register. With blockwise selection it also depends on the size of the block
and whether the corners are on an existing character. (Implementation detail:
it actually works by first putting the register after the selection and then
deleting the selection.)
The previously selected text is put in the unnamed register. If you want to
put the same text into a Visual selection several times you need to use
another register. E.g., yank the text to copy, Visually select the text to
replace and use "0p . You can repeat this as many times as you like, the
unnamed register will be changed each time.
Answer two: R (the capital 'R') puts you in replace mode.
I'm missing answer three, I'm afraid.
Answer three: Not quite matching the "uniform yank history" spec, but "+y yanks to clipboard and "+p pastes from clipboard if a clipboard is available.
Yank into a buffer
:y b
yanks into buffer b
And
:p b
places it.
I think there are more named buffers available.

Configure Macvim's text selection to not include character under cursor

Using macvim, when I copy a text selection, it always includes the character under the cursor.
For example, if the cursor is at the far left and I press shift-down arrow, it selects the entire line plus the first character of the next line (since the cursor is sitting over the next line's first character).
Is there a way to configure macvim to not include the cursor character in text selections?
Take a look at the selection option. By default it's set to inclusive, but you can change it to exclusive to make text selections act the way you want:
:set selection=exclusive
You can also set it to exclusive with the behave command:
:behave mswin
This also sets several other options, however, which may or may not be what you want. See the Vim help for the specifics.
:help :behave
:help 'selection'
I am guessing that shift-down arrow activates visual character mode, and moves the cursor down a line. If you are trying to select entire lines, you would be better off using visual line mode, which is activated from normal mode by pressing V (shift-v). This will select the current line in its entirety. You can then extend your selection to include the lines above and below using the k (or up arrow) and j (or down arrow) keys.
When using Vim, I think it is better to go with the grain rather than to fight against it. Don't expect it to work the same way as other text editors. Accept that the Vim way is different.

Simple Vim commands you wish you'd known earlier [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm learning new commands in Vim all the time, but I'm sure everyone learns something new once in a while. I just recently learned about this:
zz, zt, zb - position cursor at middle, top, or bottom of screen
What are some other useful or elegant commands you wish you'd learned ages ago?
I really wish I'd known that you can use Ctrl+C instead of Esc to switch out of insert mode. That's been a real productivity boost for me.
The most recent "wow" trick that I learnt is a method of doing complicated search-and-replace. Quite often in the past, I've had a really complicated regexp to do substitutions on and it's not worked. There is a better way:
:set incsearch " I have this in .vimrc
/my complicated regexp " Highlighted as you enter characters
:%s//replace with this/ " You don't have to type it again
The "trick" here (for want of a better word) is the way that you can use the search to create the regexp (and 'incsearch' highlights it as you enter characters) and then use an empty pattern in the substitution: the empty pattern defaults to the last search pattern.
Example:
/blue\(\d\+\)
:%s//red\1/
Equivalent to:
:%s/blue\(\d\+\)/red\1/
See:
:help 'incsearch'
:help :substitute
I created this reference of my most used command for a friend of mine:
select v
select row(s) SHIFT + v
select blocks (columns) CTRL + v
indent selected text >
unindent selected text <
list buffers :ls
open buffer :bN (N = buffer number)
print :hardcopy
open a file :e /path/to/file.txt
:e C:\Path\To\File.txt
sort selected rows :sort
search for word under cursor *
open file under cursor gf
(absolute path or relative)
format selected code =
select contents of entire file ggVG
convert selected text to uppercase U
convert selected text to lowercase u
invert case of selected text ~
convert tabs to spaces :retab
start recording a macro qX (X = key to assign macro to)
stop recording a macro q
playback macro #X (X = key macro was assigned to)
replay previously played macro * ##
auto-complete a word you are typing ** CTRL + n
bookmark current place in file mX (X = key to assign bookmark to)
jump to bookmark `X (X = key bookmark was assigned to
` = back tick/tilde key)
show all bookmarks :marks
delete a bookmark :delm X (X = key bookmark to delete)
delete all bookmarks :delm!
split screen horizontally :split
split screen vertically :vsplit
navigating split screens CTRL + w + j = move down a screen
CTRL + w + k = move up a screen
CTRL + w + h = move left a screen
CTRL + w + l = move right a screen
close all other split screens :only
* - As with other commands in vi, you can playback a macro any number of times.
The following command would playback the macro assigned to the key `w' 100
times: 100#w
** - Vim uses words that exist in your current buffer and any other buffer you may have open for auto-complete suggestions.
gi switches to insertion mode, placing the cursor at the same location it was previously.
:q!
I wish I knew that before I started vi for the first time.
^X-F completes using filenames from the current directory. No more copying/pasting from the terminal or painful double checking.
^X-P completes using words in the current file
:set scrollbind forces one buffer to scroll alongside another. e.g. split your window into two vertical panes. Load one file in each (perhaps different versions of the same file). Do :set scrollbind in each. Now when you scroll in one, both panes will scroll together. Ideal for comparing files.
You can use a whole set of commands to change text inside brackets / parentheses / quotation marks/ tags. It's super useful to avoid having to find the start and finish of the group. Try ci(, ci{, ci<, ci", ci', ct depending on what kind of object you want to change. And the ca(, ca{, ... variants delete the brackets / quotation marks as well.
Easy to remember: change inside a bracketed statement / change a bracketed statement.
The asterisk key, *, will search for the word under the cursor.
[+Tab will take you to the definition of a C function that's under your cursor. (It doesn't always work, though.)
Don't press Esc ever. See this answer to learn why. As mentioned above, Ctrl + C is a better alternative. I strongly suggest mapping your Caps Lock key to escape.
If you're editing a Ctags compatible language, using a tags file and :ta, Ctrl + ], etc. is a great way to navigate the code, even across multiple files. Also, Ctrl + N and Ctrl + P completion using the tags file is a great way to cut down on keystrokes.
If you're editing a line that is wrapped because it's wider than your buffer, you can move up/down using gk and gj.
Try to focus on effective use of the motion commands before you learn bad habits. Things like using 'dt' or 'd3w' instead of pressing x a bunch of times. Basically, any time that you find yourself pressing the same key repeatedly, there's probably a better/faster/more concise way of accomplishing the same thing.
Some of my latest additions to my Vim brainstore:
^wi: Jump to the tag under the cursor by splitting the window.
cib/ciB: Change the text inside the current set of parenthesis () or braces {}, respectively.
:set listchars=tab:>-,trail:_ list: Show tabs/trailing spaces visually different from other spaces. It helps a lot with Python coding.
ZZ (works like :wq)
And about the cursor position: I found that a cursor which always stays in the middle of screen is cool -
set scrolloff=9999
gv starts visual mode and automatically selects what you previously had selected.
:shell to launch a shell console from Vim. Useful when for example you want to test a script without quitting Vim. Simply hit ^d when you done with the shell console, and then you come back to Vim and your edited file.
This always cheers me up:
:help 42
vimcryption
vim -x filename.txt
You will be asked for a passphrase, edit and save. Now whenever you open the file in vi again you will have to enter the password to view.
Build and debug your code from within Vim!
Configuration
Not much, really. You need a Makefile in the current directory.
To Compile
While you're in Vim, type :make to invoke a shell and build your program. Don't worry when the output scrolls by; just press Enter when it's finished to return to Vim.
The Magic
Back within Vim, you have the following commands at your disposal:
:cl lists the errors, warnings, and other messages.
:cc displays the current error/warning message at the bottom of the screen and jumps to the offending line in your code.
:cc n jumps to the nth message.
:cn advances to the next message.
:cp jumps to the previous message.
There are more; if you're interested, type :help :cc from within Vim.
Press % when the cursor is on a quote, parenthesis, bracket, or brace to find its match.
^P and ^N
Complete previous (^P) or next (^N) text.
^O and ^I
Go to previous (^O - "O" for old) location or to the next (^I - "I" just near to "O").
When you perform searches, edit files, etc., you can navigate through these "jumps" forward and back.
Marks
Press ma (m- mark, a - name of mark). Later to return to the position, type `a.
^r^w to paste the word under the cursor in command mode.
It is really useful when using grep or replace commands.
Until [character] (t). It is useful for any command which accepts a range. My favorite is ct; or ct) which deletes everything up to the trailing semicolon / closing parentheses and then places you in insert mode.
Also, G and gg are useful (Go to bottom and top respectively).
^y will copy the character above the cursor.
Typing a line number followed by gg will take you to that line.
I wish I'd known basic visual block mode stuff earlier. Even if you don't use Vim for anything else, it can be a big time saver to open up a file in Vim just for some block operations. I'm quite sure I wasted a ton of time doing this kind of thing manually.
Examples I've found particularly useful, when, say, refactoring lists of symbolic constant names consistently:
Enter Visual Block mode (Ctrl + Q for me on Windows instead of Ctrl + V)
Move the cursor to highlight the desired block.
Then, I whatever text and press Esc to have the text inserted in front of the block on every line.
Use A instead of I to have the text inserted after the block on every line.
Also - simply toggling the case of a visual selection with ~ can be a big time saver.
And simply deleting columns, too, with d of course.
q<letter> - records a macro.
and
#<same-letter> - plays it back.
These are by far the most useful commands in Vim since you can have the computer do a whole lot of work for you, and you don't even have to write a program or anything.
Opening multiple files using tabs
:tabe filepath
Navigating between open files
gt and gT or :tabn and :tabp
Save the open session so that you can get back to your list of open files later
:mksession session_file_name.vim
Open a created session
vim -S session_file_name.vim
Close all files at once
:qa
Another command I learned recently
autocmd
It allows you to run a command on an event, so you could for example run the command make when you save a file using something like:
:autocmd BufWritePost *.cpp :make
qx will start recording keystrokes. You can do pretty much any editing task and Vim remembers it. Hit q again when you're finished, and press #x to replay your keystrokes. This is great for repetitive edits which are too complex to write a mapping for. You can have many recordings by using a character other than x.
I would have to say that one of my favorites is putting the help window in a new tab:
:tab help <help_topic>
This opens up help in a new tab and, as somebody that loves Vim tabs, this is ridiculously useful.
:x #(Save and Quit a File)
Same as :wq or ZZ
cw
Change word - deletes the word under the cursor and puts you in insert mode to type a new one. Of course this works with other movement keys, so you can do things like c$ to change to the end of the line.
f + character
Finds the next occurrence of the character on the current line. So you can do vft to select all the text up to the next "t" on the current line. It's another movement key, so it works with other commands too.
:b [any portion of a buffer name] to switch buffers. So if you have two buffers, "somefile1.txt", and "someotherfile2.txt", you can switch to the second with simply ":b 2.t<enter>". It also supports tab completion, although it's not required.
Speaking of tab completion, the setting :set wildmode=full wildmenu is also very helpful. It enables complete tab completion for command-mode, as well as a very helpful ncurses-style menu of all the possible matches when using it.

How can I add non mouse-selectable line numbers to vim? [duplicate]

This question already has answers here:
How to clear the line number in Vim when copying?
(12 answers)
Closed 6 years ago.
So I've figured out how to add line numbers to vim (:set no or :set number) but how can I make it so that when I use my mouse in a terminal emulator to select a block of lines, it does not also select the numbers?
For example, say I have three lines that look like so in vim:
1 First line
2 Second
3 Third
If I want to select the three lines with the mouse what I want is for it to ONLY select the actual text. But what ends up happening is it selects the line numbers as well as all the space to the left and right of the line numbers.
Is there any way to change this behavior? BTW, I'm using the gnome terminal editor in gnome if that makes a difference.
Use the following:
:set mouse=a
to turn on xterm style mousing in all modes. This will allow you to do what you want. Keep in mind if the vim is remote via ssh, you'll need X11 forwarding turned on for the selection to make it to your local clipboard.
AFAIK, that is not possible.
The only thing I can add at the moment is that you'd be better off with
:set invnumber
It will inverse the current condition, so you can map it to a key, and toggle it. That way you don't have to remember two commands.
I agree with the first answer. If you use gvim, you can experiment with using set mouse=n and set mouse=a, which should change the line number selecting behavior.
When I want to select text into a terminal, I remove line numbers.
:set nonu
When I finished
:set nu
You're probably not on a Macintosh, and I can't tell if you mean you want to use system copy rather than vim's yank.
But if you are both those things, you can use option-drag to select text. This creates a 2d box over the text, selecting things that are under it. It should be functionally the same as 'take columns x1 to x2 of rows y1 to y2'.
I'm pretty sure this is not possible in terminal-vim. There is no accepted standard (e.g. a TTY escape) for indicating blocks of characters that aren't highlightable by mouse, as far as I know.
I would use gvim if you want to be able to do this at any cost. Its behavior is as you describe.
My recommendation is to get used to the little box on the right hand corner of the screen which has the current line's number and character position information. Before I used vim I could never imagine living without line numbers, but since then I've moved beyond this. Not having line numbers clutter up the screen allows for distraction free code viewing, and do you really need to know that the line number above your current active line is one less than your current line's number, and the line below is one more?
A copy without mouse:
Enter Visual mode: Position your cursor, press v (For complete line V) move up or down until desired position
Press control + c to copy into clipboard (if +clipboard exists in :ve command output) press ESC and paste it wherever you want with control + v.
Or press y to yank into register and press p to put the text after the cursor

Resources