Vim search and replace variable names with uppercase - search

I have a file with a few hundred lines of code. I'd like to search through the file and change all of a variable name to uppercase. Imagine like this:
:%s/variable_name/<gUaw>/g
...would change variable_name to VARIABLE_NAME
Is there a more 'vim' way to do this than just running the search/replace for each variable, typing both the original name and the uppercase name?

#phd got it: :%s/variable_name/\U&/g. Thanks for the assist!

You can create a macro for that:
First, search for your pattern:
/yourPattern
Then count the number of occurrences:
:%s///n
Finally, create your macro:
qqngUawq
qq starts recording the macro in the register q, n goes no the next occurenceand the finalq` stops the recording.
Execute it n-1th times (the nth time was done by creating the macro):
42#q
Here n-1 == 42 ;-) replace 42 by your number of occurences.

Related

Vim wizardry to do this

Want a simple way to create this file:
def foo1():
def foo2():
def foo...():
def fooN():
without typing each function signature out. Possible? In excel I would make 'def foo' a column, make an integer column, then a '():' column, copy paste into a .py file, but I want an equally simple solution in vim or sublime! Anyone know how to do this sorcery?
For this trick, we'll use vim. Start by typing the first definition at the top of the code. In insert mode type:
def foo1():
Then exit insert mode using <esc>.
We'll now create a vim macro to replicate this as many times as you like. Go to the beginning of the file with gg.
Now, start recording a macro using qq. This will store your macro in the "q" register. First make a copy of the function definition using yyp. If you want, you can create a blank line above using O<esc>j. Then increment the function number of your copy using Ctrl + a. Finish by going to the beginning of the line using 0 and stop recording the macro using q.
Now simply replay the macro as many times as you like. For example, type 100#q to play it 100 times. VoilĂ !
Change the value of the variable N to your needs:
:let N=5
:execute "normal! ".N."idef foo0():\r\r\<Esc>" | g/\d\+/ :.,$s//\=submatch(0)+1/
N denotes the number of foo functions starting from 1 to N

How to complete the whole list with Vim?

I have a list of products to place on a rails seed and I would like to instead of put brackets one by one on the list with a command place the brackets on the whole list?
for example:
1. Dakine
2. Dale of Norway
3. Dan Post
1. ["Dakine"],
2. ["Dale of Norway"],
3. ["Dan Post"],
I searched on the help but did not find any about. Thanks.
You can record a macro in Vim and repeat that.
If you are on number 1, you can do following:
qqf a["Esc$a"],Esc0jq
Explanation:
qq: Start recording macro in register q
f: Go to first space character
a: : Insert after (the space character from above)
\[": Insert those characters
Esc: Back to normal mode
$: Go to end of line
a: Insert after (end of line)
"],: Insert the characters
Esc: Back to normal mode
0: Jump to start of line
j: Go down one line
If you have 100 such lines, you can do 100#q to achieve your result.
With vim substitute command:
:%s/.*/["&"]/
If you don't want to operate on all lines, then select the ones you want to transform or note the related line numbers, and then type :s/..... without the %. You'll see actually :'<,'>s this range represent the visually selected lines, and vim adds it automatically in visual mode.
On Atom you can enable the find to use Regex in the search(there is a button next to the search field)
Then you can search for something like (^.*$) to get every line separated by groups and in the Replace field you use ["$1"],. The $1 represents the value matched by the Regex.
Then just do a Replace All and remove the last comma in your list if needed.

vim change newline into space but not every nth-line

I have a file.txt of 1000 lines and every first 10 lines need to be on one line.
The eleventh line starts on a new line and is added with lines 12-20.
How can I do this in Vim!
I'm not 100% sure I grok what you're after, but I'll give you a few hints and perhaps they'll help.
To join lines together you can use J (that's capital J or Shift-J). You can precede this with a number, such as 10, and join 10 lines together. For example, typing 10J will give you:
Now, what you really need is a macro. This sounds fancy but it's just a way or recording a set of commands. So for example you could take 10J and record it to a macro. Repeat that macro 10 times and then you do that to 100 lines.
I think that the solution to your problem would therefore be something like this:
qa10Jjjq99#a
This does this (see below for an explanation):
Explanation:
q starts recording a macro. Everything that you type afterwards, until you type q again will be recorded. The next character a records the macro to the character a. Thus, qa of qa10Jjjq99#arecords 10Jjj to a. Now, to use the macro that you have stored to a you use the # symbol followed by the letter of where you stored the macro (in this case a, because we typed qa). The 99 means repeat this 99 times, thus, 99#a means repeat what's stored in macro a 99 times.
All together qa10Jjjq99#a means: record a macro q and store it in a. Then, join 10 lines 10J and move down two lines jj before stopping the recording q. Then repeat the macro stored 99 times 99#a.

How to append a number sequence at the end of multiple lines with different length?

I'm rewriting some scripts. I would like to append to many lines like these:
short=s[
verylongword=s[
P=s[
These numbers:
0
1
2
To get this:
short=s[0
verylongword=s[1
P=s[2
I've tried with VIM ctrl+v put I can't put them at the end of the line... Which is the best way to do that even with emacs if with vim is not possible...
EDIT:
Now I know I can do it with this method if I have the number in a column like in my example, so the question is how can I append a number sequence faster without writing the squence first?
Using vim you could use following search and replace statement
%s/$/\=line('.')-1
Breakdown
% apply to entire buffer
s substitute
/$ search for end of line
/\=line('.')-1 replace with linenumber - 1
You asked vim or Emacs...
In Emacs you can simply select the lines to modify and then execute:
replace-regexp RET $ RET \# RET
(on a stock Emacs setup that would be M-x replace-regexp...)
The \# is a special substitution which starts at 0 and increments everytime a substitution is made. It is great and you can of course mix it with other elisp substitutions.
For example if you want to number your entries starting at 100:
replace-regexp RET $ RET \,(+ 100 \#) RET
In Emacs you can also use a keyboard macro to do this. With point at the end of the first line (short=s[), define it like this:
F3 start macro definition
F3 insert macro counter (starts at 0 by default)
C-f move to beginning of next line
C-e move to end of line
F4 end macro definition
Execute the macro
manually by pressing F4 repeatedly
specifying a prefix argument representing the number of times (n) you would like to repeat the macro: C-u n F4
specifying a prefix argument of 0 to repeat the macro for all remaining lines: C-u n F4
If you want the numbering to start at a value different than zero, set the macro counter value by issuing M-x kmacro-set-counter. The default keybinding for this command is C-x C-k C-c.

Add lines using contents of a list

If I had a sentence - "I know a person named Ted who likes //^$" - or basically a sentence with a lot characters I didn't feel like escaping, and I wanted to insert copies of that sentence with different names (e.g. John Mary Bob)...
Can a for loop do this by copying the sentence, pasting it as the next line, and then subbing out the name? How do I tell it where to paste?
I could also paste the list of names in first and then sub the sentence in around the names - eg :s/^/I know a person named /, but I find that if there is a lot of text with a lot characters to escape, I'll probably make an error somewhere and waste time having to scrutinize the expression.
So then, is there an easier way to grab the contents from the sentence and put it into a substitute command?
You can do this with a macro in vim.
Check here for a explanation of vim macros: http://vim.wikia.com/wiki/Macros.
It's a lot easier than using regex stuff.
Like Chiel92 suggests, a macro is the easiest way. Suppose you have a text file that looks like this:
I know a person named XXX who like //^$
John
Mary
Bob
Personally I would:
Go to line 1, and copy the line into a named buffer: :1<enter>"iyy
Go to line 3 and record a macro that copies the name on the line, pastes the contents of the i buffer, and then replaces XXX with the name that was on the line:
Go to line 3: :3<enter>
Start recording a macro to register m: qm
Delete the name into a different register: "od$
Paste in the template: "ipkdd
Replace XXX with the name: :s/XXX/^Ro/
Go to the next line: j
Finish recording: q
For each name line you can now replay the macro: #m or ##
Note: when making macros and have problems replaying I always find it helpful to look at the contents of my recording registry. You can just do ^R^Rm to see all the commands you recorded.

Resources