Undo cursor movement in Vim - vim

Let's say I have my cursor lying in the code below:
internal static SingleSelectList<Country, int> CreateCountrySingleSelectList(List<Country> countries, List<Airport> airPorts)
And the cursor is in the head of the line. Now I want to move my cursor to the second < of this line of code, which is in the WORD List<Country>, and just doing the key sequence of f<; will bring me there.
But what if I did something wrong by pressing another ;, that will bring me to the next < in the line, which is in the word List<Airport>.
In this situation, how can I get back to the second < using the minimum key strokes?
Is there a fastest way to undo cursor movements, instead of F< or pressing the key hfor a very long time?

Try CtrlO to go back in cursor history.
http://www.rosipov.com/blog/open-previously-edited-file-in-vim/

The reversible action of ; is , so the answer to your question would be to use , one time.
You can look up what the command does by :h ;
; Repeat latest f, t, F or T [count] times.
, Repeat latest f, t, F or T in opposite direction
[count] times

Contrary to Ruslan Osipov's wrong answer, the f motion does not store the previous position in the jump list (only motions that usually go to non-adjacent lines do).
But nothing prevents you from explicitly setting a jump yourself with m'. Then, you can return to that position via ``, or <C-O>.

Related

Changing case at the end of a word in Vim

I know gUw will turn properties to PROPERTIES and stay at the first character of the word.
But if I use gUb, it will be PROPERTIEs and cursor at the first charactoer.
If I use gUge, it will be PROPERTIES but cursor at last word.
What should I do if I want to turn the word to upper case, and stay at the end of the word(so that i can keep on typing without a w)
All operator+motion edits will leave the cursor on the first character of the motion. From :help motion.txt:
After applying the operator the cursor is mostly left at the start of the text that was operated upon. For example, "yfe" doesn't move the cursor, but "yFe" moves the cursor leftwards to the "e" where the yank started.
So, assuming your cursor is on the last character of the current word:
properties
^
there is no built-in way to ensure that the cursor will stay there after an operator+motion edit.
One can leave a mark and jump back to it after the edit:
m'gUiw``
but that's more work than just pressing w. It could be turned into a mapping, though, if that's a common need:
nnoremap <key> m'gUiw``
but that doesn't sound very scalable.
Another approach would be to record your edit:
qq
m'gUiw``
q
and play it back as needed:
#q
But your last sentence is puzzling:
What should I do if I want to turn the word to upper case, and stay at the end of the word(so that i can keep on typing without a w)
If you are at the end of:
properties
^
and your edit leaves the cursor at the beginning of:
PROPERTIES
^
w will move the cursor to the beginning of the next word:
PROPERTIES foo
^
and not to the end of the current word.
We are missing some context, here.

Vim: Repeat previous motion?

Let's say that I'm in visual mode, and I type "aw" to extend the visual area to include the next word. I'd like to then include the next couple of words. Is there a single key that I can press to repeat the previous motion (include text object motions)?
I'm aware that '.' repeats the previous change, and 'n' repeats the previous search, amongst other 'repeat' commands, but I'm unaware of any command to repeat the previous motion (whatever it was).
Well there is a command to repeat every motion made with f,t,F or T
Remember that
fx takes to the next occurrence of the character x
Fx takes to the previous ocurrence of x
tx takes you to the character before the next occurrence of x
Tx takes you you to the character after the previous occurrence of x
to repeat those motions press
;
to repeat them backwards (in the oposite direction) press
,
There are some plugins that provide this functionality:
repmo.vim: repeat motions for which a count was given
repeatable-motions.vim: Make most motions repeatable
repmo.vim: give vim support of repeat motion operations (k,j, h,l, w,b, W,B, e,E, ge,gE)
Instead of repeating the motion, there is a plugin for expanding regions via + and shrinking _: https://github.com/terryma/vim-expand-region
NO PLUGINS use ;.
Here's a detailed example:
int function1(){
some code here;
...
...
return 0;
}
int function2(){
some code here;
...
...
return 0;
}
Now let's say you want to rewrite the part inside {} for function1 and function2.
Place your cursor somewhere inside the {}
From Normal Mode (ESC or ctrl+c), press di} (for "delete" "inner" "{")
Now you've deleted everything in the function
Now bring your cursor inside the {} of function2
Press ;.
For visual mode, I think macros is your only option (maybe overkill for aw)
To start recording a macro, press q + q (that last q can be any letter. It's where you macro will be saved)
... do the actions you want to repeat ...
Press q again to stop recording
To replay these actions (even in visual mode):
#q (or whatever letter you saved it to)
To replay the actions 99 times:
99#q (or whatever letter you saved it to)

How to move cursor to specific word in current line in Vim

Let's say we're currently in this line of code:
readonly L|a|zy<ICountryRepository> countryRepo;
and the cursor is in the position of letter "a", as shown in the code between two "|" symbols.
Now I want to move my cursor to the letter y of the word countryRepo, how can I do that using the minimum key strokes?
(Currently I'm using the key sequence of fyfyfyfy in normal mode ... Kind of stupid)
If you know that it's the 4th y, you can do
4fy
If you know it's the last y in the line, you can do
$Fy
If you don't know at which position it is, you can still do
fy;;;
In this case, I would use
W
to move to countryRepo, followed by
fy
I can think of:
4fy
But you should only do this if you are some strange robot.
/co<cr>fy
Which is one character shorter than your solution, but more easy..
Wfy
Go one WORD forward and then find y.
f>fy
Something like this I would do. Depends on what popups in my mind.
You should look into the easymotion plugin, which helps with arbitrary movements.
EDIT:
easymotion is rather worthless here, it is more useful for jumping to targets further away.
If you have vim-easymotion, https://github.com/Lokaltog/vim-easymotion
You can do <leader><leader>t and then search for letter y. It's not that fast for the letters on the same line though. The real advantage is when you jump in the entire file.
I would do
tR;
or
WtR
or maybe
Wfy
Use EasyMotion.
In your case, <Leader><Leader>e then a corresponding keypress (in this case b) will bring your cursor onto the second y. Personally I use <Leader> as the easymotion trigger so it is only 3 keystrokes for me. The main advantage is you do not need to guess or calculate.
use / for search, then type your word and press Enter
however, if you want to jump to next word, just press n

Preferred way to move around in vim (normal mode)

I haven't seen this asked on stackoverflow, and this is my biggest pain point in vim:
How do you all navigate within a file? I found myself using the hjkl too much, or too repetitively, and I want to get better at this. This is frustrating when you're on a large monitor.
I installed EasyMotion - and so far it's been good for me - I just want to know if there's something better...
Thanks!
I like the cheatsheet of Ted Naleid. It's like a reticle so you can easily find the horizontal and vertical movements. Put it on a wall next to your monitor and you will soon pick up new movements on the fly.
The movements that I liked recently are:
() and {} which let you hop function wise in source code
/ and ? + n/N just search, you normally know where you want to go
fx and tx - to jump to or before the next character x
of course you can do a 2fx to jump to the second occurrence of x, like you can do with all movements
% to move between starting and ending parenthesis
I use b and w to move left and right respectively on a single line. For up and down, I use Ctrl+u and Ctrl+d respectively. IMO Ctrl+u and Ctrl+d are better than Ctrl+b and Ctrl+f because they scroll half window at a time so that you don't loose context.
I haven't really used any plugin for moving around in vim so far.
Forgot to mention two other important keystrokes, $ and ^ to move to end of line and start of line respectively.
Several move commands:
b B e E f F ge gE gj gk go G h H j k l L M n N t T w W { } / ? ^ $ # * ` ' | %
Learn them, plus all commands starting with [ like [{ which is very useful when editing C-style codeā€¦
See :help index.txt for reference.
Mostly I use the following (in order of frequency):
'R go to marked position (the ` is too off the baseline keyboard to use much)
/search|?search forward|backward search
n|N next|previous in search
H|L|M top|bottom|middle of display
G go to end of file
1G go to line 1
{ go backward a 'paragraph' (often a code block)
} go forward one 'paragraph'
Most all of these can be augmented with a count before the command.
It depends on how you want to move around, but generally,
A puts you in insert mode at the end of a line
I at the beginning
o inserts a line below
O above
and more powerfully, searching with /<thing you want to jump to> is very handy. In a c file where the functions are formatted
int
funcname()
/^funcname will jump you to the start of the function. There's a bunch more, but this shold be a good start for someone new to vim.
Simple documentation:
http://vim.wikia.com/wiki/Moving_around
Regular movement:
hjkl/arrow keys/page up/page down
% will switch between open/ending braces
gg/G move to top/bottom
Folding:
For collapsing large blocks of code, you can use folding.
http://vimdoc.sourceforge.net/htmldoc/fold.html
Search:
To jump to something in particular type /searchstring (use with set inc for jumping to matches while typing)
* to search forward for the same word the cursor is on
# same but search backward
You can also use marks.
http://vim.wikia.com/wiki/Using_marks
I also use ctags and jumping to find stuff across multiple files.
http://vimdoc.sourceforge.net/htmldoc/tagsrch.html
I've never needed anything else.
I don't really see much to add in terms of general enlightenment but I use (ranked by how often I use them):
w and b
to move by one word to the right and to the left.
/ and ?
to search for a word or pattern to the bottom or to the top.
G and gg
to jump to the bottom and the top of the buffer.
<C-f> and <C-b>
to jump to the next and previous screen.
* and #
to jump to next and previous occurence of the word under the cursor.
f and F
to jump before a character to the right or to the left.
t and T
to jump on a character to the right or to the left.
Ho! and
$ and ^
a lot, too, to jump to the end and the beginning of a line.
Read http://www.viemu.com/a-why-vi-vim.html and run vimtutor, also :help motion.txt will be usefull. I recommend also staying in normal mode all the time - as described in article above. Generally, learning vim is learning piano - you have to practice much.

Moving between lines in VIM

Let's say I have a file with N lines. I'm at line X and I'd like to move to line Y, where both X and Y are visible on screen. I can do that by typing :Y<cr>, but if Y>99 that's a lot of typing. I can also do abs(Y-X)[kj] (move up or down by abs(Y-X)), but for big X,Y computing this difference mentally isn't so easy.
Is there a way to exploit the fact, that both X,Y are visible on screen and move between X and Y fast?
You can :set relativenumber which does that Y-X computing for you (only in Vim >= 7.3).
You can use H, M or L to go the top, middle and bottom of the screen.
Perhaps you can make use of H, M, or L.
These keys jump the cursor to display lines:
H "Home" top of screen
M "Middle" middle of screen
L "Last" last line of screen
With a count, they offset: 4L would go to the third line above the last (1L is the same as just L).
Personally, I make heavy use of the m command to mark a line for navigation. From where I am now, hit mq to mark the position with label q; then navigate to another line, and ma to mark it with label a; and from then on I can hit 'q to jump to position q and 'a to jump to position a. (q and a are arbitrary; I use those mostly due to their position on a QWERTY keyboard.)
One you have the marks, you can use them for commands. To delete from the current position to the line marked with q, you just use: d'q
There is a variant, where instead of single quote you use back quote. This takes you to the exact position on the line where you placed the mark; the single quote uses the start of the line.
Those marks work even for ex (command line) commands. To limit search and replace to a specific set of lines, I mark the beginning and end lines respectively with labels b and e, and then do my search and replace like so:
:'b,'es/foo/bar/g
Dropping my dime in the pond:
I find that traversing code is exceptionally easy with text objects. I rarely do use jk/JK for larger jumps any more. Instead I navigate for whitespace lines using { and }
Since on any one screen there are usually only so-many whitespace delineations (and they are very easily visually recognized and counted), I find that e.g.
3}j
lands me on the intended line a lot more often than, e.g., a guesstimated
27j
To top it all, many 'brace-full' programming languages have opening braces at the start of functions. These can be reached with [[ resp. ]]. So sometimes it is just a matter of doing, e.g.
2[[}
(meaning: go to start of previous function, after the first contiguous block of lines)
My version of VIM lets you guestimate a number immediately before hitting J or K to go that many lines.
15K goes up 15 lines
The tougher vimmer you are becoming, the bigger amount of lines you can count at first glance.
Don't know, maybe there are some clever techniques, but I just type something like 17k/23j and so on.
also, searching some word on the string you want to jump works.
also, zz (center screen) is sometimes helpful in this cases.

Resources