Annoying vim behavior in visual mode - vim

Let's say I have the following three lines and cursor is where ▐ is:
1. This is a slightly longer line.▐I want to delete this and the next line.
2. This is a shorter line
3. This is the third line
I want to delete the rest of the line from the cursor and line 2, so I do vjd, but that leaves my text like:
# This is a slightly longer line.# This is the third line
This is because when selecting text in visual mode, vim selects an additional virtual character at the end of the line. I've played around with virtualedit but that didn't seem to help.
Any clues on how I can get the original behavior that I wanted?

Try three keystrokes - sh-d, sh-j, sh-d
shift-d (delete to end-of-line)
shift-j (merge next line with this)
shift-d (delete the merged, i.e., second line
That will do the trick for you.

That's because if you don't have virtualedit set, vjd will delete everything including the new line character. So you just have to do vjhd instead, to keep the new line intact.
Otherwise with virtualedit=all, I don't face this problem.

Related

Vim: how to visual select up to characters with backspaces in

I have a *.qif file (text file) in which I would like to remove all lines from the second line (not including the header) to a line which contains the characters 'D1/7/2015'. In vim I have tried block selecting from the second line and searching up to a the characters but it doesn't work. What I have tried so far:
Move the cursor to second line then:
v/D1/7/2015
v/D1//7//2015
v/D1///7///2015
None of these seem to work.
You can escape slashes with backslashes:
v/D1\/7\/2015
or tell Vim to parse your pattern with "verymagic" syntax:
v/\vD1/7/2015
See :help \v.
Not exactly what I wanted but it works. I used /D1.7.2015 to get to the line then started visual selection up to the beginning of the file with vgg after which I moved one line down with the cursor then pressed d.
You can delete directly without using the visual selection from the second line to the line containing 'D1/7/2015' by executing the command below:
:2,/D1\/7\/2015/ d

Cursor placed at the very beginning of the line instead of the place where I left it

I don't understand why sometimes in Sublime Text 3 the cursor is placed at the very beginning of the line, instead of the place where I left it. I illustrate it below:
1) I start a new line, the cursor is in line with other commands, i.e., where it should be
2) Before starting to write in that line, for whatever reason, I move to some other line and I start a new line there:
3) Then I move back to the line from 1) but the cursor is placed at the very beginning of the line, instead of the place where I left it in 1)
Is this the desired behavior? Personally, it's annoying to me. I've tried to change it, but I cannot find any solution in the documentation nor Google. I'm under Max OS, have "auto_indent": true and rather standard settings in ST3.
Add the following to your user preferences (Sublime Text -> Preferences -> Settings-User):
"trim_automatic_white_space": false
The default true value gets rid of any auto-indentation whitespace when the cursor is moved off the line. In languages like Python where whitespace is important, the interpreter (maybe) and linters (definitely) might complain if you end up not putting anything on a line except whitespace. However, Sublime has an option for that, too! Setting the following:
"trim_trailing_white_space_on_save": true
will get rid of any whitespace between the last non-whitespace character and the newline, and all whitespace on a line containing nothing else when you save the file. That setting has saved me lots of PEP8 warnings.

VIM Select Entire Line

How do you select a single line in VIM, when your cursor as at some random point along that line?
I know you can do (v, $) to get to the end of the line, or (v, ^) to get to the start, but when you do (v,$,^) it logically doesn't select the whole line, it selects from cursor, until end, then switches it to cursor until beginning... So this approach fails of course.
Capital V selects the current line in one key stroke; two, if you include the "shift" in shift+v.
V would be direct answer. However, I rarely need to do this because "selecting the current line" is generally part of a larger task. Example of such tasks includes copying the line and deleting the line. There's generally a better way to accomplish the task as a whole. The following are some of the tasks I can think of:
copy the line: yy
delete the line: dd
indent the line: >> or <<
select the current paragraph: vap or vip
delete from the current line to the end of the file 0dG
highlight the current line to see where my cursor is: use :set cursorline in .vimrc file
One case in which I do use V is to select multiple lines that are not a paragraph or some other text object. In this case, there's a tip that might be useful for you: once in the selection mode, you can use o to jump the cursor between the start and the end of the selection.
While this might be more keystrokes.
If you are already in visual mode you can use o to go to the other end of the visual selection.
So you can type
v0o$
To select the whole line. Take a look at :h visual-change
However from the comments it seems you just want to copy the whole line.
Which would just be yy
Just change your order of operations. You almost have it.
^,v,$
Or as suggested by #Kent: because ^ goes to the first non-empty char, if the line has leading spaces:
0,v,$
I know this thread is super old, but I just had the same question. This thread came up first, but I found a different answer than any found here. Use 'V' to select whole lines. That easy. One character to select the whole current line.

Easier line referencing

I was wondering if anyone knows of a plugin to enable easier line determination.
I have issues quickly scanning to see what line I was to reference in commands such as t and m
See this screenshot:
If I wanted to quickly reference line 5 (I do have line numbers switched on, I just accidentally cut it out in this screenshot) I find I have to look rather hard to find the correct line number.
so: Is there a plugin which makes referencing lines less eye-straining?
I guess your problem is with those deeper indented lines. sometime it is not easy to "connect" the line number and the line text. If this is the case, you may try followings:
set listchars=tab:>-
(see :h 'listchars' for detail) this line will show the <tab> with certain chars. for example, following screenshot is a formatted maven pom.xml, with relative deeper indent lines. I think it would be ok to read the line numbers of them. E.g. the line 1180-1184.
I hope it helps.
with plugin
If the above doesn't help, e.g. you have spaces not <tab>, you could try a plugin: indentLine, with this you could set a variable g:indentLine_char with the char you like. e.g. > to show indent level clearly.
The link of the plugin: https://github.com/Yggdroot/indentLine
:move and :copy are not limited to line numbers (absolute or relative) only, either as source or as target.
You can use search patterns too:
:m?foo
would move the current line just under the first line matching foo going upward,
:t/bar
would copy the current line just under the first line matching bar going downward,
:?foo?t/bar
would copy the first line matching foo above the current line to just below the first line matching bar going downward, and so on.
You can also use marks:
:'at'b
would copy the line marked a to below the line marked b,
:m''
would move the current line to just below the line you were before the last jump, and so on.

How to delete, including the current character?

Let's say I've typed "abcdefg", with the cursor at the end. I want to delete back to the c, so that I only have "abc" left.
Is there a command like d that includes the current character? I know I could do dTcx, but the x feels like a work-around and I suppose there's a better solution.
No. Backward motions always start on the left of the current character for c, y and d which is somehow logical but also unnerving.
The only "clean" solutions I could think of either imply moving to the char after c first and then do a forward delete:
Tcde
or using visual mode:
vTcd
v3hd
But, given your sample and assuming you are entering normal mode just for that correction, the whole thing sounds extremely wasteful to me.
What about staying in insert mode and simply doing ←←←←?
try this:
TcD
this will leave abc for your example... well if the abcdefg is the last word of the line.
if it is not the last word in that line, you may do:
ldTc
or golfing, do it within 3 key-stroke:
3Xx or l4X
See this answer to a similar question : there is a setting to be allowed to go beyond the end of the line
From the doc :
Virtual editing means that the cursor can be positioned where there is
no actual character. This can be halfway into a tab or beyond the end
of the line. Useful for selecting a rectangle in Visual mode and
editing a table.
"onemore" is not the same, it will only allow moving the cursor just
after the last character of the line. This makes some commands more
consistent. Previously the cursor was always past the end of the line
if the line was empty. But it is far from Vi compatible. It may also
break some plugins or Vim scripts. For example because |l| can move
the cursor after the last character. Use with care!
Using the $ command will move to the last character in the line, not
past it. This may actually move the cursor to the left!
The g$ command will move to the end of the screen line.
It doesn't make sense to combine "all" with "onemore", but you will
not get a warning for it.
In short, you could try :set virtualedit=onemore, and see if your environment is stable or not with it.
Use d?c
That will start d mode, search back to 'c' and then delete up to your cursor position.
Edit: nope, that does not include current position...
I may be misunderstanding your request, but does 3hd$ do it?
I would use vFdd in this example. I think it's nicer than the other solutions since the command explicitly shows what to delete. It includes the current character and the specified character when deleting.
v: enter visual mode (mark text)
F: find/goto character backwards
d: the character "d" that will be included for removal.
d: delete command
Since it is visual mode, the cursor can also be moved before executing the actual removal d. This makes the command powerful even for deleting up to a non unique character by first marking a special character close to the character and then adjusting the position.

Resources