I cant find the files I save in vim [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
Improve this question
I am trying to learn code using a vim editor as I hear I can get superior long term benefits when I start making long codes and documents. I am a pretty big fan of keyboard shortcuts so I do not mind taking the long road to superior editing abilities. Any way the Linux book I am reading has recommended it and even has many exercises that require typing and saving in vim. The problem is that I cannot find the files I save in vim. I enter the escape mode hit the colon key and use the w option. The vim tells me that the file is saved but when later try to find it in my Linux directory I can find nothing. I have tried using the type, file, find, and locate commands as well as whereis even though this is for commands. Thank you for help in advance.

There is no such thing as escape mode. Most likely you are referring to normal mode. The easiest way to enter normal mode is by hitting the escape key, Esc. Once in normal mode you can save the contents of the current buffer into the file by typing :w. Alternatively you can specify a new file name with :w filename. Then you can quit vim with :q. These steps can be combined by typing :wq.
You mention that Vim tells you the file has been saved but you cannot find the file. In this case type Ctrl-g after saving the file, the file name relative to the current directory should be displayed.

Your "Linux book" almost certainly tells you to do something like:
$ vim filename
with filename being an existing file or a non-existing file. In both cases, doing :w<CR> in Vim, will write filename…
in the working directory if filename is just a file name, without anything "path-like":
$ cd /tmp
$ vim foo.txt
(do your thing)
:wq
$ echo $PWD/$_
/tmp/foo.txt
at the given location relative to the working directory if filename is a relative path:
$ cd ~/Documents
$ vim exercices/foo.txt
(do your thing)
:wq
$ echo $PWD/$_
/home/rubberbandface/Documents/exercices/foo.txt
at the given location if filename is an absolute path:
$ vim ~/Documents/exercices/foo.txt
(do your thing)
:wq
$ echo $_
/home/rubberbandface/Documents/exercices/foo.txt
That said, you shouldn't learn Linux, Vim, and programming at the same time.

Related

How to remove comments of ASM code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have an directory with about 300 source code in ASM.
So, I need to remove the comments (";") of this codes.
Until now, I removed the comments of 3 files, where each file has 100 lines of code.
Somebody know a script that can help me?
Sed is your friend:
find <asm_dir> -type f | xargs sed -i -e '/^;/d' -e 's/^\([^;]*\);.*$/\1/'
The first expression deletes lines that begin with the comment character ;. The second expression strips inline comments (from ; to the end of the line).
You can simply accomplish this in e.g. Vim or Emacs. I'm going to discuss Vim here.
Assuming you have all your .asm files in one directory go to your shell and do something like
cd /path/to/my/files
gvim *.asm
This will open all your .asm files for editing in gvim (graphical vim). If you are not familiar with Vim it's a great text editor and we are going to record a macro to do our job. Only do what I say or you will mess things up :) To be on the safe side you should backup your files in case you mistype something and delete more than you wish to.
Type these characters (or press the appropriate thing on your keyboard as specified in <>'s):
qa:g/^\s*;/d<Enter>:n<Enter>q
This will start recording a macro [q], store it in [a], execute a global deletion of every line in the first file that starts with optional whitespace followed by a semicolon [:g/^\s*;/d<Enter>], move to the next file [:n<Enter>] and save the macro [q]. All you have to do now is run this macro as many times as you have files left. You can either get this by running ls *.asm | wc -l in your directory with the saved files or you can simply overshoot it and input a larger number, Vim will stop on the last file and notify you there's no more files to edit. So with the overshooting example you could type
1000#a
and the macro will start running through all the files. This may take some time so be patient. Once it's done we still haven't saved our files so we could check the results before commiting to them. You can check a couple of files if they look OK and if yes type
:wa<Enter>
All your files are saved now and you can exit Vim with ZQ or :q<Enter>.

How to Insert Before IP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this question
I have a single file contents with thousand of IP's, my problem is how can i add all the IP line with 'allow from' for example as below:
From This:
27.146.0.0/16
49.50.12.0/19
49.50.44.0/20
49.50.60.0/22
To Be this:
allow from 27.146.0.0/16
allow from 49.50.12.0/19
allow from 49.50.44.0/20
allow from 49.50.60.0/22
allow from 49.124.0.0/15
allow from 57.73.15.0/24
Is there any Windows tools can do that or Linux command should be ok too. Please help
What's wrong with using a real manly editor?
In Vim/gVim:
gg: place the cursor in the start of the file
Control + v: start visual block mode
G: move the cursor to the end of the file, selecting every line
I: Enter Insert Mode
Type your string: allow from
Press ESC to leave Insert Mode.
:wq Save and exit.
Done!
Sed is probably easier...
Linux
With awk:
$ awk '$0="allow from "$0' file
allow from 27.146.0.0/16
allow from 49.50.12.0/19
allow from 49.50.44.0/20
allow from 49.50.60.0/22
As $0 is the whole string, we append text in the beginning of it. Then, it gets printed because the default behaviour of awk is {print $0}.
With sed:
$ sed 's/^/allow from /' file
allow from 27.146.0.0/16
allow from 49.50.12.0/19
allow from 49.50.44.0/20
allow from 49.50.60.0/22
As ^ means beginning of the line, we replace it with the "allow from " text. This way, the "new beginning of file" becomes the text you want to add.
Both examples will show you the output in your screen. To save it, do command file > new_file.
Windows
This answer may help you: Adding text to start of each new line in a .txt file. However, as Kent pointed out in comments, any editor can make it. I strongly recommend Notepad++ for this kind of purposes.
You can do this with awk
awk '{print "allow from "$0}' <filename>

bash auto complete [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
Command completion in bash(1) is quite handy; I like this feature very much. But I have a question.
For example, we all have Documents and Downloads in $HOME directory.
So if I input cd ~ and then press TAB, then it will traverse all the directory under $HOME. When Documents come up and the shell now displays cd ~/Documents, I want to go deeper into this directory. For example, there is a work directory in Documents. My intention is to cd to the work directory.
So what should I do?
I usually input a w then press TAB, but when this action repeats too much I found it quite annoying. And sometimes I don't know exactly what files are under this directory.
Do you have any good ideas?
ps: In fact, i met this problem when i was using vim. I input :e then TAB, it will cycle the subfolders, the only way i found to stop the cycling and enter the next level is input a /. So the path has double '/' in the end.
You could switch your shell to ZShell, which allows you to tab through directories without typing the first letter.
For example, you can type
cd ~/
Pressing TAB will bring up a list of all subfolders (like in bash), but pressing TAB multiple times will allow you to cycle through the subfolders. Pressing the -> or / key will allow you to start tab-completing inside that directory, and so on.
You can sometimes use chsh (man chsh for more info) to change your shell, if it's your own personal machine, or your network might have a special way to change your shell. You might also want to Google for some common .zshrc / .zshenv settings or migrate your old .bashrc / .cshrc / .profile settings.
Zsh also has ways to set up TAB completion for other tasks, for example, it can TAB complete svn files based on those that aren't already in SVN (for svn add). To get these features, add
autoload -U compinit
compinit
to your .zshrc file. There are plenty more ways to customize your TAB completion in zsh (such as with case-insensitivity, or for arguments to different programs), but if you're interested in those, you can probably find more information than I know by searching.
Like the Jack Toole's zsh suggestion (which is a perfectly fine shell), if you have to use bash, you can stick this in your .bashrc for the same "continue to hit tab cycle through possibilities" feature:
test -n "$PS1" && bind TAB:menu-complete
There's also a project called bash-completion that provides a lot of other kinds of completion other than pathname completion.
If you don't know what is in the directory, just press TAB one more time.

Clear a terminal screen for real [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
Using the clear command on the terminal only fools the user into thinking the screen has been cleared...you can still see output from the previous commands when you scroll using the mouse. This makes life difficult when you are drowning in a tsunami of text.
Various solutions (escape code etc.) which can be found on the Internet are only variations of what the clear command already does.
So how do you clear the contents of a terminal in Linux for real?
Use the following command to do a clear screen instead of merely adding new lines ...
printf "\033c"
yes that's a 'printf' on the bash prompt.
You will probably want to define an alias though...
alias cls='printf "\033c"'
Explanation
\033 == \x1B == 27 == ESC
So this becomes <ESC>c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.
Edit
Here are a few other ways of doing it...
printf "\ec" #\e is ESC in bash
echo -en "\ec" #thanks #Jonathon Reinhart.
# -e Enable interpretation of of backslash escapes
# -n Do not output a new line
KDE
The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer...
clear && echo -en "\e[3J"
Or perhaps use the following alias on KDE...
alias cls='clear && echo -en "\e[3J"'
I got the scroll-back clearing command from here.
Try reset. It clears up the terminal screen but the previous commands can be accessed through arrow or whichever key binding you have.
tput reset
That will do the trick!
None of the answers I read worked in PuTTY, so I found a comment on this article:
In the settings for your connection, under "Window->Behavior" you'll find a setting "System Menu Appears on ALT alone". Then CTRL + L, ALT, l (that's a lower case L) will scroll the screen and then clear the scrollback buffer.
(relevant to the OP because I am connecting to an Ubuntu server, but also apparently relevant no matter what your server is running.)
Clean the visible screen
clear
Clean screen and clear buffer
clear && clear
Clean and 1-sec delay
reset
Clean without 1-sec delay
tput reset
My favorite human friendly command for this is:
reset
Tested on xterm and VT100. It also helps after an abnormal program termination.
Keeps the command buffer, so up-arrow will cycle through previous commands.
I know the solution employing printing of new lines isn't much supported, but if all else fails, why not? Especially where one is operating in an environment where someone else is likely to be able to see the screen, yet not able to keylog. One potential solution then, is the following alias:
alias c="printf '\r\n%.0s' {1..50}"
Then, to "clear" away the current contents of the screen (or rather, hide them), just type c+Enter at the terminal.
The following link will explain how to make that alias permanent so you don't have to keep typing it in.
https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias
These are the steps detailed at that link.
vim ~/.bashrc or gedit ~/.bashrc or what ever text editor you like
put alias cls='printf "\033c"' at the bottom of the file
save and exit
. ~/.bashrc (and yes there should be a space between . and ~)
now check to see if everything worked!
I take no credit for this information just passing it along.
Just to add that tmux scroll buffer does not clear with clear, reset or printf. You need to :clear-history. See link.
With KDE and Ubuntu 12.04 LTS and the "Konsole" terminal, none of the posted answers work. However, pressing default keyboard shortcut CTRL+Shift+X does work! Source:
https://bugs.kde.org/show_bug.cgi?id=288913
echo -e "\e[3J"
This works in Linux Machines
Compile this app.
#include <iostream>
#include <cstring>
int main()
{
char str[1000];
memset(str, '\n', 999);
str[999] = 0;
std::cout << str << std::endl;
return 0;
}

cscope or ctags why choose one over the other? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I primarily use vim / gvim as an editor and am looking at using a combination of lxr (the Linux Cross Reference) and either cscope or ctags for exploring the kernel source. However, I haven't ever used either cscope or ctags and would like to hear why one might choose one over the other taking into consideration my use of vim as a primary editor.
ctags enables two features: allowing you to jump from function calls to their definitions, and omni completion. The first means that when you are over a call to a method, hitting g] or CTRL-] will jump to the place where that method is defined or implemented. The second feature means that when you type foo. or foo->, and if foo is a structure, then a pop-up menu with field completion will be shown.
cscope also has the first feature - using set cscopetag - but not the last. However cscope additionally adds the ability to jump to any of the places where a function is called as well.
So as far as jumping around a code base is concerned, ctags will only ever lead you towards the place where the function is implemented, whereas cscope can show you where a function is called too.
Why would you choose one over the other? Well, I use both. ctags is easier to set up, faster to run and if you only care about jumping one way it will show you less lines. You can just run :!ctags -R . and g] just works. It also enables that omni complete thing.
Cscope is great for bigger, unknown code bases. The set up is a pain because cscope needs a file containing a list of names of files to parse. Also in vim, by default there are no key bindings set up - you need to run :cscope blah blah manually.
To solve the fist problem I've got a bash script cscope_gen.sh that looks like this:
#!/bin/sh
find . -name '*.py' \
-o -name '*.java' \
-o -iname '*.[CH]' \
-o -name '*.cpp' \
-o -name '*.cc' \
-o -name '*.hpp' \
> cscope.files
# -b: just build
# -q: create inverted index
cscope -b -q
This searches for code that I'm interested in, creates the cscope.files list and creates the database. That way I can run ":!cscope_gen.sh" instead of having to remember all the set up steps.
I map cscope search to ctrl-space x 2 with this snippet, which mitigates the other downer of cscope:
nmap <C-#><C-#> :cs find s <C-R>=expand("<cword>")<CR><CR>
There's this cscope_maps.vim plugin that sets up a bunch of similar bindings. I can never remember what all the options mean, so tend to stick to ctrl-space.
So to conclude: ctags is easier to set up and mostly works without doing much else, it's vital for omni-complete too. cscope provides more features if you have to maintain a large and mostly unknown code base, but requires more leg work.
I was in the same situation some months ago...
The lack of precision of ctags is a pain in a.., and i find cscope much better for all the macros related stuff (and there are a bunch of macros in the linux kernel)..
concerning the usage, that's actually straightforward...you just type cscope -R at the root of your kernel and then you've got nothing to worry about.. (I mean if you just want to explore that's perfect...)
Then, the key bindings are all based on Ctrl-\ (you can remap it if you're allergic to Ctrl), you mainly use s and g....,
Developing for the kernel, I didn't need so much the completion....
Anyway, go for cscope, this is much more convenient, accurate.
Hmm... You should probably use etags instead of ctags...
If you use cscope, then you can see call chains, i.e., who calls this function & which functions does this function call?
I am not sure if this can be done using etags / ctags...
That's just one feature... what about finding out the file that contains a particular function definition? This you get only in cscope.
I use both cscope and etags, they are both good for different things, especially when working with a large codebase, such as the Linux Kernel. In fact, I started using cscope and etags when I started working with the Linux Kernel / Xen.
LXR is not great, because you have to click, go over the network etc., whereas you can build the cscope and tags databases on your kernel code and do not have to go over the network (unlike lxr).
Suggest use global gtags. Could use vim plugin gen_tags to integrate gtags with vim.

Resources