English mnemonics to Vim's shortcuts - vim

I realize that Vim's main author is Dutch, so I'll settle for those as well. I'm interested, do Vim's "control keys" have equivalents in the english language? You know, Ctrl-O for Open, Ctrl-N for New and so on.
Some of Vim's "control keys" could be assigned some meanings
(a) append / (i) insert
(w) word / b (back one word)
These are just those that I thought off the top of my head.
Do they all have some meaning (:e ?)
I find it much easier to remember them if I know they mean something; they're not just randomly used keys.

In normal mode:
a: append
b: beginning (of current or previous word)
c: change
d: delete
e: end (of current word)
f: find (next given character on current line)
g: go (used as "leader" for many commands)
h: left (only makes sense on the keyboard used by vi's author, same for jkl)
i: insert
j: down
k: up
l: right
m: mark
n: next (occurrence of last search)
o: open (new line below current line)
p: put (paste)
q: quote? (record a macro in given register)
r: replace
s: substitute
t: toward (next given character on current line)
u: undo
v: (enter) visual mode
w: (next) word
x: x-out (delete a single character)
y: yank
z: fold (it's visual, it looks like a folded sheet of paper)

Some do. Check out this cheatsheet, it has a lot of mnemonics:
http://michael.peopleofhonoronly.com/vim/
Some of the more obvious ones:
y = yank
c = change
O = over
f = find
r = replace
u = undo
t = unTil character

My mnemonic for ^ (go to beginning of line): ^ looks like a roof, roof symbolizes home. Home key moves your cursor to the start of line/document.
Look at ADM-3A keyboard layout: the Home key is used to print ^ and ~ symbols.

Related

Jump to current function declaration from middle of function

Is there a way to jump to the signature of the function my cursor is currently in, then jump back to where I was?
For example, when I have a 1000 line function, where the prefix x + y: refers to line numbers, is there a way from me to jump from my cursor location at x + 555 to the signature at x + 0 then back to where I was at (x + 555):
x + 000: void theFn(int arg) {
x + ...: ...
x + 555: /// where my cursor starts
x + ...: ...
x + 999: }
And, yes, I couldn't agree with you more that there shouldn't be 1000 line functions.
Also, is there a way to automatically jump to the end of function without being at the opening bracket of the function?
Useful motions in such case are [[, ][ and <C-o>.
As we can read in help:
*[[*
[[ [count] sections backward or to the previous '{' in
the first column. |exclusive|
Note that |exclusive-linewise| often applies.
*][*
][ [count] sections forward or to the next '}' in the
first column. |exclusive|
Note that |exclusive-linewise| often applies.
*CTRL-O*
CTRL-O Go to [count] Older cursor position in jump list
(not a motion command).
{not available without the |+jumplist| feature}
In short:
[[ to got to the beginning
<C-o> to go back to previous place
][ to go to end
Those motions will have the desired effect only when braces are in the first column, but from your example seems like this requirement is not met.
In such case at the end of :h section we can read:
If your '{' or '}' are not in the first column, and you would like to use "[["
and "]]" anyway, try these mappings: >
:map [[ ?{<CR>w99[{
:map ][ /}<CR>b99]}
:map ]] j0[[%/{<CR>
:map [] k$][%?}<CR>
Unfortunately, Vim doesn't offer better solution as it doesn't parse syntax.
It may change though as Neovim experiments with Tree-sitter.
It also wouldn't be surprising if there was a plugin which provides better support for such motion.
Tagbar could fit this role:
Toggle Tagbar window
Switch to it
Cursor should be already over the current tag
Press enter
Toggle window
You are at beginning of the function
Use <C-o> to get back
I also once found and had in my config a mapping which could also be useful in such case:
nnoremap <Leader>gd ?\v%(%(if|while|for|switch)\_s*)#<!\([^)]*\)\_[^;(){}]*\zs\{

Delete till end of sentence in vim

So I'm playing vim adventures and I got stuck. I need a Vim command that will delete the keys in red. I thought dd would do it, but that only deletes the current line.
Use das or dis to delete a sentence. Use dap or dip to delete a paragraph. See :help text-objects for details. Unrelated to your question, see this wiki page for plugins that provide other, highly useful text objects.
) jumps to the beginning of next sentence, so d) will delete (from the cursor) till the beginning of the next sentence. Vim detects sentences using ., meaning period + space. This does mean that d) will have some problems if your cursor is on either the period or space delimiting two sentences, and will only delete until the first character of the next sentence (meaning it deletes either a space or the period and space, which is almost never what is desired). das will work as you probably expect, deleting the sentence and the delimiter (period + space).
If you specifically want to move (and delete to) the last character in a sentence it is more complicated according to this vi.SE answer:
The solution was either dk which deletes the line and the line above it or dj which deletes the line and the line below it.
My original question was actually not the right question (there are multiple sentences).
To delete to the end of the sentence, from where your cursor is, use the letters, use "d)". the "d" is the delete command object, followed by a motion object ")" which advances the cursor (and the deletion process) to the end of the sentence.
To delete "around" a sentence, including all the extra whitespace, use "das" (delete around sentence). Or to delete inside the sentence, and not all the whitespace, then use "dis" (delete inside sentence).
Once you understand the VIM language, then you can easily memorize a plethora of operations. Use this table to understand VIM's vocabulary:
COUNT NUMERAL + TEXT OBJECT COMMAND + MOTION (or OPERATORS)
"3das" will perform "delete around sentence 3 times"
So, if practical, you could place a numeral followed by...
a command:
d=delete
y=yank (into memory buffer to "put" later)
c=change (delete then insert new text)
and then a motion:
) = move cursor to end of sentence
( = move cursor to beginning of prior sentence
} = move cursor to the next paragraph
{ = move cursor to the beginning of the prior paragraph
w = move cursor to next word
b = move cursor back a word
$ = move cursor to the end of the logical line
0 = (zero) move cursor to the beginning of the logical line
G = move cursor to the end of the file
gg = move cursor to the beginning of the file
h, j, k, or l (you might have to look those up)
OR instead of a Motion, define a field of area using:
a = around
i = inside
followed by the name of the area around the cursor:
s = sentence
p = paragraph
w = word
t = xml-tag <tag example> lots of text between tags </tag example>
< or > = inside or around a tag, frequently found in xml documents
{ [ ( ) ] } = inside or around any type of bracket ...
... {a large area [some more (a little stuff) things] of a great many things }
I actually find this table from the help file the best overview for block commands:
"dl" delete character (alias: "x")
"diw" delete inner word
"daw" delete a word
"diW" delete inner WORD (see |WORD|)
"daW" delete a WORD (see |WORD|)
"dgn" delete the next search pattern match
"dd" delete one line
"dis" delete inner sentence
"das" delete a sentence
"dib" delete inner '(' ')' block
"dab" delete a '(' ')' block
"dip" delete inner paragraph
"dap" delete a paragraph
"diB" delete inner '{' '}' block
"daB" delete a '{' '}' block
So deleting a sentence is das or deleting a paragraph is dap.
If you want to delete from J up to and including the . start at J and use df.
If you want to delete both lines then 2dd
Another option (not sure if it works in the game) is to delete up to and including the period:
d/\./e
You have to escape the period when using a search pattern like this after the delete command.
If you were limited to a single line, it is much simpler:
df.
You can use the command: d2d, but I do not know whether it works in the game.
Vim grammar is [Nubmer] [Operator/ Command] [Motion or Text Object]
So in this case, you can use: 2dd

How to delete till backward in vim

I know how to delete until a char forward, like using dtx to delete till the x char from current cursor onwards. But how can I to do that backwards?
Say I have the following:
"abc I want to delete back to I"
And the cursor is at the end of the line, I want it to be "abc ".
In general, capital letters reverse. So we have:
t do something until a character
T do something BACKWARDS until a character
f do something until and including a character
F do something BACKWARDS until and including a character
I guess the problem is not how to remove till/to (T/F) the char backwards. You need delete the char under cursor too , in your case, the last I. leaving abc<space> at the end.
option 1: (3 key strokes)
You can move to the first I backwards, than D:
FID
option 2: (4 key strokes)
or move cursor to the beginning, do it forwardly.
0FID
option 3: (4 key strokes)
you can do an extra x.
dFIx
option 4: (4 key strokes)
with v:
vFIx
With cursor at end of line in normal mode, do dTc.

How do I substitute the word under cursor in vim script?

After getting the current word with expand("<cWORD>") and processing the result string, I'm trying to replace the current word with it.
How can I do this?
EDIT Source added. I wrote it in python.
cur_word = vim.eval('expand("<cWORD>")')
parts = cur_word.split('.')
if parts:
obj, accesses = parts[0], parts[1:]
result = obj + ''.join("['%s']"%a for a in accesses)
# how do I replace the current word with result?
Edit
It looks like you wanted this:
viW
:s/\%V\.\(\w\+\)\%V/\="['" . submatch(1) . "']"/g
E.g., for the following text, curosr on the second line:
x = a.get.property;
x = a.git.another.property; # cursor on the first letter 'e'
The result will be
x = a.get.property;
x = a['git']['another']['property'];
You probably wanted you
yank one word, then
move the cursor (which you don't mention)
_replace the word under cursor by previously yanked word?
That would be
yiW
(move cursor around)
viWp
So e.g.:
the lazy cow mooned over the racy hump
cursor here: ----> +
Now, doing yiW (yank inner WORD), Fa (back to:)
the lazy cow mooned over the racy hump
--> +
Now, viWp replaces current WORD:
the over cow mooned over the racy hump
--> +
In the python interface for Vim, you can execute normal mode command, in your case,
vim.command("normal BcW%s" % result)
will do the trick.

How to add a word at the beginning of multiple lines in vim?

In Vim,
How do i add a word at the beginning of all lines?
Also how do i add it at end?
Eg..
If i have
A
B
C
D
I want to make it to
int A =
int B =
etc..
use visual block mode (Ctrl-v) to select the column you want, and then hit I, type the characters you want, and then hit Esc
So in this case, you'd put your cursor on A, hit Ctrl-v, go down to D, hit I and type int (it'll only appear on the first line while you type it), and then hit Esc at which point it'll apply that insert to all visually selected portions.
This works for anywhere in the document, beginning of line or end of line.
:he v_b_I for more info on Visual Block Insert
You can do this:
:%s/^/at the beginning/
:%s/$/at the end/
:%s/.\+/int & =
+ won't match on empty lines
If you need to copy just the first word, then do:
:%s/^\w\+/int & =/g
If you want to preserve indentation, then do:
:%s/^\(\s*\)\(\w\+\)/\1int \2 =/g
A global substitute should do i:
:%s/.\+/int & =/
This is how it works: in the second part of the substitution (ie in the int & =) the ampersand is replaced with what machted in the first part (the .*). Since .* matches the entire line, each line is subsituted as wanted.
If you have empty lines (in which you don't want to have any replacements), you could go with a
:%s/^\S\+$/int & =/

Resources