vim - set chars to uppercase between parenthesis
2014-07
let's assume in vim I have following lines:
all what (strong) people have to do is pushing (heavy) weights
over (and over) again in order to gain muscles
and I need to convert words inside parenthesis to uppercase, what is the most convenient way to do so? How do I tell vim it needs to select everything to the first (!) closing parenthesis? So far I came up with
:%s/\s(.*)\s/\U&/g
unfortunately this will uppercase everything between 'strong' and 'heavy' which is not what I want. Any chance to tell vim it should select the chars to the next closing bracket only? (sorry for the silly example, couldn't think of something more sophisticated... or at least vim related... huh)
Try this command in vim
:
:%s/\s([^)]*)\s/\U&/g
Using .*
is 'greedy': it will match all characters, including the first )
on the first line, and go on until the last )
, thus capitalizing everything in between.
Using [^)]*
tells the regex engine to look for any characters that are not a )
within ()
, and capitalize them. Basically, it forces the match to be 'lazy' rather than 'greedy'.
See the sections "Laziness vs Greediness" and "An Alternative to Laziness" in the Regex Tutorial.
This substitution does what you want:
:%s/(.\{-})/\U&/g
Like savanto said, *
is greedy: it matches 0 or more, as much as possible, of the preceding atom. Here I just use the opposite of *
: {-}
to match 0 or more, as few as possible, of the preceding atom.
Remember that if you only need to uppercase the first parenthesis :
Put your cursor inside parenthesis - anywhere - and press gUi(
gU " change to uppercase and wait for a motion
i( " it s for "inside parenthesis"
In a vim buffer if I have a list of characters say:
A
B
c
C
d
D
and for each one I want to replace it with its corresponding ascii code ( in decimal ). Is there a way to do this without using an external tool through :r!some_tool
For instance, I know there is the :ascii and ga commands but they print the value to the screen but I can't find a way to get its output into the buffer.
Check out str2numchar.vim. Down load & install in your vim plugins folder. Add the example visual mode key map to your .vimrc file:
vmap <silent> sn :Str2NumChar<CR>
In vim, highlight the text you want to convert and type sn
.