Vim jumps to last occurence of replacement argument - vim

When I execute a replace command, e.g
:%s/toBeReplaced/Replacement/g
Vim jumps to the last occurence of toBeReplaced. This is quite irritating because I have to go back to the position where I was editing. I've quikly renamed my .vimrc to .vimrc_ in order to disable my personal .vimrc. The same thing happens, hence it seems to be vim's default behavior.
When a replacement command is executed, I want vim to stick at the position where I am editing at that moment. Is there any way to achieve this??

after you executed the :s, press :
``
will bring you back to the position before your run the cmd.

Related

Moving around the vim command line

I've been a vim user for a while now and I know how to move around the files, but is there anyway to do so on the editor command line itself (is there a name for that?).
e.g. I typed :vimgerp /sometext/ files/*.js and I realized I spelled :vimgrep incorrectly and I want to just jump to the beginning of that line and fix it. Any other sort of small tips here (jump between words -- neither w,e nor alt+left/right seem to work) also appreciated.
http://vimdoc.sourceforge.net/htmldoc/usr_20.html
<Left> one character left
<Right> one character right
<S-Left> or <C-Left> one word left
<S-Right> or <C-Right> one word right
CTRL-B or <Home> to begin of command line
CTRL-E or <End> to end of command line
The easiest method would be to use CTRL-F. This opens your command in the command-line window where you can edit your misspelled command like any other plain text.
from vimhelp
OPEN c_CTRL-F q: q/ q?
There are two ways to open the command-line window:
From Command-line mode, use the key specified with the 'cedit' option. The default is CTRL-F when 'compatible' is not set.
From Normal mode, use the "q:", "q/" or "q?" command. This starts editing an Ex command-line ("q:") or search string ("q/" or
"q?"). Note that this is not possible while recording is in progress
(the "q" stops recording then).
It's not the fastest and therefore probably only worth it for long commands, but you could just hit enter on the incorrect command as is, undo any incorrect changes if necessary, then open up your command history with q:, where you can edit it as you like. Enter then runs the command if you have your cursor over it in the command history buffer.
Also, shift+arrow left/right jumps between words in command mode, and you can use home/end to go to the beginning and end.

Jump to tag (Ctrl-]) stopped working

When I position cursor over a tag name (no matter at what position inside the tag), pressing Ctrl-] should jump to that tag. It used to work before, but now it seems my Vim has some problems in identifying where tagnames start and end.
For example when I position cursor at first character of usr_09.txt and press Ctrl-] it raises error: E426: tag not found: usr_09. When I place the cursor in the middle of {ident} tag and press Ctrl-] it raises: E149: Sorry, no help for {ident}. In both cases when I visually select whole usr_09.txt and only "ident" inside {ident}, pressing Ctrl-] works fine and jumps properly to their definitions.
What could be the source of these problems?
The <C-]> command uses the 'iskeyword' option to determine the characters that a tag is comprised of. It seem like you lost the . and got {} added to it.
You can reset the value to Vim's help default via
:setlocal iskeyword=!-~,^*,^\|,^\"
or retrigger processing of modelines (what Vim's help page use; see the last line) via
:doautocmd FileType
If this permanently affects the Vim help, check where it got last modified via
:verbose setlocal iskeyword?
and change / remove that wrong :set command.

A strange stuff of my gVim and emmet plugin

I install gvim and its emmet plugin(win7 64bit,default _vimrc),but has some problem with it.
I found a strange stuff in my gVim. when I expand at a letter, my vim will pick one letter from the line next to the current line and push to the current cursor point. like this:
body { c| }
#wrap{position:relative....
(cursor at '|')when expand, it will pick the 'o' in 'position' to c and so problem comes.
but if I back to normal mode and expand,all will run perfect.
What a devil! If the next position of cursor in the next line is blank, the expand action will run with no mistake.
I just use vim a week, can somebody tell me the possible reason of this?
It looks like a bad installation but another thing seems off: Emmet's default leader combo is <C-y> so, if anything, Vim should insert a character from the line above (:help i_ctrl-y) not from the line below (:help i_ctrl-e).
I'd suggest insisting on the issue tracker.

Wrap long lines in Vim?

I've noticed that gq does not work when I paste in a long line. For example, with a textwidth=72 and formatoptions=tcroqbnl, gq refuses to wrap this (in insert mode, I pasted the entire label contents, and then exited insert mode with ESC):
<label for="contact_reason_1">To get assistance with or to confirm a tire replacement recommendation</label>
If I add a line break in (after "to", for example), it'll wrap then. The funny thing is if I join the line back together, it'll happily wrap it again. So VIM seems to somehow be remembering "oh, this is one paste, don't wrap it".
How do I turn that feature off? I'd like gq in command mode to always work. Taking l out of formatoptions did not seem to help (and it shouldn't, this isn't insert mode).
clarification
Yes, I'm using a motion command, in particular, gq<Right>. formatexpr and formatprog are both unset. If it matters, this is in gvim on Debian GNU/Linux, vim version 7.2p284.
steps to reproduce
Pop up gvim on an open file.
Press i to get into insert mode, then type This is a long line. A long line. But not wrappable yet. Or yet. Soon.
Press ESC, then I. Type Now putting text in front of the long line. note: there is a space after the final period, can't get SO to show it, except when this note is here. FUN.
Press ESC, then A. Type And some after. note: space before the And, same SO problem.
Press ESC one last time. Now try gq<Left>, note it only wraps And some after.; I can't get vim to wrap the rest of the line (without going into insert mode and doing a line break by hand, then it works).
Fixing this state is doable; putting a newline after "now" and then hitting undo makes line wrap work again. WTF.
gq isn't enough to wrap the text. You have to give it a motion over which to wrap (like gqj) or tell it to wrap the current line with gqq. Are you sure you're not just mistyping it?
If you aren't, what are the formatexpr and formatprg options set to, if anything?
Update
The problem is the b setting in formatoptions. That's telling Vim to only wrap the text added during the last insertion.
I find that if I select the line before doing the gq, it works fine. Doesn't gq want to be combined with some text selection operation to work?
UPDATE
I confirm the bug. Running vim -u NONE, my formatoptions are vt.
Maybe Bram Molenar or at least the vim community would be interested?

Vim / vi Survival Guide

What are the essential vim commands? What does a new-user need to know to keep themselves from getting into trouble? One command per comment, please.
What I find irreplaceable (because it works in vi also, unlike vim's visual mode) are marks. You can mark various spots with m (lower case) and then a letter of your choice (eg x). Then you go elsewhere, and can go back with ``x(backquote letter) to the exact spot, or with'x` (apostrophe letter) to go to the line.
These movements can be used as arguments to commands (yank, delete, etc). For example, you want to delete 10 lines; instead of counting and then moving to the topmost line and entering 10dd, you go to either the start or the end of the block, press mm (mark m), then go to the other end of the block, and press d'm (delete apostrophe m). If you use backquote instead of apostrophe in this example, then the deletion will work character-wise, not line-wise. Try marking in the middle of the line with "mark m", moving to the middle of another line, then entering "d backquote m" and you will see what I mean.
I was very happy the day I learned about using * or # to search, down or up respectively, for the word under the cursor. Make sure to :set incsearch and :set hlsearch first.
I like this QRC!
http://www.fsckin.com/wp-content/uploads/2007/10/vi-vim_cheat_sheet.gif
When you have some repetitive action to take Macros are usually faster than regex.
Just type
q[0-9a-z] in normal mode
Many people use
qq
because it's fast.
Press
q in normal mode
again to stop recording.
Then just type
#[0-9a-z] in normal mode
to repeat what you just recorded.
#q
for the example like above.
Edited to add: you can also repeat the macro. Let's say you programed a macro to jump to the head of a line, insert a tab, and then jump down one line. You then test your macro by typing "#q" to run it once. Then you can repeat the action nine more times by typing "9#q".
:q -> quit
:w -> save
:q! -> quit and don't save
:x -> save and quit
:[number] -> go to line number
G -> go to end of file
dd -> delete line
p -> "put" line
yy -> "copy" line
:s/[searchfor] -> search
I guess those are the basic one to start from
Use the 'J' (J for Join; upper-case) command to delete the newline at the end of a line. You'll find it tricky otherwise.
This recent Vim tutorial from IBM is pretty good
First of all you need to know how to close vi:
ctrl-c : q!
Rest can be found from vimtutor. Launch vimtutor by typing vimtutor at your command line
Although this is a matter of personal preference I've found that one of the essential things to do is to remap Esc to something else.
I find it very uncomfortable to reach for the Esc key to exit insert mode, but the beautiful thing about Vim is that allows key mappings.
I'm currently using the following mapping using Control + S:
inoremap <C-s> <Esc>:w<CR>
This has the advantage of being a key mapping I have already committed to memory and has the added value of saving my work every time I go to normal mode. Yeah, I know it is crazy but I would be hitting the save command that frequently anyway. It's like a bad habit, you know.
" ~/.vimrc
" Turn on line numbering
set nu
" Turn on syntax highlighting
syntax on
" Set 4 space expanding tabs
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
"turn off line wrapping
set nowrap
" Map CTRL-N to create a new tab
:map <C-n> <ESC>:tabnew<RETURN>
" Map Tab and CTRL-Tab to move between tabs
:map <Tab> <ESC>:tabn<RETURN>
:map <C-Tab> <ESC>:tabp<RETURN>
If you're using vim, the 'u' command (in command mode) will Undo the last command you typed. You can use this command repeatedly to undo mistakes you may have made before saving the file.
See http://www.rayninfo.co.uk/vimtips.html for a great collection of Vim tips, from the basic can't-live-without to very sophisticated stuff that you might never have thought of trying.
Lots of great commands are listed at the Vim Tips Wiki.
It's also good to run the vimtutor when learning these commands
alias vi nedit :)
all humor aside..
for vi WHEN NOT using nedit..
i (switch to insert mode)
a (append = move to end of line and switch to insert mode)
esc (exit insert mode)
dd delete a line
x delete a character
:wq (save and quit)
/ start a search
n find Next
? search backwards..
yy (yank) copy a line to the buffer
pp (paste) paste it here
r (replace a character)
<N> <command> this is a neat - but aggravating feature that lets you type digits and then a command so
5dd will delete 5 lines
but at this point you might as well
- man vi and refresh your memory
While there are LOTS more, I switched from Vi to nedit several years ago, which I find has more features I can use on a regular basis more easily. Tabbed editing, incremental search bar, column select, copy and paste. sort selected lines, search and destroy within selection, whole doc or all open docs..
tear-off drop down menus..
and it supports syntax highlighting for all the languages I use.. (with pattern files I've used a long time over the years. VIM many now be equivalent, but It has to introduce a feature that Nedit doesn't and an easy way to migrate my pattern files before I switch again.
I like the Vim 5.6 Reference Guide, by Bram Moolenaar and Oleg Raisky.
You can directly print it in booklet form, easy to read, I always have it laying around.
It's a tad old, but what are 8 years in Vi's lifespan ?
:set ignorecase smartcase
Makes searching case-insensitive, unless your search includes a capital letter. Not the most indispensable perhaps, but I find myself setting this option any time I'm editing in a new place. It's in any vimrc file I own.
:%!xxd
View the contents of a buffer in hexadecimal. To revert:
:%!xxd -r
My biggest tip: ctrl+q saves the day when you accidentally hit ctrl+s to save the file you are working on
I have this in my vimrc
set number
set relativenumber
This gives me a line numbering system which makes j, k keys really productive.
I use vi very lightly, and I only use the following commands:
a - switch to insert mode (after the cursor)
esc - return to command mode
:wq - save and quit
:q - quit (no save, only without modification)
:q! - force quit (no save, also with modification)
x - delete one character (in command mode)
dd - delete the whole line (in command mode)
I know there are many many more, but those are enough to get you by.
One of my favourite commands is %G which takes to directly to the end of a file. Especially useful in log-files.
How to switch between modes (i to enter insert mode (one of many ways), esc to exit insert mode, colon for command mode) and how to save and exit. (:wq)
Another useful command is to search something: /
e.g. /Mon will search (and in case of vim highlight) any occurences of Mon in your file.
As a couple of other people have already mentioned, vimtutor is the way to go. It will teach you everything you need to know in vim. The one piece of general advice I would give you is to stay out of insert mode as much as possible. There is enormous power in the other modes, it just takes a little bit of practice to get used to it.
i - insert mode (escape to exit)
dd - delete line
shift-y - 'Yank' (copy) line
p - 'Put' (paste) line(s)
shift-v - Visual mode used to select text (tryin 'yanking' this text and 'putting' it somewhere.
ctrl-w n - create new window (you can open a file or start new file here)
ctrl-w v - split existing window vertically
ctrl-n (in insert mode) - autocomplete (if supported)
:! to run a shell command, usually with standard in as the file or a selection (shift-V)
Useful plugins to look at:
* Buffer Explorer - use \be to view files in the buffer (and select to re-open)
NB vi is not vim! vim is rapidly turning into the emacs of the new century. nvi is probably the closest thing to the original vi. Here's a nice hint: "xp" will exchange two characters (try it).
replace 'foo' with 'bar' everywhere in the file
:%s/foo/bar/gc
The real power is in the searching. Here are the essential commands:
/Steve will find the first instance of "Steve" in the text.
n will find the next "Steve" in the text.
:%s//Stephen/g will replace all those instances of "Steve" you just searched for with "Stephen".
Not to promote myself, but I wrote a blog post on this subject. It focuses on the critical parts of Vim for a beginner.
My favorites:
% find matching bracket/brace
* and # next/previous match
gg top of page
G end of the page
<Ctrl-v> Change to visual mode and select column
<Ctrl-a> increase current number by 1
<Ctrl-x> decrease current number by 1
Running macros

Resources