How do I go to a search phrase in VIM - vim

I've looked at dozens of tutorials, and they all say how to search in Vim, but not how to stay on the search result.
Let's say I'm at the top of the file, and the word cow is on line 700. If I press Esc, then /cow it will show me the word on line 700, and I can of course use n or N. But as soon as I hit Enter, or Esc, or spacebar,... it puts me back at the top of the screen.
Is there a way to search in Vim and then make the cursor go to and stay on that searched word/phrase?
Thank you.

/cow<CR>
is how you move the cursor to the next occurrence of cow in normal mode.
Try again without your config:
$ vim --clean <file with cow on line 700>

Related

When I search in vim and then press “esc” cursor returns to previous position

Something has seemingly happened to my vim install, and I'm not exactly sure what. I'm a long time vim user (although I don't use it as a main editor).
When I search:
/foo
I want to edit the location that it found, so I press ESC (in preparation for getting into insert mode). vim now jumps BACK to where I started from in the file. E.g. if I was on line 0 of a 3000 line file, I search for a particular string, find it at line 1700, and want to edit it - ESC takes me back to line 0.
What's going on? Did I accidentally set some strange mode? Or did I forget a hotkey combination that I should know?
This is the expected behaviour with the incsearch option on:
Note that the match will be shown, but the cursor will return to its
original position when no match is found and when pressing <Esc>. You
still need to finish the search command with <Enter> to move the
cursor to the match.
If incsearch is not on then the cursor doesn't jump to the first match at all, it doesn't move until you press <Enter>.
you said
so I press ESC (in preparation for getting into insert mode).
you don't need to press ESC before you can get to insert mode, you need to press enter (known in vim as <CR> for carriage return).
so if you wanted to find foo and start inserting text, type
/foo<CR>i
remember that <CR> is a single pressing of the enter button.

Vim Relative Numbering Reset on Scroll

When I scroll down a page the relative numbering is no longer based upon the cursor position.
Instead the line position relative to the top of the screen is displayed.
Sometimes I would like to delete or yank 200 lines and I dont want to have to do the subtraction and addition to figure out how many lines down my text is.
How can I show relative line numbers to the cursor even when scrolling?
I think what you want is, you scroll with mouse, and expect that vim keeps the cursor in original place. E.g. your cursor is at line 5, and you scroll down 5000 lines, you expect your cursor is still at line 5. That is, the cursor is out of the window.
AFAIK, the cursor won't go out of the window. That means, if you keep scrolling down, and the cursor line will be the top line of your current window. and the rnu are gonna re-calculated by the cursor line.
May be you could just explain what do you want to do. the cases in your question could be done by 200dd or 200Y but I guess it is not as simple as that.
You may want to find out the ending line by reading/scanning your text lines, and pick the line number (rnu), and do a xxxdd if this was the case. Here you should use normal line number. e.g. your cursor was at line 5, and you scroll down a lot, find the line you want to delete till from line 5. you could do :5,.d vim will delete from line 5 to your current line.
Or you can do 5, 23452d if you find out the lines between 5 and 23452 need to be removed.
If you can search your ending line by /pattern search, vim can do :.,/foo/d this will delete from current line till the next line, which matches foo.
You can still press V enter line-wise visual mode, and moving down by vim-motions. when it reaches the point you want to remove/yand press Y or d
You can take a look this Question/answer:
VIM to delete a range of lines into a register
At the end, I suggest you not using mouse in vim.
This is probably because the cursor moves down a page when you scroll down a page. In vim, the cursor is always on the screen. If you're scrolling down with, say, the mouse wheel, the cursor will just get "stuck" on the top line (modulo scrolloff) and stay there as you continue to scroll down.
Perhaps use ShiftV to start a line-based visual selection before scrolling, then use d or y on the selection?
I can confirm that the desired feature is available in Visual Studio Code (VSC) with the Vim extension installed. This is because VSC does not function like Vim by default and holds the cursor in place like other text editors do. This feature not only makes VSC bearable but proves more useful than vanilla Vim when coding large blocks of code also.
Additionally, VSC also allows for easy and language agnostic comment/uncomment toggling with <Ctrl> + / which is also very useful when used together with the above feature.

moving text to next line in vi

There is a line in Shell script
f) SCREEN = TRUE
and I want to make it
f)
SCREEN = TRUE
I cannot do this very easily in vi. Obviously in a normal text editor enter command can shift the text after f) to the next line. What is the best way to do it in vi?
Press l to move the cursor right and j to move the cursor down to get the cursor to the S. Then press i to "insert" and hit enter to create a new line. Then hit esc to stop inserting and :wq to save and quit.
There are lots of basic vi command lists on the internet that might help.
I was trying to figure out if there was a way to do this in normal mode without pressing 3 buttons to insert, move the down line with enter and then escape to normal. I believe this is what OP may have been asking for. Not sure why there aren't any built in shortcuts for this.. Anyways I just ended up making a quick mapping in my .vimrc.
nmap <leader>j i<cr><esc>
With your cursor on the line, 0fSi<ENTER><ESC>
(0 Go to beginning of line, fS find 'S', i enter insert mode, <ENTER> insert some kind of newline, <ESC> exit insert mode)

Is there something in VIM that behaves like ^E + j?

Is there a key-combination that behaves as though I'd press ctrl-E followed by a j, that is the text scrolls up a line but the cursor keeps where it is, relativ to the screen.
I am aware that I could achieve what I want with a :map but before I do I thought I'd rather want to know if there is already some "built-in" functionality
Yes, use CTRL-D with a count of 1 (not that that saves you anything, really).
The CTRL-D command does the same as CTRL-E, but also moves the cursor down the same number of lines
There is the z command
z. Redraw, line [count] at center of window (default
cursor line). Put cursor at first non-blank in the
line.
zz Like "z.", but leave the cursor in the same column.
Careful: If caps-lock is on, this commands becomes
"ZZ": write buffer and exit! {not in Vi}
These mappings makes it possible to scroll up and down one line with focus on center line (hard to describe so that it sound correct, try it instead)
"scroll with line in center
map <C-Up> <ESC>0kzz
map <C-Down> <ESC>0jzz

Vim keep cursor location while scrolling

Is there a way to keep the cusror location off-screen in Vim / gVim while scrolling? Similar to many Windows editors.
I know about marks, and do use them. I also know the '.' mark (last edit location), But looking for other ideas.
I'm asking this because sometimes i want to keep the cursor at some location, scroll to another place using the mouse-wheel, and then just press an arow key or something to get me back to that location.
No. vim is a console application, so it doesn't really make sense to have the cursour off-screen (it's possible, but would just be confusing)
An alternative solution, to paraphrase posts from this thread from comp.editors:
Ctrl+o goes to the previous cursor location, Ctrl+i goes to the next (like undo/redo for motions)
Marks seem like the other solution..
Also, use marks. Marks are named by letters. For instance typing ma remembers
the current location under mark a. To jump to the line containing mark a,
type 'a. To the exact location use `a.
Lower-case-letter marks are per-file. Upper-case-letter marks are global;
`A will switch to the file containing mark A, to the exact location.
Basically ma, move around, then `a to jump back.
Another option which Paul suggested,
gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Why don't you split the window, look at what you wanted to look at, and then close the split?
:split
or
:vsplit (if you want to split vertically)
The only similar behavior that I've found in Vim:
zt or zENTER "scroll the screen down as far as possible without moving the cursor"
zb "scroll as far up as possible".
Ctrl+E "scroll one line down, if possible"
Ctrl+Y"scroll one line up, if possible"
Sometimes you can avoid jumping to marks before entering text — gi command switches Vim to Insert mode and places cursor in the same position as where Insert mode was stopped last time.
Google says that the cursor (and therefore current line) must be visible in Vi, so you'll have to use marks.
Also very useful are the '' (2x single quotes) and `` (2x back quotes).
The former jumps back to the line you were prior to the last jump (for instance, a page down).
The latter jumps back to the line and column you were prior to the last jump.

Resources