How do I keep vertical alignment when opening a new window (split) under current one in vim?
When I edit a file, some lines are visible (say 1-20). Once I open another window the visible lines change to something else (say 5-15). I would like to keep the original window vertically aligned, meaning see lines 1-11 in my example.
The "vertical alignment" changes because Vim is trying to keep the line that your cursor is currently on in view. The simplest way to do what you want is just to press H before you do your split. H moves your cursor to the top line of the window, so when you split the viewport won't have to change to keep your cursor in view.
This will move your cursor of course, but you can just hit `` to return to the mark that was set when you pressed H.
Related
In sublime text, I can see using Alt - takes to previous cursor position.
But it also takes to other files. I want to go to previous cursor position in the current file only.
What can be done for this
Case1: Open file1 in vi. Select a few lines(select copy is enabled). Paste in a different place.
Case 2: run the command less file1. From the console, select some lines. Paste in a different place.
In case2, I see that there are new lines introduced at where the line display shifts to new line. So, if the terminal width is 80 characters and my line is 100 characters, then 20 characters will be shown in the new line. If I copy from vim, all 100 characters are copied without any line-break. However, if I copy from "less" command, line-break is introduced after 80th character.
This messes up things like path.
Does "less" introduce line-break dynamically for lines longer than the display width?
less is not designed to handle mouse events. So when you select text while running it, the selection will be handled by the terminal behind, which doesn't give any sense to lines, paragraphs and so on; the text buffer is copied as it is displayed, that's all.
On the opposite, if you use vim with the right configuration, mouse events will be detected and treated by vim itself : the terminal will gracefully let vim handle them, for convenience. Then the line layout will be restored correctly when copying lines of 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.
In a text document, I'm [visually or otherwise] selecting several lines, cutting them with d... I'd like to paste these lines to the end of the file without moving the cursor. Is there a relatively simple way to do this?
Use the Implicit Mark from Last Jump
You can use the implicit mark (e.g. ') to return your cursor to the location it occupied just before the last jump. For example:
Gp''
This will (G)o to the end of the file, (p)aste the contents after the last line, and then return to your position at the time you typed G.
There are a few ways:
Marks
Set a mark, do your paste, then jump back to the mark
m':$pu<cr>``
Visual mode
Visually select your lines, copy them, append, and then restore visual selection (optionally delete)
y:$pu<cr>gv
Append to the file
Visually select your lines, use :w to append to the file, and then reload the file. (Note: will move the cursor to the start of the visually selected lines)
:w >><cr>:e!
Create your own command/mapping
You can create your own command and/or mapping that will use winsaveview() and winrestview() to append then restore the cursor.
You can define a mapping which marks the current location, pastes at the end of the buffer using :$put then returns to the original cursor location using the mark.
This works because :put allows a line number prefix (the last line being representable as $). From :help put:
:[line]pu[t] [x] Put the text [from register x]
This would map it to <leader>p:
:nnoremap <leader>p :mark '<cr>:$put<cr>`'
It sets the ' mark at the cursor, pastes at the end, then returns to the ' mark with `
Depends on how you mean "without moving the cursor".
This will paste at the bottom of the current file and then allow you to continue where you cut the lines from.
split window with :split
move to bottom with shift+g
paste with p
close the duplicate split view (zz or :q)
If you dont like the split view, you can use the ctrl+o to jump back after G
move to bottom with shift+g
paste with p
jump back with ctrl+o
Pressing p pastes things bellow the current line, dit deletes things inside html tags. How do I paste something inside html tags?
Nor here
<p>I want to paste something here</p>
Not here
I usually just do vitp which visually selects the inner contents of the tag, then pastes over what is selected.
Works for me.
The result of pressing P and p depends on what you have in the selected register at the time. If you delete or yank one or more entire lines (e.g. with dd, Y or Vd commands), then pressing P will insert the contents of your register on the line above the current line, whereas p will insert on the line below the cursor.
If you delete or yank a section of text less than a line (e.g. with the D, or yw commands), then P will insert the contents of your register directly before the current cursor position, and p will insert directly after the cursor (i.e. on the same line).
If it helps, you could consider the linewise selection as being analogous to block html elements (such as <div>), and characterwise selection as being analogous to inline html elements (such as span).
So to answer your question: it depends. Supposing you have a linewise section of text in the register, you would want to break the target tag onto two lines before doing the paste operation. In your example, rather than doing dit to delete the contents of the tag, do cit to delete the same section and go into insert mode. Hit return once, to insert a new line, then esc to go back into normal mode, then P to insert your linewise register above the line with the closing tag.
If you didn't want to split the tag onto multiple lines, you would instead have to make sure that you yanked a characterwise selection into the register. Then you could run:
"_ditP
"_ deletes the text into the black hole register, ensuring it doesn't overwrite what is in your default register. dit deletes the contents of the tag, and P pastes the contents of your default register before the cursor position.
remove the current content between the tags with the command
cit
that will 'change in tags' and once that content is gone, you can paste with middle click or if you need go back into command mode and use your normal p/etc.
vitp should handle a linewise paste.
You can press "v" for visual, then go to where the cursor is, and press p or P.