Opening file present in another folder which has same relative path as current opened file in vim using vertical diffsplit? - vim

Say I have following 2 similar files in 2 different workspace on same linux machine.
/user1/ws1/ip/src/ip_main.c
/user1/ws2/ip/src/ip_main.c
Now I did,
cd user1/ws1/
vim ip/src/ip_main.c
then I press ESC then executed
:cd ../ws2
which shows my pwd as "user1/ws2"
Now I want to open ip/src/ip_main.c in pwd i.e "user1/ws2"
using ESC
:vertical diffsplit "some logic to get to ws2/ip/src/ip_main.c"
note after :cd ../ws2
:pwd command show "user1/ws2" but :echo $PWD command still show "user1/ws1"
How can i do it,Can anyone help ?

Diffing both files is easily done via shell globbing, e.g. in Bash:
$ vimdiff -O /user1/ws{1,2}/ip/src/ip_main.c
Withing Vim, you have to use relative paths (i.e. prepend ../.., then go down into the other hierarchy). <C-R>% on the command-line (cp. :help c_CTRL-R) inserts the current path; this may avoid retyping much of the similar path, especially when used with the command-line window (:help c_CTRL-F).
With my EditSimilar plugin, you can use this:
:DiffSplitSubstitute 1=2

Related

How to get to long directory quickly when writting code in VIM

I am writing Bash script using VIM. I need to cd to a directory and run the command tool. The command tool is deep inside the directory. How do I quickly cd to that directory instead of manually typing the directory out in VIM ? In terminal prompt, I can get to the directory quickly using tab. It does not work in VIM.
Thanks
ffl3883
You can change to the currently edited file's directory with :cd %:h; see :help filename-modifiers. Likewise, if you trigger the tool from Vim :! % can do this quickly (and repeat with :!!). Or just :set autochdir, so that the current directory within Vim always follows the currently edited file (and you can then just reference the file via ./).
When typing file paths in vim (as I often do for shell scripts), I find filename-complete invaluable. Simply type <C-X><C-F> in insert mode.
N.B. It does not work in all cases (generally vim prefers the path to be a separate WORD), but a quick edit-complete-fixup isn’t terrible.

How to make a new directory and a file in Vim

When using Vim as a text editor, is there a way to create new directories and files, while in text editor mode? Instead of going back to the command line and creating a new directory and file.
If you are in the file explorer mode, you can use:
d for creating a directory
% for creating a new file
You can get into the explorer mode with issuing a command :Sexplore or :Vexplore
There is no need to call external commands with !
Assuming you're running a shell, I would shell out for either of these commands. Enter command mode with Esc, and then:
:! touch new-file.txt
:! mkdir new-directory
A great plugin for these actions is vim-eunuch, which gives you syntactic sugar for shell commands. Here's the latter example, using vim-eunuch:
:Mdkir new-directory
Switch to file browsing mode
:Ex or if that is not working use :Explore
then press
d
and add the new directory name.
Assuming you just ran vim on new file in the directory that does not exist:
vim new_dir/new_file.txt
When you try :w you will get 'E212: Can't open file for writing'
To create new directory and file use this:
:!mkdir -p %:h
For the sake of completeness:
Shell out and use normal commands, such as :!mkdir my_dir and :!touch foo.txt (as mentioned in Jake's answer here) will create the directory and file in CURRENT working directory, which is the directory when you started your current vim process in the beginning, but NOT NECESSARILY the same directory of the file that you are currently editing, or the same directory that your :Explore explorer is currently viewing. When in doubt, always use :!pwd to check your current working directory first, and use relative path when necessary.
So if your project contains multiple sub-directories, a more convenient way is to:
type :Explore to enter the explorer mode first,
and then you can easily navigate to whatever sub-directory you like, by typing up-arrow or down-arrow (or j or k) to move cursor, typing Enter to enter a sub-directory, typing - to go up a level of directory. (Note that, all these navigation does NOT change your current working directory either);
Now you can type d to be prompted for a directory name, or type % to be prompted for a file name, and then they will be created in the directory currently shown on screen.
PS: These keys are actually mentioned in the built-in help F1.
Alternatively you can use :e . to get to explorer mode and then hit d .to create the new directory .Thought a shorter answer might be better

gvim: change the default working directory

when I open gvim using Alt+F2 it takes as its default working directory my home folder.
How can I change the working folder after or while running gvim? can i pass that folder as a parameter when open gvim?
You could use a shortcut.
The simplest way, though, would be to
:edit $MYVIMRC
append a line
cd /home/user/my/work/dir
save (optionally execute :w|source % to immediately reload)
Inside vim
use
:pwd
:cd some/other/dir
To view/change current working directory.
Use e.g.
:cd %:h
to change to the directory containing the file loaded in the active window.
If you need/want to do this often, consider just setting 'autochdir'
:se autochdir
From the docs:
When on, Vim will change the current working directory
whenever you open a file, switch buffers, delete a
buffer or open/close a window. It will change to the
directory containing the file which was opened or
selected. This option is provided for backward
compatibility with the Vim released with Sun ONE
Studio 4 Enterprise Edition.
Note: When this option is on some plugins may not work.
You can pass an a folder to gvim (when you have NERDTree then it will be a file tree) You can cd before start to begin in directory you want or use :cd <path> command to change current working directory, which can be passed to -c flag when running Vim:
$ [g]vim -c 'cd <path>'
You can also check current dir using :pwd command.
You can change the working directory with the :cd command. You can also pass this in a command-line option like this:
vim -c "cd wherever"
If you like the working directory to always be the file you're currently editing you can use the set autochdir option. Put that in your ~/.vimrc or see :help autochdir.
I know I'm late, but I started using CDargs which is a bash tool to mark certain directories as bookmarks, then use cdb and press tab to list all the bookmarked directories.
There is a vim plugin that interacts with the settingsfile of this tool: vim-cdargs.
This combo works really nice for me to switch between projects.
Or after opening gvim to go quickly to some bookmarked folder, then use Ctrl-p plugin to quickly find the file I want to edit.
extra hint: I don't even want to type :Cdb so I abbreviated c to expand to :Cdb by adding this to my vimrc:
cnoreabbrev c Cdb
after which typing :c followed by a space, will expand into :Cdb.
EDIT: I now use vim-startify which provides a start page for vim that shows the most recent used files. And with the option let g:startify_change_to_vcs_root = 1 it will change the working directory to the outermost vcs root folder of the file you opened. Which is almost always what I want.
Furthemore, I created my own 'plugin' with some key mappings that will switch to the closest or furthest directory, in the path of the current buffer, containing a .git directory or file. In order to easily switch between searching for files in the current git submodule or in the overal supermodule.
Also I switched to fzf with fzf-vim instead of Ctrl-p, which works significantly faster and is more highly configurable.

Indenting in VIM with all the files in Folder

I have a folder containing hundreds of TTL (TeraTermLanguage) files.
Now I wanted indent all these files.
I have created teraterm.vim for indentation and I open a file using VIM and do "gg=G" and whole file gets indented properly.
But is there any way, where I can indent all the files in folder.
I wanted to do with help of Shell. But in VIM I couldnt pass file indent command as the argument to VIM.
Please suggest which is the best way I can do indentation to all the files in VIM.
Much simpler than scripting vim from the bash command line is to use vimscript from inside of vim (or perhaps a much simpler one-liner for scripting vim from the command line). I personally prefer using the arg list for all multi-file manipulation. For example:
:args ~/src/myproject/**/*.ttl | argdo execute "normal gg=G" | update
args sets the arglist, using wildcards (** will match the current directory as well as subdirectories)
| lets us run multiple commands on one line
argdo runs the following commands on each arg (it will swallow up the second |)
execute prevents normal from swallowing up the next pipe.
normal runs the following normal mode commands (what you were working with in the first place)
update is like :w, but only saves when the buffer is modified.
This :args ... | argdo ... | update pattern is very useful for any sort of project wide file manipulation (e.g. search and replace via %s/foo/bar/ge or setting uniform fileformat or fileencoding).
(other people prefer a similar pattern using the buffer list and :bufdo, but with the arg list I don't need to worry about closing current buffers or opening up new vim session.)
Open up a terminal. Type:
$ vim -w indentme.scr foo.c
Then, type this exactly (in command mode):
gg=G:wq
This will close vim, saving the process of indenting all lines in the file to a Vim script called indentme.scr.
Note: indentme.scr will contain a record of all key commands typed, so when you are done indenting the file, don't spend a lot of time using the arrow keys to look around the file, because this will lead to a much larger script and will severely slow down batch operations.
Now, in order to indent all the lines in a file, just type the following command:
$ vim -s indentme.scr unindented-file.c
Vim will flash open-shut (if you're on a fast computer and not editing a huge file), indenting all lines, then saving the file in-place.
Unfortunately, this will only work on one file at a time, but you can scale the functionality easily using sh's for loop:
for filename in *.ttl ; do
vim -s indentme.scr "$filename"
done
Note: This will save-over any file. Unless set bk is in your ~/.vimrc, don't expect a backup to be saved.
I went off of amphetamachine's solution. However, I needed to recursively search through multiple directories. Here's the solution that I used:
$ find . -type f -name '*.ttl' -exec vim -s indentme.scr "{}" \;
Taking reference from the above answers I would like to make this complete.
I will start from the scratch so that a beginner can understand.
Step-1
Install astyle (tool used for formatting ) by using the following command
Open up a terminal. Type:
sudo apt-get install astyle
Step-2 Make sure you have vim installed on your system.Run the below commands from the directory in which your code files are.The below command will create a script that we intend to run recursively so as to beautify each and every file in our directory.(as mentioned in the above answer)
vim -w indentme.scr foo.c
Step-3 Then, type this exactly (in command mode) and press enter
:%!astyle
Step-4Then type this exactly (in command mode) and press enter
:wq
Step-5 Last run this recursively by the following command:
find . -type f -name '*.cpp' -exec vim -s indentme.scr "{}" \;
All your cpp files will be formatted properly.

How to open a file in a list of files in Vim?

I have a longish list of files opened in vim that looks like this:
/dir1/file1
/dir2/file2
/dir2/file3
.....
How can I open all of them one by one the easiest way possible in the same session of vim either with split or edit?
I'd say with -p for tabs
vim -p `cat yourlistoffiles`
You can use quickfix mode, as following
:set errorformat=%f
:cf myfilelist
at this point you can use the normal quickfix shortcuts to go through your files, :cn for the next file, :cp for the previous one and :cr to go to the first again.
EDIT:
oh, if you want to read the list from the current buffer, use :cb instead of :cf in in the instructions above
You can do the following
cat file | xargs vim
Where "file" contains your list of files, this will open the files in the same vim session. As usual when opening multiple buffers, you can navigate forward with :bn and backward :bp.
I'm going to assume you have the file list open inside Vim, and want to simulate the "gf" command across the whole list...
Edit your .vimrc to include this function:
function Openall()
edit <cfile>
bfirst
endfunction
You can then highlight the entire file (or the set of paths you want to open) using visual mode (1G, Shift-V, G) and typing ":call Openall()". Afterwards the command row will show this:
:'<,'>call Openall()
This will run the new Openall() function across all highlighted lines.
Press Enter and all the files will be opened in background buffers. You can then access them using the usual buffer commands. :ls will display them as a list.
I suppose you want to select and list in vim. all the files of a certain extension. From your home directory or a particular source.
find . -name "*.sh" | vim -
Then within vim, you could search and view this potentially huge list.
(Another topic)
You found your file, now you want to open it in a split?
CTRL-W F *CTRL-W_F*
Split current window in two. Edit file name under cursor and
jump to the line number following the file name. See |gF| for
details on how the line number is obtained.
{not available when the |+file_in_path| feature was disabled
at compile time}
CTRL-W gf *CTRL-W_gf*
Open a new tab page and edit the file name under the cursor.
Like "tab split" and "gf", but the new tab page isn't created
if the file does not exist.
{not available when the |+file_in_path| feature was disabled
at compile time}
For my use case, where you already have a buffer open with the list of files in it, I have found the best way to do this. You can type this in vim cmd mode:
:% normal gf<C-v><C-o>
(where <C-v> is you literally typing Ctrl and v, same for <C-o>)
and it becomes:
:% normal gf^O
How this works:
% runs the command for every line in the file
normal runs everything after it in normal mode
gf opens the file under the cursor in a new buffer
<C-v> lets you enter a control-code (<C-o> in this case)
<C-o> goes back to the last location, which will always be this buffer with the list of files
Potential usage:
# open buffer with list of files from grep
$ grep -rl "new User()" mycode/ libraries/ | vim -
# then runs this in vim:
:% normal gf^O
Bonus: Make a keyboard shortcut
Add the following to your ~/.vimrc :
" Open all files in current buffer (e.g. after piping to `vim -`)
nnoremap <leader>oa :% normal gf^O<cr>
This maps \oa to run the command (since <leader> is \ for me).
An alternate method using a macro:
qogf<C-o>j
which runs gf (go file) then uses <C-o> to get back to the initial unnamed buffer, then just goes down one line with j, ready to be repeated.
Then see how many lines in the buffer (12) and run the macro like this:
12#o
I often need to open a changing list of files that had been modified in my SVN checkout.
This one liner works to open all modified files in vim tabs.
svn st | grep ^M | awk "{print($2)}" | xargs vim -p
Try this with bash:
$ vim -S <(sed "s/^/badd /" <your file name>)
But I don't know why the first line of the file is ignored... :-O
This script works as expected:
rm -f myfile
for i in `seq 10000`
do
touch $i
echo $i >> myfile
done
vi -c "badd `head -1 myfile`" -S <(sed "s/^/badd /" myfile)
http://vimdoc.sourceforge.net/htmldoc/starting.html#-S
http://vimdoc.sourceforge.net/htmldoc/windows.html#:bad
Maybe this could help:
http://vimdoc.sourceforge.net/htmldoc/windows.html
It's as simple as typing
vim /dir1/file1 /dir2/file1 /dir2/file2 ...
Once you're in vim, you can switch betwen then with ":n" to go to the next file, ":prev" to go to the previous file.
My searchInRuntime plugin has a :Sp and a :Vsp commands that'll do the trick when called banged. However, the files have to exist.

Resources