vim: how to select and replace one word for all its appearance? - select

In a file, I wish to replace all "middle" with "medium", I dont' wish to use
:%s:middle:medium:g
I wish to
1. put the focus on the word of "middle"
2. press 'gd' to high light it,
3. and then do something to replace all its appearance(multiple places) into "medium".
What's more, does vim provide that when I replace a word, while typing, all its locations will display the same change?
How to do this, should I use normal mode or visual mode?

First question
You can use :help gn:
* jump to the next occurence of the word under the cursor
`` jump back
cgnmedium<Esc> change it to whatever you want
. repeat change on next occurrence
. repeat change on next occurrence
. repeat change on next occurrence
You can map it if you want:
nnoremap <key> *``cgn
Second question
No but you can google for "multiple cursor vim" if you really want such a feature.

Related

Select multiple lines with cursors at each line start

I want to select multiple lines and put a cursor at the beginning of each line. Sublime Text can do this with Ctrl-Shift-L
select multiple lines
ctrl + shift + L and then put cursor at beginning of each line
Press Crtl + Shift + Alt + Arrow up/down to select multiple lines in Visual Studio Code. Note that the selected lines will be in one column (if possible).
You can also mark some lines and then do this combination and you have all selected lines included.
Moreover you can press and hold Alt and click the lines you need. This way you can select multiple lines that are not neighbours or in the same column.
To do exactly what Ctrl-Shift-L does in Sublime Text, you must do:
On Windows:
Select the lines.
Alt-Shift-I (will add multiple cursors)
Shift-Home (will go at the beginning of each line and be selected)
On Mac :
Select the lines.
alt-shift-I (will add multiple cursors)
cmd-shift-←
(will go at the beginning of each line and be selected)
More information in this answer.
Put cursor at beginning of first line
Ctrl-Shift-Alt-Arrow down/up will put a cursor at the beginning of the following/preceding lines
Ctrl-I will select those lines with the cursor at the beginning of each line.
NOTE : On my vscode the cursors look like they might be shifted down one line but they actually are not - the are in the right place. If you start typing, it works but you have to hit Enter when you are done to get back separate lines. It is a little quirky but works as you would expect.
EDIT (using a hint from #Maxime's answer)
Select your test first.
Alt-Shift-I : puts cursors at the end of each of those lines but text unselected (I as in island not a lowercase L)
Function-Home : put cursors at beginning of each line.
Ctrl-I : selects all lines.
Important: read the NOTE above.
--------------------- v1.43 see How to put the cursor at the end of all selected lines in Visual Studio Code? with column selection mode it is easy to put the cursor at the beginning or end of lines selected by dragging.
You can hold alt and click the places you need with the mouse. This way you can select rows that aren't related, like row 10,15,18. Also you can select at different places in the same row.
I hope this helps someone, but there is a setting in VSCode called Editor: Multi Cursor Modifier which may do what OP is asking:

How to edit all lines in Visual Studio Code

I have a list of data to which I need to put a ' symbol at the start of the line and at the end of the line. So the original data looks like this:
abcde
cdeab
deabc
eabcd
And I want all of the lines to look like this:
'abcde'
'cdeab'
'deabc'
'eabcd'
In my real data, I would have 10,000 of lines. So if I can do something like Ctrl+Shift+A to select the entire document and then have some magic shortcut to change from selecting all lines to editing all lines that would be perfect!
You could edit and replace with a regex:
Find (Ctrl+F):
^(.+)$
Replace:
'$1'
This regex finds any content on a line and wraps it inside quotes. The $1 refers to whatever is matched inside the parentheses in the regex. In this case, it's "one or more characters" i.e. everything on the line. Be sure to tick the regex icon.
If every line may or may not have a space before the content, and you want every line to have a space, try this:
Find:
^ ?(.+)$
Replace (notice the space before the first quote):
'$1'
Here is an easy way to do this:
Ctrl+A to select all or select your desired text.
Shift+Alt+I to put a cursor at the end of each line.
Type your ' (or whatever you want at the end).
Home will move all your cursors to the beginning of the lines.
Type your ' (or whatever you want at the beginning of all the lines).
You can use the Alt + Shift shortcut.
First press Alt + Shift then click the mouse button on the first line.
Go to the last line, and then do the same.
This will mark all the parts of one side. Whatever you type will be reflected in the marked spaces.
Do the same on the other side too.
Use Toggle Multi curosr Modified from action pane.
Select the cursor points with ctrl + <Mouse click> , you can modify everything simultaneously.
This will require lots of manual efforts if lines are more
You can use Find and Replace.
Besides, paste to Excel and using a function to add character '.
The first thing that came to my mind - replace abcde with 'abcde' line by using option Find and Replace option. I'm pretty sure Visual Studio Code has something similar to that.
You can use the Shift +Alt shortcut for windows and for Mac use Shift + Option
First press Alt + Shift/Shift + Option then click the mouse button on the first line.
This will mark all the parts of one side. Whatever you type will be reflected in the marked spaces.
Place Cursor where you want to insert/delete text.
Goto Selection Menu and choose Column Selection Mode
Scroll to the bottom of the data and shift + click in the last line where you placed the first cursor.
Perform action (add/delete whatevs)
Repeat for whatever other areas you want to change.
v: 1.74.3
1- You can use the Ctrl + H shortcut (menu Edit → Replace)
Enter abcde in Find Control.
Enter 'abcde' in Replace Control.
Then press Ctrl + Alt + Enter.

How do I get a cursor on every line in vscode

I'm trying to use the multi cursor functionality of vscode on a large(ish) file.
the file is too large to select every line individually with ctrl-alt-up or down. In sublime-text I would select everything and push ctrl-shift-l. Is there a similar thing in vscode. I've tried using a regex search for ^, but that gives me an error stating "Expression matches everything".
The command Selection / Add Cursors to Line Ends altshifti will put a cursor on every line in the current selection. (For mac use optshifti)
Tip: You can pull up the keyboard shortcut reference sheet with ctrlk,ctrls (as in, those two keyboard combos in sequence).
(For mac use cmdk,cmds)
Hold Alt+Shift and select the block. Then press End or Right button.
You get selected individual lines.
I use version VSCode 1.5.3 in Windows.
Hold Alt+Shift+i
Hold Home (fn+-> Mac) for right-most or End for left most(fn+<- Mac)
This feature is actually called split selection into lines in many editors.
Sublime Text uses the default keybinding, CTRLSHIFT L
VSCode uses ALTSHIFTI
For Atom you actually need to edit your keymap to something like this
'.platform-win32 .editor, .platform-linux .editor':
'ctrl-shift-L': 'editor:split-selections-into-lines'
Real Lines vs Display Lines
First we have to understand the difference between Real Lines and Display Lines to completely understand the answer of the question.
When Word Wrap is enabled, each line of text that exceeds the width of the window will display as wrapped. As a result, a single line in the file may be represented by multiple lines on the display.
The easiest way to tell the difference between Real Lines and Display Lines is by looking at the line number in the left margin of the text editor. Lines that begin with a number correspond to the real lines, which may span one or more display lines. Each time a line is wrapped to fit inside the window, it begins without a line number.
Cursor At the Beginning of each Display Lines:
Cursor At the Beginning of each Real Lines:
Answer to the Question
Now that we know the difference between Display Lines and Real Lines, we can now properly answer the actual question.
Hold AltShift and select the text block.
Press Home to put cursor on the beginning of every Display Line.
Press End to put cursor on the end of every Display Line.
Press HomeHome (Home twice) to put cursor on the beginning of every Real Line.
Press EndEnd (End twice) to put cursor on the end of every Real Line.
Please understand that AltShiftI put cursor on the end of every Real Line.
Install the extension Sublime Commands.
[Sublime Commands] Adds commands from Sublime Text to VS Code: Transpose, Expand Selection to Line, Split into Lines, Join Lines.
(Don't forget to add the keybinding(s) from the extensions details page to your keybindings.json)
Doesn't VS Code already have a "split into lines" command?
Yes, yes it does. However it differs from the one in Sublime.
In VS Code, when you split into lines your selection gets deselected and a cursor appears at the end of each line that was selected (except for the last line where the cursor appears at the end of the selection).
In Sublime, when you split into lines a cursor appears at the end of each line (with the same exception as in VS Code) and the selection is divided on each line and "given" to the same line.
I have the same problem, i'm used to Alt + drag to do 'box selections' in visual studio but it does'n work in code.
It seems to be impossible for now to do it differently than by selecting every single line.
However plugins should be supported soon so we will likely see a plugin for this if not implemented directly by microsoft.
From visual studio uservoice forums:
We plan to offer plugin support for Visual Studio Code. Thank you for your interests and look for more details in our blog in the coming weeks. http://blogs.msdn.com/b/vscode.
For the preview we are looking for exactly this type of feedback. Keep it coming.
Sean McBreen – VS Code Team Member

Can I make a macro in n++ that does a search/replace?

I'm new to n++, but I have been most impressed with this tool so far. I've been trying to record a macro that do a search/replace, but the 'search' part seems to have the initial search text from the recording 'hard-coded' in the macro.
What I want is:
Manually locate the cursor at the beginning of the first line of a fixed format code segment, then Macro actions:
move cursor two lines down
move cursor right x characters
mark charters from pos x to x+n
search and replace all occurrences of the selected text with "{p_'selected text'}"
In an more advanced version, I'd like to add some logic to step 4: only execute the replace part if the # of occurrences are > 1 (e.g. by first adding a count statement, but I'm not sure how to obtain the returned count # from the dialog box)
Is this possible?
While I'm a big fan of Notepad++, this sounds like something I would accomplish with AutoHotKey. You would select the text and copy it to the clipboard. AutoHotKey would read the clipboard, replace the text as you desire, and either replace the clipboard contents, or send it back to your document. Let me know if you would like to go that route.

What useful macros have you created in Netbeans? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I use Netbeans (nightly build) for Ruby on Rails development and I'm looking to beef up my macros. I've created a few myself:
copy identifier:
select-identifier copy-to-clipboard
paste clipboard over identifier:
select-identifier paste-from-clipboard
double quote element
select-element-next "\""
single quote element:
select-element-next "'"
But I'm looking for other useful ones and Google is giving me nothing.
The record macro feature usually doesn't work too well so I'd rather just "write" the macros myself but I can't even find a reference that lists what commands (like "select-identifier") are actually available.
Any Netbeans macro gurus out there?
Here is a list of macro codes with short explanations. Might be useful for someone.
Update: replaced old link with one to the Netbeans site and included the macro list in here.
abbrev-debug-line Debug Filename and Line Number
adjust-caret-bottom Move Insertion Point to Bottom
adjust-caret-center Move Insertion Point to Center
adjust-caret-top Move Insertion Point to Top
adjust-window-bottom Scroll Insertion Point to Bottom
adjust-window-center Scroll Insertion Point to Center
adjust-window-top Scroll Insertion Point to Top
all-completion-show Show All Code Completion Popup
annotations-cycling Annotations Cycling
beep Beep
build-popup-menu Build Popup Menu
build-tool-tip Build Tool Tip
caret-backward Insertion Point Backward
caret-begin Insertion Point to Beginning of Document
caret-begin-line Insertion Point to Beginning of Text on Line
caret-begin-word Insertion Point to Beginning of Word
caret-down Insertion Point Down
caret-end Insertion Point to End of Document
caret-end-line Insertion Point to End of Line
caret-end-word Insertion Point to End of Word
caret-forward Insertion Point Forward
caret-line-first-column Insertion Point to Beginning of Line
caret-next-word caret-next-word
caret-previous-word caret-previous-word
caret-up Insertion Point Up
collapse-all-code-block-folds Collapse All Java Code
collapse-all-folds Collapse All
collapse-all-javadoc-folds Collapse All Javadoc
collapse-fold Collapse Fold
comment Comment
complete-line Complete Line
complete-line-newline Complete Line and Create New Line
completion-show Show Code Completion Popup
copy-selection-else-line-down Copy Selection else Line down
copy-selection-else-line-up Copy Selection else Line up
copy-to-clipboard Copy
cut-to-clipboard Cut
cut-to-line-begin Cut from Insertion Point to Line Begining
cut-to-line-end Cut from Insertion Point to Line End
default-typed Default Typed
delete-next Delete Next Character
delete-previous Delete Previous Character
documentation-show Show Documentation Popup
dump-view-hierarchy Dump View Hierarchy
expand-all-code-block-folds Expand All Java Code
expand-all-folds Expand All
expand-all-javadoc-folds Expand All Javadoc
expand-fold Expand Fold
fast-import Fast Import
find-next Find Next Occurrence
find-previous Find Previous Occurrence
find-selection Find Selection
first-non-white Go to First Non-whitespace Char
fix-imports Fix Imports
format Format
generate-code Insert Code
generate-fold-popup Generate Fold Popup
generate-goto-popup Generate Goto Popup
generate-gutter-popup Margin
goto Go to Line...
goto-declaration Go to Declaration
goto-help Go to Javadoc
goto-implementation Go to Implementation
goto-source Go to Source
goto-super-implementation Go to Super Implementation
in-place-refactoring Instant Rename
incremental-search-backward Incremental Search Backward
incremental-search-forward Incremental Search Forward
insert-break Insert Newline
insert-date-time Insert Current Date and Time
insert-tab Insert Tab
introduce-constant Introduce Constant...
introduce-field Introduce Field...
introduce-method Introduce Method...
introduce-variable Introduce Variable...
java-next-marked-occurrence Navigate to Next Occurrence
java-prev-marked-occurrence Navigate to Previous Occurrence
jump-list-last-edit Last edit
jump-list-next Forward
jump-list-prev Back
last-non-white Go to Last Non-whitespace Char
make-getter Replace Variable With its Getter
make-is Replace Variable With its is* Method
make-setter Replace Variable With its Setter
match-brace Insertion Point to Matching Brace
move-selection-else-line-down Move Selection else Line down
move-selection-else-line-up Move Selection else Line up
org.openide.actions.PopupAction Show Popup Menu
page-down Page Down
page-up Page Up
paste-formated Paste Formatted
paste-from-clipboard Paste
redo Redo
reindent-line Re-indent Current Line or Selection
remove-line Delete Line
remove-line-begin Delete Preceding Characters in Line
remove-selection Delete Selection
remove-tab Delete Tab
remove-trailing-spaces Remove Trailing Spaces
remove-word-next remove-word-next
remove-word-previous remove-word-previous
replace Replace
run-macro Run Macro
scroll-down Scroll Down
scroll-up Scroll Up
select-all Select All
select-element-next Select Next Element
select-element-previous Select Previous Element
select-identifier Select Identifier
select-line Select Line
select-next-parameter Select Next Parameter
select-word Select Word
selection-backward Extend Selection Backward
selection-begin Extend Selection to Beginning of Document
selection-begin-line Extend Selection to Beginning of Text on Line
selection-begin-word Extend Selection to Beginning of Word
selection-down Extend Selection Down
selection-end Extend Selection to End of Document
selection-end-line Extend Selection to End of Line
selection-end-word Extend Selection to End of Word
selection-first-non-white Extend Selection to First Non-whitespace Char
selection-forward Extend Selection Forward
selection-last-non-white Extend Selection to Last Non-whitespace Char
selection-line-first-column Extend Selection to Beginning of Line
selection-match-brace Extend Selection to Matching Brace
selection-next-word selection-next-word
selection-page-down Extend Selection to Next Page
selection-page-up Extend Selection to Previous Page
selection-previous-word selection-previous-word
selection-up Extend Selection Up
shift-line-left Shift Line Left
shift-line-right Shift Line Right
split-line Split Line
start-macro-recording Start Macro Recording
start-new-line Start New Line
stop-macro-recording Stop Macro Recording
switch-case Switch Case
to-lower-case To Lowercase
to-upper-case To Uppercase
toggle-case-identifier-begin Switch Capitalization of Identifier
toggle-comment Toggle Comment
toggle-highlight-search Toggle Highlight Search
toggle-line-numbers Toggle Line Numbers
toggle-non-printable-characters Toggle Non-printable Characters
toggle-toolbar Toggle Toolbar
toggle-typing-mode Toggle Typing Mode
tooltip-show Show Code Completion Tip Popup
uncomment Uncomment
undo Undo
word-match-next Next Matching Word
word-match-prev Previous Matching Word
I needed to surround text fragments with HTML tags, but couldn't figure out how to do it quickly, so I came up with this macro:
cut-to-clipboard "<strong>" paste-from-clipboard "</strong>"
Simple, clean, and you can put any tag/text you want. Give it a shortcut like Alt+B and you have quick way to insert HTML
for php add semicolon to end line ("complete line")
but keep your cursor at the current point,
so you can keep typing the rest of the line
split-line
caret-down
caret-end-line ";"
caret-begin-line
selection-begin-line remove-selection
delete-previous
This is my first netbeans macro.
"echo '< pre >';print_r();die();"
my shortcut key => Alt+A
for this you can use any shortcut
Oh well, just experimenting with netbeans.
I add a simple new macro for netbeans.
name: "scrollup_newline"
code:
insert-break
scroll-up
It works well, so every i press enter, the editor move one line down. This way i can code and have the caret in the middle of the editor.
This is for HTML. I just made:
caret-begin-line "<li>" caret-end-line "</li>" caret-down
Mapped it to ctrl+shift+L This is my first netbeans macro. Later maybe I can make it work for an entire selection somehow.
Edit: Can't figure out doing it for selections, but adding "caret-down" at the end was almost as good.
Here is the macro I wrote today. I am very much missing an "extract method" or "introduce method" refactoring in PHP, so I wrote this macro. I know this is far less complex than the refactorings in Java, but it can be used and it saves a few copy/pastes and writing repetitive code.
cut-to-clipboard
"$this->UNNAMED_METHOD();"
collapse-fold
caret-end-line caret-backward caret-end-line insert-break
"private function UNNAMED_METHOD() {"insert-break
paste-from-clipboard insert-break
This macro inserts -moz- and -webkit- prefixed equivalents of a css3 style rule:
caret-end-line selection-begin-line copy-to-clipboard caret-backward insert-break caret-up "-webkit-" paste-from-clipboard insert-break "-moz-" paste-from-clipboard
insert-date-time remove-word-previous remove-word-previous
Mapped to CTRL+SHIFT+D to insert the short date, so useful cause I like to date my comments / TODOs but often don't know the date!
Although I think Code Templates and more useful, for example:
// <editor-fold defaultstate="collapsed" desc="${cursor}">
and
// </editor-fold>
Mapped to 'fold' then tab and 'endfold' then tab respectively.
Also
JOptionPane.showMessageDialog(null, "${EXP default="Got here"}");
Mapped to 'jop' then tab is handy.
Probably not the most efficient but it helps me out lol
"(() ? : );"
caret-backward
caret-backward
caret-backward
caret-backward
caret-backward
caret-backward
caret-backward
caret-backward
As one can see, this is for PHP quick conditionals. This then places the cursor in the spot where you start typing the condition. Perfect no matter where your throwing it into. I put this to Ctrl+NUMPAD0 for quick typing access. I orignally had it to two TABs but that quickly got annoying haha
I often like to have commas as the first character separating properties or list items in JavaScript; like so:
var foo = {
a : 'a'
//, b : 'B' //todo: make lcase
, c : 'c'
};
Sometimes, I either forget to do this, or have a mass of text to update. To fix these quickly, I bound the keyboard shortcut C-, C-, -- That's two presses of ',' while holding CTRL key -- to:
caret-end-line selection-next-word selection-next-word " " caret-backward caret-backward insert-break
I found Code Templates to be very useful, especially this one (for HTML):
<${tag}>${cursor}</${tag}>
The abbreviation is "tt", and it can also surround any text with custom tag, when called by the code template menu.
<${param_name default="tag"}${cursor}>
${selection}
</${param_name default="tag"}>
This one will surround anything with any tag. Select something, then Alt+F3, then just select this template from the menu. After that type the tag you want, e.g. "div".
This is a code template for creating a static logger in a class using SLF4J. I tie it to "Log".
private static final ${loggerType type="org.slf4j.Logger" default="Logger" editable="false"} log = ${loggerFactoryType type="org.slf4j.LoggerFactory" default="LoggerFactory" editable="false"}.getLogger(${classType editable="false" currClassName default="getClass()"}.class);
This makes a log.debug line which I tie to "log":
log.debug("${message}", "${variables}");