I just noticed that entering :q1 closes the file I currently have open (I found this by typo-ing :q!)
Based on commands like 2dd (delete two lines) or 2j (move down two lines), I would have expected :2q to "quit two files" (although, if I open two files with vi -O testfile1 testfile2, :2q only closes one of them - and so does :q2).
Is the number after :q simply discarded? Or is it having some effect that I haven't been able to determine?
And, more generally - how could I answer this question for myself? I checked usr_21.txt| Go away and come back in Vim help, but found nothing about this behaviour. I even checked the famous question, but no-one had mentioned anything about this.
The q command with number closes the given split in that position.
:q<split position> or :<split position>q will close the split in that position.
Lets say your vim window layout is as follows:
-------------------------------------------------
| | | |
-------------------------------------------------
| | | |
| | | |
| Split 1 | Split 2 | Split 3 |
| | | |
-------------------------------------------------
If you run q1 command it will close the first split. q2 will close the second split and vice versa.
The order of split position in the quit command does not matter. :2q or :q2 will close the second split.
If the split position you pass to the command is greater than the number of current splits, it will simply close the last split.
For example, if you run the q100 on the above window setup where there are only 3 splits, it will close the last split (Split 3).
This is whats listed in the vim documentation. You can find the documentation by typing :help :close then scroll up to find the Closing a window section.
If [count] is greater than the last window number the last
window will be closed:
¦ ¦ :1quit " quit the first window
¦ ¦ :$quit " quit the last window
¦ ¦ :9quit " quit the last window
" if there are fewer than 9 windows opened
¦ ¦ :-quit " quit the previous window
¦ ¦ :+quit " quit the next window
¦ ¦ :+2quit " quit the second next window
Related
Usually NERDTree doesn't get resized when I do this. But when I am working with a bunch of horizontal and vertical splits for a while, it can happen.
I don't know what the change in state of within the instance of Vim is that makes this issue happen during a vim session. I'm not sure if the number of splits makes a difference.
Usually I set up 4 splits, along with the NERDTree window on the left like this...
|--|-----|-----|
| | | |
|NT|-----|-----|
| | | |
|--|-----|-----|
When I do the equal split resize I end up with this when things go wrong...
|-----|-----|-----|
| | | |
| NT |-----|-----|
| | | |
|-----|-----|-----|
That's not the end of the world. The real problem is when I close a couple of splits it ends up like this...
|--------|--------|
| | |
| NT |--------|
| | |
|--------|--------|
... which is just silly and I have to exit Vim and start over.
I use Ctrl-w followed by = to carry out the equal resizing of vim splits.
I think this is a bug, perhaps in NERDTree. There is a "change of state" that causes this problem to start happening. It is when you do a Ctrl-w followed by 'o' to maximize one of the splits. I've reported this as an issue in the NERDTree github project at https://github.com/scrooloose/nerdtree/issues/644
For example, I use 3 split windows to open 3 different files:
+---------------+-----------+
| | |
| window 1 | |
| | |
+---------------+ |
| | window 3 |
| | |
| window 2 | |
| | |
| | |
+---------------+-----------+
Now, I want to open them with 3 different tabs and vice versa
Is there any trick to achieve this? Or do I need any plugin?
Besides, if I want to display 3 buffers in 3 full screen windows (as opposed to a split window), what should I do?
Not sure if there is a plugin, which will do all splits at once. But there is a way to do it one at a time
For moving the current split into a new tab use
ctrl + w, T (shift + t)
repeat the above process for all splits.
Now for getting the tab in to split, you can make use of a plugin call "Tabmerge", download Tabmerge.vim from http://www.vim.org/scripts/script.php?script_id=1961 to ~/.vim/plugin
then to merge the tab use :Tabmerge [tab number] [top|bottom|left|right]
If you are careful with your buffer list, you can use :sball and :tab sball to open in windows or tabs, respectively.
How to copy a specific line from less ? Lets say I am opening a man ( which is by default opened by less ) and want to select and copy it to clipboard and after that lets say paste it to file opened in vim ? I don't want to use the mouse wheel to paste. I am looking for a simple Ctrl-c , Ctrl-v method as in windows.
When opening a man page I can't switch to my default editor (which is vim ) with 'v' key because less shouts with "Cannot edit standard input" error.
Thanks a lot and sorry if this question is silly.
tl;dr, use m and |.
Example:
Within the man page of less, by running man less:
7g
mx
6g
|x
xclip (Linux) or pbcopy (macOS), to copy to clipboard.
cat > file, to save to file, or cat >> file for append mode.
We would get:
less - opposite of more
The key things to learn are just two less commands: m (mark), and | (pipe).
Command m (mark)
Followed by any lowercase letter, marks the current position with that letter.
The marker we used above is x, as in step 2, it marked line 7 with x.
Command | (pipe)
| <m> shell-command
<m> represents any mark letter. Pipes a section of the input file to the given shell command.
The section of the file to be piped is between the first line on the current screen and the position marked by the letter.
<m> may also be ^ or $ to indicate beginning or end of file respectively. If <m> is . or <newline>, the current screen is piped.
Using |xpbcopy, we pipe the line-range [7, 6] into pbcopy, as line 6 is currently the first line on the screen, and line 7 is the one we marked as x, and pbcopy is the command to put text into the macOS clipboard.
Alternatively, use xclip on Linux, or even dd of=/path/to/file to save as a file.
Note
The text range is boundary inclusive, so both the beginning and the ending lines of the range, or at least 2 lines are copied.
We marked the range in the backward way, namely from bottom to top, otherwise, less might behave awkwardly, and throw the whole screen through the pipe.
I think I found the solution: it is using tmux. Tmux provides it's own clipboard ( correct me if I am wrong ). From tmux I can enter the copy-mode wherever I am ( in MAN pages, less, console output ) and let me to copy the content.
The current accepted answer is based on setting a mark, navigating one line up, piping the current screen up to that mark into the clipboard.
Copy 1 line into clipboard
Navigate to the line (using 1g to go the first line)
|
Press RETURN
head -1 | clip (or xclip et. al)
head -1 | tr '\n' '\' | clip
Explanation
| pipes the whole screen into the command, so navigate to where we want that to start.
2-3. | <m> shell-command if <m> is . or newline, the current screen is piped.
head -1 just the first line
| tr '\n' '\' replace the the carriage return with \ (for pasting to shell)
| clip pipe to clipboard (or /dev/clipboard, xclip et. al)
Short answer: Ctrl+C and Ctrl+V are associated with other actions. For instance Ctrl+C sends an interrupt signal to the foreground process. Usually you need to use Ctrl+Shift+C and Ctrl+Shift+V in order to copy and paste from a terminal.
Long answer: This very good thread from superuser.
I have a screen session where in i have created 10 sessions .
I use the below key combination to toggle to the active screen session attached.
ctrl + a + number 0 to 9
Ctrl-a 0-9 Go to a window numbered 0-9
i have created a 10th session now
ctrl-a 10 will end up to the screen session 1.
how to toggle to the 10th session ??
work around for the same is , go to the 9th session
Ctrl-a 9
and then ctrl-n will lead me to the 10th session.
Link i referred to learn screen in linux
http://www.kb.indiana.edu/data/acuy.html
Pressing Ctrl+A then ' (apostrophe) will display a prompt and let you enter the number or name of a window to switch to.
To rename a window, use Ctrl+AShift+A. (Ctrl+U to remove the old name before typing a new one.)
Other ways to switch windows include:
Ctrl+ACtrl+A to return to the window you were looking at before
Ctrl+An or Ctrl+ASpace to go to the next in sequence
Ctrl+Ap to go to the previous in sequence
Ctrl+A" to select from a list (thanks stan and dogbane!)
I think some of these can open a windows list:
ctrl+a " #window list
ctrl+a w #window list
You can use C-a " which will show you a list of all windows and you can then select the 10th one
Alternatively, go to the 9th window and then use C-a n to go to the 10th.
According to screen's manual page, you can add the following lines to your ~/.screenrc file:
bind -c demo1 0 select 10
bind -c demo1 1 select 11
bind -c demo1 2 select 12
bindkey "^B" command -c demo1
makes C-b 0 select window 10, C-b 1 window 11, etc. Alternatively, you can use:
bind -c demo2 0 select 10
bind -c demo2 1 select 11
bind -c demo2 2 select 12
bind - command -c demo2
makes "C-a - 0" select window 10, "C-a - 1" window 11, etc.
I was just looking at this post which describes how to wrap entire words in vim. The accepted solution was this:
:set formatoptions=l
:set lbr
Which takes this text (tabs are shown as \t):
*Inside of window *Outside of window
|---------------------------------------|
|\t\tthis is a like of text that will wr|ap here
|\t\tcan you see the wrap |
| |
|---------------------------------------|
This accomplishes a behavior like this (tabs are shown as \t):
*Inside of window *Outside of window
|---------------------------------------|
|\t\tthis is a like of text that will |
|wrap here |
|\t\tcan you see the wrap |
| |
|---------------------------------------|
I would however like to redefine this function. I would like the wrapped line to have the same number of tabs in front of it that the line above has plus one. Ie:
*Inside of window *Outside of window
|---------------------------------------|
|\t\tthis is a like of text that will |
|\t\t\twrap here |
|\t\tcan you see the wrap |
| |
|---------------------------------------|
Any ideas?
This did not work when the question was originally asked, but as of June 25, 2014, this will work. (Assuming you update your vim to a version newer than that date)
Add to your .vimrc:
" Indents word-wrapped lines as much as the 'parent' line
set breakindent
" Ensures word-wrap does not split words
set formatoptions=l
set lbr
And that's it!
--
Some people (myself included) share a single .vimrc across multiple computers. In that case, it's important to have this line be robust (to avoid annoying error messages). This is a little better:
if has("patch-7.4.354")
" Indents word-wrapped lines as much as the 'parent' line
set breakindent
" Ensures word-wrap does not split words
set formatoptions=l
set lbr
endif
This way, if you're on an earlier version of vim, you don't get an error message.
The breakindent patch has what you're looking for. I successfully applied it using instructions found in this thread:
Patch Vim with the breakindent patch on OS X with Homebrew
Specifically, echristopherson's Homebrew formula.
I know this thread is old but it's popular on google and I came across it multiple times when trying to find a solution.
EDIT: This patch is now included with vim as patch 7.4.338. See: https://retracile.net/blog/2014/07/18/18.00
On Yosemite (Mac OS X), I used snowbound's command with hombrew:
brew install macvim --with-features=huge --override-system-vim --HEAD
The best you're going to get is the showbreak option which will put a fixed string in front of each wrapped line (I use set showbreak=...).
I agree with the answer that says 'showbreak' is the best option. Showbreak does not typically allow you to put nonprinting characters (e.g., spaces or tabs) into the showbreak string, so as typically used it will just give you an indicator along left margin, i.e., no real indent. This isn't great, since main goal of OP, I think, is to give wrapped lines an indent keep them from cluttering left margin area and looking like lines of their own.
So one way to add an (ugly) indent using showbreak is to just use a lot of characters, .e.g, ":set showbreak=>--------------->". This results in something that looks like this:
*Inside of window *Outside of window
|---------------------------------------|
|\t\tthis is a like of text that will |
|>--------------->wrap here |
|\t\tcan you see the wrap |
| |
|---------------------------------------|
A better alternative might be to make use of nonbreaking space characters (assuming your instance of Vim is unicode enabled), each of which can be entered into the showbreak string using the key sequence of ctrl-v,160. That way you can enter a showbreak string that is blank at the left side and appear to be a true indent. E.g., ":set showbreak=. . . . . . . . . . >>" where each '.' in the command is actually a nonbreaking space character entered by pressing ctrl-V,160. That way you end up with a wrap that is nicely indented, like this:
*Inside of window *Outside of window
|---------------------------------------|
|\t\tthis is a like of text that will |
| >>wrap here |
|\t\tcan you see the wrap |
| |
|---------------------------------------|
You still don't have any ability to vary the level of indent according to indent of previous line, but at least you get clean indent of wrapped lines without lots of visual clutter along left margin of window. There could still be confusion if indent of a wrapped line is less than that of the beginning of an actual line, but this could perhaps be avoided by making the showbreak "indent" quite large (i.e., greater than any indent commonly found in your code) but still small enough that it provides enough space for legible wrapping of the text. For many uses I think a showbreak indent of 40 or 50 spaces would do this pretty well.