gvim: file list on split window, open in top screen - vim

I have a split window open in GVIM.
On the bottom window I have a text file open.
This text file contains a list of files (with full path).
What I would like is a command so that according to my cursor position on a file name in the bottom window, that file will open on the top window. At the moment, when I press g-f it opens it in the bottom window instead of the file list.
Any ideas?
EDIT: I managed to solve it with a macro (copy line, ctrl-w, up, :e and paste). Any better suggestions are welcome!

You could use a mapping like this one:
nnoremap <F7> :let cf = expand("<cfile>") <bar> wincmd k <bar> execute "e " . cf<CR>
save the filename under the cursor in a variable,
move the cursor to the window above,
edit the file.
But what problem are you trying to solve?

Related

Vim, how to make Netrw to replace current open file instead of split opening new window, when you press preview or create new file commands?

Recently I've been using netrw. I've put these four lines on my .vimrc:
let g:netrw_banner = 0
let g:netrw_liststyle = 3
let g:netrw_winsize = 25
map <C-n> :Lexplore <CR>
And I can easily toggle Lexplore and browse through the files to edit them. In this mode when I press Enter on any file on left hand side Netrw, it replaces the file in the right hand side window with new file; exactly what I want.
Problems start when I want to preview a file with "p" command on netrw or create a new file with "%" command. In former case (Preview) it split to the new window but I want it again to replace the file in right hand side window just like when I press Enter to to edit the file. And in latter case (creating a new file) it replaces the Lexplore (Netrw in the left hand side) instead of replacing the file on the right hand side window.
Is there any way that I can fix these issues? I've tried a lot of Netrw commands but nothing gives me what I want.
The "p" netrw command is mostly just pedit. So, assuming you don't already have a preview window, just press <cr> atop the file you want to preview and then :set pvw. That will make the window holding the file the preview window.
The second issue concerns "%" to create a new file. I'll think this over, but I think perhaps the better behavior is as you suggest -- keep the Lexplore window but open the new file in the editing window.
Please try version 171e of netrw which you can find at my website.
I believe this is related to a bug in vim. Once you use :Lexplore it apparently changes the g:netrw_chgwin option permanently, so whenever you try to pres <CR> in any netrw window, it will always open in a new window.

keyboard shortcut for vim

please could someone give me a list of some keyboard shortcut editor vim in particular to save and close?
I try ctrl + s orders or ctrl + q or ctrl + x and then ctrl + q but no success until a present
ZZ does save and close
ZQ does close without save
both work in normal mode.
For quiting vim shell,
hit ESC and
then :q for quit without any changes.
Enter
Some commands are here:
:q[uit] Quit Vim. This fails when changes have been made.
:q[uit]! Quit without writing.
:cq[uit] Quit always, without writing.
:wq Write the current file and exit.
:wq! Write the current file and exit always.
:wq {file} Write to {file}. Exit if not editing the last
:wq! {file} Write to {file} and exit always.
:[range]wq[!] [file] Same as above, but only write the lines in [range].
ZZ Write current file, if modified, and exit.
ZQ Quit current file and exit (same as ":q!").
For quick overview : Vim commands
The quick answer is:
Hit ESC
type :wq [name of the file, if it's a new file]
type Enter
The long answer, well, it's really long...
Some commands in this guide start with a colon: pressing it will display the command prompt where the subsequent command is written.
Commands without a colon are more like hotkeys - they can be used in the Vim default mode (which is the mode Vim starts in).
Commands written in CAPITAL LETTERS are specific keys: for example, ESC means the escape key on your keyboard.
All commands in Vim are case-sensitive.
EXITING VIM
To quit, discarding any changes you might have made:
:q!
Memorize: quit dammit!
To quit, saving any changes you've made:
:wq
Memorize: write to disk and quit
NAVIGATING THE EDITOR
To move around the currently open file, use your arrow keys.
To move to line 285:
:285
To search for the word import:
/import
EDITING TEXT
To start inserting text on the current cursor location:
i
Memorize: insert
To start inserting at the end of the current line:
A
Memorize: Append
To exit insert mode, and return to the default mode:
ESC
SELECTING TEXT
To start selecting, enter the visual mode:
v
Memorize: visual
Select text by moving with your arrow keys.
To exit visual mode:
ESC
COPY, CUT, PASTE
To copy the current selection into the buffer (think of it as a clipboard):
y
Memorize: yank
To cut the current selection:
d
Memorize: delete
To copy the current line into the buffer:
yy
Memorize: yank yank
To copy 3 lines including the current line into the buffer:
3yy
To cut the current line and place it into the buffer:
dd
Memorize: delete delete
To cut 5 lines including the current line:
5dd
To paste the buffer before the current line:
P
Note: Uppercase P
To paste the buffer after the current line:
p
UNDO AND REDO
To undo the last change:
u
Memorize: uh-oh :)
To redo the last change you just undid:
CTRL + R
To see the number of changes:
:undolist
To undo the last two changes:
2u
The Vim multi-level undo tree is very powerful. Read more about it here.
OPENING FILES
To open the file index.html instead of the current one:
:edit index.html
SAVING FILES
To save the file you're currently editing:
:w
Memorize: write to disk
To save the file with a different name, here changes.txt (ie. Save As):
:w changes.txt
Searching and Replacing
To search and replace all occurences of a string in the file:
:%s/typo/corrected/g
To search and replace, but prompt before replacing:
:%s/typo/corrected/gc
Memorize: confirm
Syntax highlighting and Indentation
Turn on syntax highlighting:
:syntax on
Enable automatic indentation:
:set autoindent
Increase indentation on multiple lines by selecting them in visual mode, and pressing:
>
Working with multiple files
TABS
To open server.py in a new tab:
:tabe server.py
Memorize: tab edit
To move to the next tab on the right:
:tabn
Memorize: tab next
To move to the previous tab on the left:
:tabp
Memorize: tab previous
To close a tab, move to it and use :q or :wq as you would normally.
SPLIT VIEW
To open templates/base.html in a vertical split screen:
:vs templates/base.html
Memorize: vertical split
To open shared.js in a horizontal split screen:
:sp shared.js
Memorize: the 'default' horizontal split
To move between split screens:
CTRL + W + ARROW KEYS
To close a split screen, move to it and use :q or :wq as you would normally.
More information at here.
Those are very basic questions. It is better for you to start vim in tutorial mode, like this:
$ vimtutor

Open Nerdtree file in non-adjacent split

Normally I switch over to the Nerdtree file buffer by hitting Ctrl+H twice to move the cursor over to the left edge of the screen. However, this means that when I open files, they are always in the split adjacent to the NERDTree window. How can I open files in the split that's not adjacent to the NERDTree window?
NERDtree will open a file in the previous window if you press o or enter. You can open it in a split of the previous window by pressing i, or in a vertical split by pressing s.
It sounds like you have Ctrl-H mapped to Ctrl-W h. The trick to not having NERDtree open the file into a window that is adjacent to the NERDTree window is to not have the adjacent window be the previous window.
To achieve this, toggle the NERDTree window closed. (:NERDTreeToggle, or whatever your mapkey is to toggle NERDTree). Then move to the appropriate window you want to open the file in. Then toggle NERDTree open again. Now you can open the file in the previous window that you were in using o or enter... or in splits using i or s.
To save having to toggle the NERDTree window open/close, you may want to set the option NERDTreeQuitOnOpen. Then you just need to open NERDTree when you're in a window that you intend to open a file into.
I usually use :NERDTreeFind in order to move from the right split window to the NERDtree on the very left.
This command shows the currently open file within the NERDtree window. Based on this, the previous window is the one on the right side. This of course only works conveniently if the file you want to open is close in the directory tree to the file already open.
To quickly access :NERDTreeFind, I mapped it to Ctrl + m in my ~/.vimrc:
nnoremap <C-m> :NERDTreeFind<CR>

How can I do something like gf, but in a new vertical split?

In vim gf opens the file whose name is under the cursor in the current window. <C-W>f does the same but opens it in a new window. However this new window is created with an horizontal split.
How can I do the same and get a vertical split?
I tried various alternatives that did work (like :vsplit +normal\ gf), but have a slight problem: if the file doesn't exist, a new window is created anyways. This does not happen with gf nor <C-W>f. I'd like to have this behaviour as well from the "open file under cursor on a vertical split" command. How can I do that?
Here is possible mapping:
:nnoremap <F8> :vertical wincmd f<CR>
With a file name under cursor, hit F8 and voila.
Here is a solution involving 5 key strokes:
Ctrl-wv
gf
That is Ctrl-wv followed by gf in the normal mode.
Here is an equivalent solution that involves 7 key strokes:
:vs
gf
That is :vs command in command-line mode followed by gf in normal mode. Assuming we are in the normal mode with the cursor on the filename already, the complete sequence of keystrokes are: Shift:vsEntergf.
When we enter Ctrl-wv in normal mode or :vs in the command-line mode, the current window is split into two with the same file in both and the cursor remains in the same position (i.e., on the filename of the file that we want to go to in a new vertical split). So if we press gf now, the current window is now updated with the file we want to go to.
The end result is two vertical split windows: one with the first file and another with the file we wanted to go to.
If we're playing Vim Golf, I think the winning solution is to do:
Ctrl-w then f
to open the relevant file in a horizontal split.
Then change it to a vertical split, send then active window to the left or right side of the screen by doing either
Ctrl-w then L (active window to the left)
or
Ctrl-w then R (active window to the right)
Note that the final keystroke for left / right must be uppercase. The others should be lowercase

VIM: How can i open a file at right side as vsplit from a left side NerdTree panel?

I installed NerdTree plugin.
How can i open a file at right side as vsplit from a left side NerdTree panel ?
To make vsplit put the new buffer on the right of the current buffer:
set splitright
Similarly, to make split put the new buffer below the current buffer:
set splitbelow
I haven't tried this with NerdTree, however.
There's a s command, but it opens a file split to the left of current buffer. Though you can press Ctrl+W r to swap windows then.
This is a bit of a hack, but how I do it is this:
Put cursor in window I want to open file into
Hit <leader>n<leader>n (this closes NERDtree and then opens it again with the cursor in NERDtree)
Select the file
On my system this opens it on the last window I was just on if the file isn't already open on my screen.
Start in the window you want to open your file into.
Hit <leader>n<leader>n to close and reopen NERDTree
Select the file you want!
A quick C-w = will get your windows back to proper proportions.
Not sure if anyone else is still struggling with this, but here's how I dealt with it.
You can use
:ls
to list the available buffers. Which would look something like:
1 "foo.txt"
2 "blame_the_user.java"
:b1 to select foo.txt
:b2 for blame_the_user.java
This method can be done from any window setup using :sp or :vs.
Open the buffers you want to split first.

Resources