I'm looking for a way to insert a line of text above each line in the selected text in TextMate.
Does anyone know how to write a command or snippet to do this?
Example, selected text:
This is line 1
This is line 2
This is line 3
This is line 4
I want to insert the following above each line selected:
--im an inserted line--
Which would result in the following:
-
-im an inserted line--
This is line 1
--im an inserted line--
This is line 2
--im an inserted line--
This is line 3
--im an inserted line--
This is line 4
Could this be done using *NIX commands then I could setup a TextMate command to do this?
Select your text, press APPLE + F, then replace \n with \n--Im an inserted line--\n. Make sure that 'Regular expression' is ticked and that you hold in SHIFT while pressing the 'Replace All' button.
I would record a macro:
Insert a new line above with ⌥⌘↩
Paste desired text
Then save it as a command and possibly assign a shortcut to it.
EDIT
The best way to achive what you want in TextMate is to select all the lines then hit ⌥⌘a (Text > Edit Each Line in Selection) then do all the keyboard mashing necessary to paste what you want where you want:
⌘← to go back to the beginning of the line
↩ to insert a newline, effectively adding a blank line above the current line
↑ to jump to the new blank line
⌘v to paste your text
It will do that on each selected line simultaneously.
You can save a few keystrokes the next time you have to do that by recording all these steps in a macro and save it as a command as in the first part of my answer.
To insert line above current line
I believe the key shortcut is: ⌥ + ⌘ + ↩
Related
I know what recording in vim is.
Let's say I have already a recording on a register and I'm missing some more keys that I want to add to that sequence. Is there a way to append these keys to an earlier recording in vim?
Using an uppercase letter will append to a register, so qA would continue recording the #a macro.
From :help q:
q{0-9a-zA-Z"} Record typed characters into register {0-9a-zA-Z"}
(uppercase to append).
Note: this works for everything related to registers, so "Ayw would also append the next word to register "a.
Pasting the Macro
You can paste your register on the current file by doing this (let's say register "a"):
:put a
If I want to delete the last word of the line and I want to erase the space before it, jump to the beginning of the line and go down one line
Original register "a":
$diw
Pasting the register
:put a
On insert mode we need insert the macro literally, so we need to use Ctrl-rCtrl-ra
Note: The second Ctrl-r is necessary to capture Esc or Enter.
Modifying the Macro
Modifying the register "a":
$diwx0j
In the above example we just added x0j
Reassigning the Macro
Then you can make a visual selection and yank to the register "a":
0vg_"ay
0 ........ goes to the beginning of the line
v ........ visual selection
g_ ....... goes to the end of the line without line break
"ay ..... copy to the register "a"
If you want to swich modes you can type Esc literally by typing Ctrl-vEsc
You can also use "let" to set a register, in this case a register "a" to swich case of the line and go to the next line:
let #a="V~\<Esc>0j"
The let option allows you to save macros in your ~/.vimrc, wich comes in handy for those situations where you need the macro in a daily basis.
Another advantage of using "let" to assign and reassign your macro is that you can insert special keys this way:
:let #a="iHello World\<Return>bye\<Esc>"
Is necessary to use double quotes otherwise it will not work as expected.
Note: The digraph ^[ is inserted by typing Ctrl-vEsc and it represents the Esc literally.
I have a file in Linux. I want to delete all lines from cursor position till the EOF. How can I do that using vi editor.
Do something like:
:1,$d
Replace 1 with the number of the line you're on.
: - gets you to command mode
1,$ - indicates a range from the first line to the end
d - the delete command
Step 1:
Esc -> to enter command mode
Step 2: press key d (d stands for delete, vim will wait for second input specifying number of lines to delete)
Step 3 : Shift + g (To delete till EOF)
Press **Esc** come to command mode
press **v** enter visual mode
**Shift + g** "selects everything from current position to EOF"
**DEL** "delete selected"
be careful it can even delete the first line in which case you probably need to do this from the next line in cursor.
Press Esc come to command mode
press v enter visual mode
Shift + g "selects everything from current position to EOF"
DEL "delete selected"
How to delete a current word(word at a caret)?
Below solution forces to put caret at start or end of the word, but that is not very convenient.
Ctrl-BACKSPACE
Delete to word end from caret.
Ctrl-DEL
Delete to word start from caret.
Other solution is to select word using Ctrl + W and then delete it but it is also two steps process.
Is there any shortcut or setting which can delete current word in a single step?
IDEA macro feature is usable here:
Open an editor and place the caret in a word
Main menu: Edit -> Macros -> Start Macro Recording
Press Ctrl-W, then Delete
Main menu: Edit -> Stop Macro Recording
A dialog pops up where you can enter a macro name like "Delete Word at Caret"
Assign a keyboard shortcut: Main menu: File -> Settings, then Keymap -> Macros, "Delete Word at Caret"
I see you have already created a tcket in YouTrack: https://youtrack.jetbrains.com/issue/IDEA-180648
How can I script an action in Vim that will take the selected lines, indent them once, jump to the beginning and insert some code, then jump to the end and insert some code?
The code that needs to be inserted at the beginning and end are static and don't need to change (at least, not at this point).
Let's say beginning text is --- and ending text is ***
:execute "normal gv>i---\<Esc>`>a***"
If you want to record it into a macro, say buffer "a", then starting with a visual selection, use:
qa start recording into a
> indent
i insert mode
--- start text
Esc normal mode
`> end of last visual selection
a insert after
*** end text
Esc normal mode
q end recording
The following key sequence steps should work.
search for the start line e.g. /mysearch
qa - starts recoding the a macro
Vxj where x is the needed number of line(s)
>> to indent
ESC
'< jumps to the start of the marked lines
insert whatever you want (e.g. O to start a new line right above)
ESC
'> jumps to the end of the marked lines
insert whatever you want (e.g. o to start a new line right below)
hit q to end recording the macro.
Or something like this.
HTH
This is an old question, but I thought I'd contribute anyway. This is how you can take a paragraph and wrap text around it. Not exactly what you asked for but this one is just much neater if you write small nice small chunks of code. From anywhere in the paragraph type:
>ip (indent paragraph)
ki (jump before the first line and open a new one)
type what you want at the beginning of the paragraph here
<Esc>} (jump to after the last line of paragraph)
type what you want at the end of of the paragraph
<CR><Esc>
Caveats here are, as implied, this must be a "paragraph" (no empty lines) and this will not work properly if there is no new line after the paragraph. To accomodate this, use i instead of o but then you will be an empty line before the bottom bit of code you insert. You would need to write up something in vimscript if you wanted to account for this.
Here are all the strokes I use for setting up macro t to do a begin/rescue/end (for example) block:
qt>ipkibegin<Esc>}orescue<CR>end<CR><Esc>q
There are certainly better ways but this is keeping it at the marco level.
I use gvim in windows. How do I copy text from the current position to the end of the line in vi and paste it in another file opened in vi?
The normal-mode command to move to the end of the line is $.
You can copy to the end of the line with y$ and paste with p.
To copy/paste between different instances, you can use the system clipboard by selecting the * register, so the commands become "*y$ for copying and "*p for pasting.
$ move-to-linebreak
$
y$ yank-to-linebreak
y,$
"*y$ select clipboard-register yank-to-linebreak
",*,y,$
"*p select clipboard-register paste
",*,p
Check :h registers for more information.
If you don't want to include the line break with the yank, you can use yg_. (Or in your case, "*yg_)
Basically, just recognize there's a difference between $ and g_ movement-wise. It's helped me on numerous occasions.
Add this line to your .vimrc
" Make Y yank till end of line
nnoremap Y y$
More at my vimrc.
A different solution: Dp and paste it with p. In fact this first deletes to the end of line and re-pastes it at the same location. Paste it somewhere else with p.