While using Vim, in visual mode, selecting text and then calling a colon command shows : '<,'> instead of just : as it would show when I do other things (such as opening a file).
What does '<,'> mean?
Using linux (debian), gnome-terminal, vim7.2
It means that the command that you type after :'<,'> will operate on the part of the file that you've selected.
For example, :'<,'>d would delete the selected block, whereas :d deletes the line under the cursor.
Similarly, :'<,'>w fragment.txt would write the selected block to the file called fragment.txt.
The two comma-separated things ('< and '>) are marks that correspond to the start and the end of the selected area. From the help pages (:help '<):
*'<* *`<*
'< `< To the first line or character of the last selected
Visual area in the current buffer. For block mode it
may also be the last character in the first line (to
be able to define the block). {not in Vi}.
*'>* *`>*
'> `> To the last line or character of the last selected
Visual area in the current buffer. For block mode it
may also be the first character of the last line (to
be able to define the block). Note that 'selection'
applies, the position may be just after the Visual
area. {not in Vi}.
When used in this manner, the marks simply specify the range for the command that follows (see :help range). They can of course be mixed and matched with other line number specifiers. For example, the following command would delete all lines from the start of the selected area to the end of the file:
:'<,$d
The Vim Wiki has more information on Vim ranges.
Related
Lets say i have the following text.
this.is.some.text
this.is.emos.text
this.is.some.text
this.is.emos.text
I want to edit this text in 'Visual Block' mode so that the text looks as follows.
this.is.some.text
this.is.emos_suffix.text
this.is.some.text
this.is.emos_suffix.text
It should work like this:
Select a visual block
If the visual block contains emos append '_suffix' to the visual
block (Just like the 'A' command would do)
The only native way to accomplish that from visual-block mode or any other visual mode is to use a substitution:
:'<,'>s/emos/&_suffix<CR>
where…
you press :,
Vim inserts the range '<,'> for you, meaning "from the fist selected line, :help '<, through the last selected line, :help '>`,
s/emos/&_suffix substitutes every first occurrence of emos on each line of the given range with itself, :help s/\&, followed by _suffix.
Visual selection is often an unnecessary step and, in this case, visual-block mode is totally useless because A or I is going to operate on every line of the selection anyway.
Another method:
/emos/e<CR>
a_suffix<Esc>
n
.
Another one:
/emos<CR>
cgn<C-r>"
_suffix<Esc>
.
Another one, assuming the cursor is on the first line of your sample:
:,'}s/emos/&_suffix<CR>
Etc.
I want to wrap some code :
myObj.text;
with a function call where the code is passed as an argument.
console.log(myObj.text);
I've thought about using surround.vim to do that but didn't manage to do it.
Any idea if it's possible ? I
With Surround in normal mode:
ysiwfconsole.log<CR>
With Surround in visual mode:
Sfconsole.log<CR>
Without Surround in normal mode:
ciwconsole.log(<C-r>")<Esc>
Without Surround in visual mode:
cconsole.log(<C-r>")<Esc>
But that's not very scalable. A mapping would certainly be more useful since you will almost certainly need to do it often:
xnoremap <key> cconsole.log(<C-r>")<Esc>
nnoremap <key> ciwconsole.log(<C-r>")<Esc>
which brings us back to Surround, which already does that—and more—very elegantly.
I know and use two different ways to accomplish this:
Variant 1:
Select the text you want to wrap in visual mode (hit v followed by whatever movements are appropriate).
Replace that text by hitting c, then type your function call console.log(). (The old text is not gone, it's just moved into a register, from where it will be promptly retrieved in step 3.) Hit <esc> while you are behind the closing parenthese, that should leave you on the ) character.
Paste the replaced text into the parentheses by hitting P (this inserts before the character you are currently on, so right between the ( and the )).
The entire sequence is v<movement>c<functionName>()<esc>P.
Variant 2:
Alternatively to leaving insert mode and pasting from normal mode, you can just as well paste directly from insertion mode by hitting <ctrl>R followed by ".
The entire sequence is v<movement>c<functionName>(<ctrl>R")<esc>.
You can use substitution instruction combined with visual mode
To change bar to foo(bar):
press v and select text you want (plus one more character) to surround with function call (^v$ will select whole text on current line including the newline character at the end)
type :s/\%V.*\%V/foo\(&\)/<CR>
Explanation:
s/a/b/g means 'substitute first match of a with b on current line'
\%V.*\%V matches visual selection without last character
& means 'matched text' (bar in this case)
foo\(&\) gives 'matched text surrounded with foo(...) '
<CR> means 'press enter'
Notes
For this to work you have to visually select also next character after bar (^v$ selects also the newline character at the end, so it's fine)
might be some problems with multiline selections, haven't checked it yet
when I press : in visual mode, it puts '<,'> in command line, but that doesn't interfere with rest of the command (it even prevents substitution, when selected text appears also somewhere earlier on current line) - :'<,'>s/... still works
I want to copy paste some lines in vi.
I have a text like
python class1 def:
code code code
...
code code code
last line class1
python class2 def:
code code code
...
code code code
I want to copy the whole class1. I was trying to do it with yNy, so I needed to get N, that is, to count the number of lines the class has.
Then I thought it would be good to get the line number of python class1 def: (let's say X) and the last line class1 (Y), calculate N=Y-X, go to the first line of the class and do the yNy. However, I could not figure out how I can get the line numbers.
So, is there any way to know which line I am in? And in general, is there any other way to copy paste a whole block like the one I indicated?
This is my vi version:
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 26 2012 16:44:45)
Included patches: 1-547
The current line number can be obtained by :.=. Ctrl-g gives more details including filename, column information, ...
In order to copy a block, go to the start of the line to be copied 0. Hitting v would start the visual mode. Navigate to the last line to be copied. Yank y. (Visual selection is now in buffer.)
Using only normal mode commands:
You can do y} to yank everything from the current line to and including the next empty line, delimiting what Vim considers to be a "paragraph". This may or may not work depending on your coding style.
Still using the notion of "paragraph", you can do yip or yap from anywhere in a "paragraph".
You can set the number option which allows you to see absolute line numbers and therefore be able to do y10G, "yank everything from here to line 10".
You can set the relativenumber option which allows you to see relative line numbers and therefore be able to do y5j, "yank everything from here to 5 lines below".
You can do V/foo<CR>y to yank everything from here to foo linewise.
More generally, you can simply use visual mode to select what you want and yank it.
You can also set a mark on the first line of the class with ma, move the cursor to its last line and do y'a (which sounds like the name of a Lovecraftian deity).
Using Ex commands:
Because the aforementioned number option shows absolute line numbers, you can see that the class ends at line 10 and do :.,10y.
Because the aforementioned relativenumber option shows relative line numbers, you can see that the class ends 5 line below and do :,+5y (dropping the implied .).
Using your statusline (or not):
You can :set ruler to have the current line number displayed on the right side of your statusbar if you have one or on the right side of your command line if you don't have a statusline.
Using Vimscript:
You can use line('.') to retrieve the number of the current line.
Using custom text-objects:
There are a number of custom text-objects available on vim.org for indented blocks, function arguments and many other things. Maybe there is one for Python classes.
More generally, I'd advise you to set either ruler, number or relativenumber permanently in your ~/.vimrc and get used to it.
ruler is the least invasive of the bunch but it's also the most limited: you know where you are but it doesn't help at all when you want to define a target.
number is the most classical and can be used to easily target a specific line.
relativenumber is a bit weird at first and, like number, can be used easily to target a specific line.
Choosing number or relativenumber is, as far as I'm concerned, a matter of taste. I find relativenumber very intuitive, YMMV.
Try the following in command mode
:.= returns line number of current line at bottom of screen
yNy or Nyy copies the next N lines, including the current line
p pastes the copied text after the current line
Additionally,
:set nu! in command mode will turn on/off the line number at the beginning of each line.
let the vim registers do this task. why bother calculating lines
for example if you want to copy line X to line y
1) move your cursor to 1st character of line X.
2) type "ma" . this will save current cursor position in register "a".
3) move cursor to last char of line Y.
4) type "y`a". copy is done
5) p pastes the copied text
This method can work not only lines but block ,words even on characters.
When I pop into Vim's visual mode to, for example, indent a block of text, the command prompt always starts with '<,'>. Can someone break down for me why this is or rather what it does? It seems like it's got something to do with markers but I'm not entirely sure of this based on the manual stuff I've read so far.
'< is the first line visually selected, and '> is the last line visually selected. This is vim's way of making your command apply to only the visual area.
The '<,'> at the beginning of your command line represents the range which you have selected . This is the also the range of test onto which the command you are about to enter would be applied.
For example if I selected a region of text in visual mode and then wanted to replace all occurrences of 'stack' with 'overflow' my command would look like:
:'<,'>s/stack/overflow/g
Without visual mode this same command would have to be accomplished by specifying the line range by hand eg:
:1,10s/helo/hello/g
It's a range defined by two special marks (a mark in an anchor in the text named as "quote+1 letter")
'< `< To the first line or character of the last selected Visual
area in the current buffer. For block mode it may also be the last
character in the first line (to be able to define the block). {not
in Vi}.
'> `> To the last line or character of the last selected Visual
area in the current buffer. For block mode it may also be the
first character of the last line (to be able to define the block).
Note that 'selection' applies, the position may be just after the
Visual area. {not in Vi}.
Source
Once you select in Visual Mode e.g. five lines, then '<,'> means that you will execute the command in that region.
so :'<,'>s/replaceMe/WithThis/g will apply to only that selection
Taking the time to add some points of trivia to the already given answers
:* usually means the same (:he cpo-star),
hitting C-u in command line mode removes the range marker (actually, removes to the beginning of the line)
In Unix the ^ allows you to repeat a command with some text substituted for new text. For example:
csh% grep "stuff" file1 >> Results
grep "stuff" file1
csh% ^file1^file2^
grep "stuff" file2
csh%
Is there a Vim equivalent? There are a lot of times I find myself editing minor things on the command line over and over again.
Specifically for subsitutions: use & to repeat your last substitution on the current line from normal mode.
To repeat for all lines, type :%&
q: to enter the command-line window (:help cmdwin).
You can edit and reuse previously entered ex-style commands in this window.
Once you hit :, you can type a couple characters and up-arrow, and it will character-match what you typed. e.g. type :set and it will climb back through your "sets". This also works for search - just type / and up-arrow. And /abc up-arrow will feed you matching search strings counterchronologically.
There are 2 ways.
You simply hit the . key to perform an exact replay of the very last command (other than movement). For example, I type cw then hello to change a word to "hello". After moving my cursor to a different word, I hit . to do it again.
For more advanced commands like a replace, after you have performed the substition, simply hit the : key then the ↑ up arrow key, and it fills your command line with the same command.
To repeat the previous substition on all lines with all of the same flags you can use the mapping g&.
If you have made a substitution in either normal mode :s/A/B/g (the current line) or visual mode :'<,>'s/A/B/g (lines included in the current selection) and you want to repeat that last substitution, you can:
Move to another line (normal mode) and simply press &, or if you like, :-&-<CR> (looks like :&), to affect the current line without highlighting, or
Highlight a range (visual mode) and press :-&-<CR> (looks like :'<,'>&) to affect the range of lines in the selection.
With my limited knowledge of Vim, this solves several problems. For one, the last visual substitution :'<,'>s/A/B/g is available as the last command (:-<UP>) from both normal and visual mode, but always produces an error from normal mode. (It still refers to the last selection from visual mode - not to the empty selection at the cursor like I assumed - and my example substitution exhausts every match in one pass.) Meanwhile, the last normal mode substitution starts with :s, not :'<,'>s, so you would need to modify it to use in visual mode. Finally, & is available directly from normal mode and so it accepts repetitions and other alternatives to selections, like 2& for the next two lines, and as user ruohola said, g& for the entire file.
In both versions, pressing : then & works as if you had pressed : and then retyped s/A/B/, so the mode you were in last time is irrelevant and only the current cursor line or selection determines the line(s) to be affected. (Note that the trailing flags like g are cleared too, but come next in this syntax too, as in :&g/: '<,'>&g. This is a mixed blessing in my opinion, as you can/must re-specify flags here, and standalone & doesn't seem to take flags at all. I must be missing something.)
I welcome suggestions and corrections. Most of this comes from experimentation just now so I'm sure there's a lot more to it, but hopefully it helps anyway.
Take a look at this: http://vim.wikia.com/wiki/Using_command-line_history for explanation.