Vim: change mouse click behavior - vim

I am playing with my vimrc settings, and enabled the mouse support.
set mouse=a
Now, I miss the copy and paste option with mouse. Normally, if the mouse is not enabled, I could copy text with left click + drag, and paste by right click (in Insert mode). So,
How to get the copy, paste with mouse enabled (with mouse click, not Ctrl+v etc). The only option I need with mouse enabled is to click anywhere in the file and the cursor points to that location. This helps faster navigation.
I use putty (x-term) to connect to my debian server.

Easiest solution is just to hold shift down when you select text, to get the old behavior for mouse-copy. Paste with shift-mouseclick will usually work too.

jkerian's solution is generally what I do, but if you ever find yourself wanting to copy and paste a lot, and getting tired of holding down shift, you can do something like this:
" toggle between terminal and vim mouse
map <silent><F12> :let &mouse=(&mouse == "a"?"":"a")<CR>:call ShowMouseMode()<CR>
imap <silent><F12> :let &mouse=(&mouse == "a"?"":"a")<CR>:call ShowMouseMode()<CR>
function ShowMouseMode()
if (&mouse == 'a')
echo "mouse-vim"
else
echo "mouse-xterm"
endif
endfunction

On OSX in a Terminal window you need to hold down the fn key when you want to select some text with the mouse in vim that you want to copy. You can tell it's selected as it is highlighted in a different colour (for the 'Homebrew' profile it's blue) as opposed to vim's default highlight colour (e.g. green). Then you can copy the selected text as usual using ⌘+C.
And to add if you want to do this in iTerm2 you need to hold down the ⌥ (Alt/Option) and mouse to select text for copying - see the iTerm2 FAQ for more selection region options.

Hold the shift button and select the text to copy.
Then either click middle mouse button or right click to paste in console. To paste in vim, shift+insert in insert mode.

Related

Find and replace within selection in sublime text 3?

I've read this thread indicating how to find and replace within selection in ST2. Despite reading this thread but still cannot figure out if it's possible to do a simple find and replace within selection in ST3?
Ensure the find and replace panel is closed.
Select the text in which you want to perform the find/replace - i.e. define your selection.
Open the Find/Replace panel - if desired, you can use a keybinding to run the show_panel command by going to preferences -> keybindings, then adding this line to the user keybindings: { "keys": ["ctrl+shift+s"], "command": "show_panel", "args": { "panel": "replace", "in_selection": true } } to ensure that the "in selection" mode is active and skip the next step. (see the forums for more available args.)
Turn on "in selection" mode if it is not already active by clicking on the button in the panel.
Enter your find/replace terms and perform your find/replace
If you want to change the selection you are searching within, you will need to close and re-open the panel.
If the region/selection you are searching within always contains multiple lines, you may want to consider setting the following preference for convenience:
// When auto_find_in_selection is enabled, the "Find in Selection" flag
// will be enabled automatically when multiple lines of text are selected
"auto_find_in_selection": true,
Select the word by pressing CMD+D it selects 1 from whole file and show transparent selection of all related word you can press more times CMD+D for selecting all word then edit it all words take places
You can (using ctrl-h, then toggling the 'in selection' option from the dialog), but it's a bad design IMO. Firstly, if you have an area of text highlighted when you request 'replace', it should assume "replace in selection" automatically. Secondly, as soon as you choose 'replace' it thoughtfully cancels the text selection for you. The whole process ends up involving cumbersome repetition, or an illogical sequence of steps. Visual Studio gets the UI design right; Sublime does not.
Searching and/or replacing within only a selected area in Sublime is indeed possible.
If you have text selected, once you open the Find or Replace panels, you can select the button for Find in Selection (which is turned off by default unless you have the auto_find_in_selection turned on and select multiple lines) to have the operation you carry out only apply to the selected area.
In this example I'm doing the following:
Pressing Alt+F3 to select all instances of the word to, showing how many of them there are
Select the first paragraph and then open the Find and Replace panel with Ctrl+H
Turn on the option to operate within the selection
Replace all instances of the word to with the word FROM
Show that the instances of to outside of the selection are still there untouched.
Select the line/word/code and Press Ctrl+d.
Matched piece of text will be selected and will have cursor after them.
You can edit them together.
Ctrl+h is the shortcut for 'Copy and Replace' in all versions of Sublime.

Highlighting Text With Keyboard Between Limits On Sublime Text

In Sublime Text I can highlight text between specific locations by holding down shift and clicking the start of the region I want highlighted and then the end.
How do I do this with just the keyboard?
Method 1
Explore the commands in the Edit > Mark submenu.
Move your cursor to the start of the desired selection and run Set Mark (Ctrl+K, Ctrl+space).
Then move your cursor to the end of the desired selection and run Select to Mark (Ctrl+K, Ctrl+A).
Method 2
Or, as noted in the comments, hold down Shift as you move across the desired selection (using arrows, etc.).
Try the key command Ctrl+Shift+End on Windows when you are at the beginning or in the middle somewhere of a document. This works with any program.

How to hold the highlighted section for sometime in VIM editor

I am right now analyzing some code using VI editor. In my use case, I have selected code spanning 2 Pages by using ESC SHIFT v & selecting all the lines (Spanning 2 Pages). Now the issue I have is I am not able to hold the highlight until I need. As soon as I press ESC and move the cursor the highlight goes off.
How do I hold the highlight until my need
If you just want to reselect whatever you previously selected when you leave visual mode you can use gv. You can't keep highlight when leaving a visual mode, though.
Edit:
If you just need to view selected text and you don't want to be distracted by surrounding text, you can simply copy it to an empty buffer. To do so select your text in visual mode, press y then :new then P. When you finish you can close newly created buffer with :bd!.

How to delete selected text in the vi editor

I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
I am using PuTTY and the vi editor. If I select five lines using my mouse and I want to delete those lines, how can I do that?
Forget the mouse. To remove 5 lines, either:
Go to the first line and type d5d (dd deletes one line, d5d deletes 5 lines) ~or~
Type Shift-v to enter linewise selection mode, then move the cursor down using j (yes, use h, j, k and l to move left, down, up, right respectively, that's much more efficient than using the arrows) and type d to delete the selection.
Also, how can I select the lines using my keyboard as I can in Windows where I press Shift and move the arrows to select the text? How can I do that in vi?
As I said, either use Shift-v to enter linewise selection mode or v to enter characterwise selection mode or Ctrl-v to enter blockwise selection mode. Then move with h, j, k and l.
I suggest spending some time with the Vim Tutor (run vimtutor) to get more familiar with Vim in a very didactic way.
See also
This answer to What is your most productive shortcut with Vim? (one of my favorite answers on SO).
Efficient Editing With vim
Do it the vi way.
To delete 5 lines press: 5dd ( 5 delete )
To select ( actually copy them to the clipboard ) you type: 10yy
It is a bit hard to grasp, but very handy to learn when using those remote terminals
Be aware of the learning curves for some editors:
(source: calver at unix.rulez.org)
If you want to delete using line numbers you can use:
:startingline, last line d
Example:
:7,20 d
This example will delete line 7 to 20.
Highlighting with your mouse only highlights characters on the terminal. VI doesn't really get this information, so you have to highlight differently.
Press 'v' to enter a select mode, and use arrow keys to move that around. To delete, press x.
To select lines at a time, press shift+v.
To select blocks, try ctrl+v. That's good for, say, inserting lots of comment lines in front of your code :).
I'm OK with VI, but it took me a while to improve. My work mates recommended me this cheat sheet. I keep a printout on the wall for those odd moments when I forget something.
Happy hacking!
When using a terminal like PuTTY, usually mouse clicks and selections are not transmitted to the remote system. So, vi has no idea that you just selected some text. (There are exceptions to this, but in general mouse actions aren't transmitted.)
To delete multiple lines in vi, use something like 5dd to delete 5 lines.
If you're not using Vim, I would strongly recommend doing so. You can use visual selection, where you press V to start a visual block, move the cursor to the other end, and press d to delete (or any other editing command, such as y to copy).
If you want to remove all lines in a file from your current line number, use dG, it will delete all lines (shift g) mean end of file

Moving a block of code by a tabspace

I am currently visually selecting the code and typing ">" which moves the code by 2 tabs. But I only want to move it by one tab.
Is there any alternate command in VIM to move the code by a tabspace.
Ideally I would like to put a marker and then move the whole code block by a tabspace.
Thanks
This will set your shifting width to four spaces (default tab size):
:set sw=4
You can also change the size of the tab stop itself (X is any value you like):
:set ts=X
And if you like to use spaces instead of tab characters, use this:
:set expandtab
If you use the same settings in many files, you can put these in your .vimrc.
> moves the code by one shiftwidth. So you need to set that option correctly.
There are easier ways to do what you want, as others have pointed out, but the
following is of more general use:
You can select a column by pressing ctrl+v and then using the up and down keys (or j and k).
Next press I to go to insert mode. Now you can type anthing you like. In your case, type a single tab.
Finish by pressing esc, and see how your edit is applied to all lines.

Resources