Move to first character in line above in vi - vim

When using vi in normal mode I've noticed that the enter key will bring the cursor to the first character of the line directly below. This is a nice feature but I can't seem to find the key for doing the same thing upwards. Anyone know either of a key that does this or how to create my own?

- (minus) goes to the first non black character on a line going upwards.
One way to figure this out is by looking at the help for <CR> here you find
+ or *+*
CTRL-M or *CTRL-M* *<CR>*
<CR> [count] lines downward, on the first non-blank
character |linewise|.
Which tells you that +, <C-M> and <CR> are equivalent. If you look at the section about this you would see
*-*
- <minus> [count] lines upward, on the first non-blank
character |linewise|.
which is what you were looking for.

Related

Vim: How to delete between the cursor and the first character of the line?

How to delete till the first character of the line ? In the line below, for example, with the cursor near the end as indicated, delete backwards until the first $.
I have this line:
[space][space][space]$entity->setPositionBrand(count($qb->getResult())[my_cursor_here] + 1);
After deletion, I want this:
[space][space][space] + 1);
There is a standard vim motion that goes exactly to the first non-whitespace character on the line. It's ^
So you only need to type d^.
Obviously not as succinct as Vladimir's answer (which is a better solution), but for the record, here's how you could achieve the same with visual mode.
v0wx
v Enter visual mode.
0 Move to the beginning of the line.
w Move to first word.
x delete characters in visual selection.
Any amount of whitespace counts as a word.
If you want a general solution, you would probably need some sort of regex-based keybinding. But, you can accomplish what you want with these key combinations:
Put your cursor on the first $ sign.
d 12 e
The second key sequence deletes characters from the position of your cursor up to the end of the twelfth word, which will result in exactly the line you want.

Insert characters to end of previous line in Vim

I like the Markdown style underlining, but I want it to line up with the above line.
So for example if I have this:
heading one
_
^ cursor here
I could (in normal mode) just type (something)i=<ESC><ESC>, and the result would be:
heading one
===========
^ cursor here
Does anyone know what I can use for (something)?
It doesn't really matter to me where my cursor is/ends up, so for example I could be on the last position of the heading one line and do some operation to achieve the same result. I'm picky, but not that picky.
If you use visual selections you can then use r to replace every character in said visual selection.
So if you start with your cursor on the "heading one" line type.
yypVr=
Would copy the line and then replace every character with an equal sign.
kyypv$r=j
go up one line
yank it
paste it below
visually select the line
replace every character with =
Turn it into a mapping if you ned it often:
nnoremap <key> kyypv$r=j
I'd suggest you do this on the line to underline itself and not on the line below, though:
nnoremap <key> yypv$r=

Which operator can be used to move cursor to the last non-blank character of the screen line in vim?

I know g$ can be used to move the cursor to the last character of the screen line, but which operator can be used to move the cursor to the last non-blank character of the screen line?
g_ (g followed by underscore) moves to the last non-blank character of the screen line text line. (As pointed out by han pingtian in the comments).
To move to the last non-blank character of the screen line, there's no single operator that I could find, but you could combine g$ with ge to achieve this result: use g$ to go to the last character (blank or non-blank) of the screen line, then if the cursor is on whitespace, use ge to move backwards to the last character of the previous word. (And of course, if the character under the cursor is non-blank, just omit the ge operator).
If you're trying to write a script, this won't be as helpful as a single dedicated operator, since it requires making a decision about the character under the cursor after the g$ has completed. But if you're just trying to go to the last non-blank character in visual mode or for interactive editing purposes, the g$ ge combination should suffice for what you need.
Also try substituting gE for ge, depending on whether you want to skip over punctuation or not. (See :help word-motions for the distinction between e and E: basically, E counts ALL non-whitespace characters as word characters, while e counts only letters, digits and underscores.)
Right of Screen
g$
Left of Screen
g0
Top of Screen
H (high)
Middle of Screen
M
Bottom of Screen
L (low)

What is the definition of a non-blank character in Vim?

Launch vim.
In the empty buffer, insert two lines where the first line consists of 3 spaces and the second line consists of hello world.
Here is an example file where the spaces are represented with dots.
...
hello world
Now press gg. The cursor moves to the third space of the first line.
Quoting :help gg:
<C-Home> or *gg* *<C-Home>*
gg Goto line [count], default first line, on the first
non-blank character |linewise|. If 'startofline' not
set, keep the same column.
The documentation says that the cursor should move to the first non-blank character of the first line. I have two questions.
Does :help document the definition of a non-blank character? If so, could you please point me to it?
Is the behaviour that we observe in the experiment mentioned above consistent with the documentation provided in :help gg?
I don't think there is a general definition of non-blank in the vim docs, but I also do not believe this is a "side effect" of gg.
Note that gg is consistent here with ^:
^ To the first non-blank character of the line.
|exclusive| motion.
and [:blank:] of vim's pattern matching behavior (:h blank) defines blank characters as space and tab:
*[:blank:]* [:blank:] space and tab characters
As far as whether or not this is consistent with gg, consider what it says it will do as two steps instead of one:
Go to the first line (default since no count was specified) -- it does this.
Go to the first non-blank character of said line.
Probably the easiest way to implement 2 as an algorithm is to position the cursor after all the blank characters at the beginning of the line. In your case, they are all blank characters (and it cannot move to the next line), so the cursor is positioned at the end of the line (after all the blank characters).

Why does Vim position the caret one character off with $ (end of line)?

Observe a line in a Vim instance:
Now I hit $:
Why does my cursor not go all the way to the end? Once I try inserting, the text gets inserted before the last character! Even if I try to move right again while still in normal mode I get the bell. Oddly, when in edit mode I can move to the actual end of line with the right arrow key!
Does anyone know why Vim does this? On 7.3 by the way. Thanks for the help.
Pressing $ while in command mode causes the cursor to move to the end of the line, effectively highlighting the last character. Hit i here to insert before the last character, or a to append to the line. It is slightly ambiguous here, because you're using a pipe character as a cursor rather than a rectangular block cursor. Have a look at ":help termcap-cursor-shape" if you want to change that.
If the goal is to append to the end of the line, A will jump to the end of the line and enter insert mode with a single keypress.
Use a to append a character after the current.
Or, to go to the end of the line and append in 1 step, use capital A. I.e. shiftA.
Similarly shift-I to insert at the beginning of the line without first having to press ^.
The cursor can't be between two characters, it is always on a character.
If you press $ then x, you will correctly delete the last printable character of the current line.
What you are observing is the fact that using i, you are always inserting your text before the selected character. If you want to insert after the selected character, you have to use a or better A as it has already been mentioned.
In other words:
i means "insert before character under cursor".
a means "insert after character under cursor".
mnemonic for a : a for "append".

Resources