use :tjump instead of :tag vim on pressing ctrl-] - tags

in vim,
when i press ctrl-]
1) the usual behavior:
it goes to the first matching tag with the word under the cursor,
after that we need to do :tjump separately to see a list of all matching tags and then jump to the desired tag.
2) what i want is:
vim should search the tag file,
if there are multiple matches, show me a list of all the matching tags
if there is one match, simply jump to the matching tag
(just like :tjump does)
this behavior(2) already happens when we use g-ctrl-], but i want it to happen with ctrl-]
i have seen behavior(2) using ctrl-] in some vims in some linuses.
please tell me how i can obtain behavior(2). in other words,
please tell me how i can make ctrl-] to behave like g-ctrl-] using .vimrc or whatever

This will map <c-]> to g<c-]> for both normal and visual modes.
nnoremap <c-]> g<c-]>
vnoremap <c-]> g<c-]>
I suggest you map g<c-]> to <c-]>. In other words just swap the commands.
nnoremap g<c-]> <c-]>
vnoremap g<c-]> <c-]>

Related

How to highlight multiple words on VIM and modify them all? Like CTRL+D does on VS-Code?

On VSCode, if you highlight a text, you can do "CTRL+d" and it will highlight the next matching text and add a cursor there.
You can then start typing/deleting and it will affect all the cursors.
How can I do this in VIM?
Note: I know the search and replace function, this is too slow to type, is there something as easy or almost as easy as it is on VSCode?
:%s/foo/bar
Visual explanation on VSCode:
Highlight the word you're looking to replace:
CTRL+d two times, which highlights them all:
Now modify them all at the same time:
I want this will help you.
You can do the below steps to change all selected words.
< SHIFT > + # // select the all words on your cursor.
:%s//NEW_WORD/g
If you want to change all 'const' to 'AAA',
move to one of the 'const' words and press < SHIFT >+'#'.
And type the command ":%s//AAA/g".
Move the cursor on top of a word in your code.
Type gb to add another cursor. This puts Vim into Visual
mode and ready to operate on the word you have selected.
Type gb to continue adding cursors until you’re done.
Now you can perform an action in Visual mode (delete,
change, etc).
Go back to Normal mode with <ESC>

How to use vimgrep to grep work that's high-lighted by vim?

I've installed vimgrep plugin, inside vim, under normal mode I can type:
:vimgrep mywords %
to search "mywords" for the documents under current directory.
But I wish that in normal mode, when I highlight a word using gd, or in visual mode use 'viw' to select a word, I use a hot key to vimgrep. So I add in my .vimrc and restart vim:
vnoremap <F8> :vimgrep expand('<cword>') %<CR>
Well it didn't work for me, when I put focus on one word and select it, I press F8, no response in vim. How to achieve it?
Thanks!
Vimscript is evaluated exactly like the Ex commands typed in the : command-line. There were no variables in ex, so there's no way to specify them. When typing a command interactively, you'd probably use <C-R>= to insert variable contents:
:vimgrep <C-R>=expand('<cword>')<CR> '%'<CR>
... but in a script, :execute must be used. All the literal parts of the Ex command must be quoted (single or double quotes), and then concatenated with the variables:
execute 'vimgrep' expand('<cword>') '%'
Actually, there's a built-in command for inserting the current word into the command-line: :help c_CTRL-R_CTRL-W:
:vimgrep <C-R><C-W>
Your mapping
You could use all three approaches; let's use the last:
vnoremap <F8> :<C-u>vimgrep <C-r><C-w> %<CR>
The <C-u> clears the '<,'> range that is automatically inserted.
What you probably wanted
Using the current word from visual mode is strange. You probably wanted to search for the current selection. There's no expand() for that. Easiest is to yank, as outlined by #ryuichiro's answer:
vnoremap <F8> y:vimgrep /<C-r>"/ %<CR>
Still missing is escaping of the literal text (:vimgrep searches for a regular expression pattern), and of the / delimiter:
vnoremap <F8> y:execute 'vimgrep /\V' . escape(##, '/\') . '/ %'<CR>
Robust plugin solution
Now, if you also want to avoid the clobbering of the default register, it gets really complicated. Have a look at my GrepHere plugin; it provides a {Visual}<A-M> mapping for exactly that.
Try
vnoremap <F8> y:vimgrep "<c-r>"" %<CR>
:h y
:h <C-r>
Recommended reading: Mapping keys in Vim - Tutorial (Part 1)

how could I using query-replace like isearch-forward while typing something

while isearch-forward run as a command, the context I had typed will hightlight in the current buffer, but while run query-replace don't hightlight that, how can i make it hightlight?
Use isearch-query-replace. It highlights the string to be replaced.
It sounds like you are saying that query-replace does not highlight all of the matching occurrences. Is that right? It should highlight them. If it does not, then try starting Emacs without your init file: emacs -Q. If that shows no lack of highlighting then recursively bisect your init file to find the culprit.
#Rocky mentioned isearch-query-replace. That doesn't change highlighting (which should already be turned on), but what it does do is let you start query-replacing while you are isearching, using the last search string as the pattern for the text to be matched by query-replace.
An alternative to query-replace, useful especially if you have relatively few replacements you want to make and there are lots of matches, is to use on-demand replacement while isearching. For that you need library Isearch+.
To replace any given search hit on demand, just hit C-M-RET. With a prefix arg, C-M-RET prompts you for the replacement text (the default is to replace with no text, which means to delete the hit). You can thus change the replacement text anytime, within the same Isearch invocation.
After replacing the search hit, C-M-RET moves to the next one. So you can just use it repeatedly if you want to replace several successive search hits. Or use C-s to skip replacing the current hit and move to the next one.
On-demand Isearch replacement works also for regexp searching, and just as for query-replacing, the replacement text can be either inserted literally, as is, or interpreted as in query-replace-regexp. In the latter case, you can use \&, \=\N, \#, \, and \?. You can use C-M-` anytime during Isearch to toggle whether replacement text is used literally or interpreted per the special regexp-replacement constructs.
The following packages provide live highlighting and replacement previewing for query replacing, as well as additional features:
https://github.com/syohex/emacs-anzu
https://github.com/benma/visual-regexp.el
https://github.com/benma/visual-regexp-steroids.el
I currently use visual-regexp-steroids.el.
All three packages can be installed from MELPA.

Swapping 2 columns with Emacs

I have 2 columns, separated by comma. How can I swap those columns with Emacs?
I have the following:
column 1,column2
x1,x2
y1,y2
f1,f2
and I want it like this:
column2,column 1
x2,x1
y2,y1
f2,f1
Use M-x query-replace-regexp and then:
\(.+\),\(.+\)
as replace regexp and
\2,\1
for replacement.
In Emacs, you need to escape grouping parentheses with \. So, above regexp would be usually written as
(.+),(.+)
which means that you want everything before comma in first group and everything after comma in second group.
\2,\1
means: write second group, then comma, then first group.
While you can apply techniques given by other people, you can also use the org-mode tables.
Once you convert the data into org-mode table, it is very easy to swap the columns by simple keystrokes. You can have M-x org-mode, select the region then do M-x org-table-convert-region, and then M- on the right most column. I am not sure, how to export the data as CSV, but that should be very easy for you with replace-regexp. This can be helpful: http://www.gnu.org/software/emacs/manual/html_node/org/Tables.html#Tables
Similar to the answer given by #darioo, type the following into the top of your buffer:
(query-replace-regexp "\\(.*?\\),\\(.*\\)" "\\2,\\1")
Then, put your cursor at the end of this line and press ctrl-x, ctrl-e.
You will have an interactive search-and-replace for which you press the space bar to make the change, and press ctrl-g to quit. If you press ! (exclamation mark) then the search will cease being interactive and take place on all matching text.
If you want to reverse the changes then press M-x (usually ESC followed by x) and type undo and press enter.
Emacs has a rectangular selection mode, see for example: http://emacs-fu.blogspot.com/2008/12/working-with-rectangular-selections.html
Even better, if you enable cua-mode, entering Ctrl-Enter will put you in rectangle selection mode that is very easy to use.
http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
Use a macro !
Go to the first line of the buffer
Start recording a macro (F3)
Move to the beginning of the line (^a)
Search for comma (^s ,)
Transpose (M-t)
Move cursor down one line
Stop recording macro (F4)
Select the rest of the lines and:
M-x apply-macro-to-region-lines
UPDATE: This doesn't work properly if you have multiple words in a column. Looking for a more general solution...

using idioms in vim for perl

I have the perl-support plugin enabled. now, I tried the \idd idiom shortcut which would give you a my ($,$); statement with the cursor placed on the first var. Now the second var is displayed as <+name+>. In effect the my line after entering the first variables name would be
my ( $top, $<+name+> );
If it was a code snippet I could have easily used tab to go to the next field, but it is not working in this case. How do I quickly move to changing the 2nd part of the idiom?
Those are jump targets, and you can visit them with Ctrl+J. See perlsupport-templates-jump for details.
If this is VIM, why don't you just use VIM's navigation keys for this?
3W (that is a capital W) will move you three words to the left, right onto $<+name+>.
You could also try:
f< to put the cursor on the <, and then you can change <+name+>.