bash auto complete [closed] - linux

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.

Related

I cant find the files I save in vim [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 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.

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>.

linux command for store frequently used commands

Does linux have commands allow me to push my regularly used commands into a stack or something so that I can easily fetch them from it and jump among the most frequently used commands as much as I want.
I know we can create alias for commands, but that's not what I am looking for here. My case is more like I have a few horrible long directories, I need to be able to switch between them quickly and frequently. eg.
cd /pathA/pathB/pathC/pathD/
cd /pathE/pathF/pathG/pathH/
vim /pathA/pathB/pathC/pathD/test.txt
.......
I don't really want to create alias for each command here because the paths vary quite often as well, I don't want to constantly update my alias.
Regarding directories, you can push the paths on stack
pushd path_you_want_to_store
popd
Well, there's the history feature of your shell that would allow you to recall previous commands.
A quick command for going back and forth between the last 2 directories is
cd -
As far as more directories are concerned, I use this set of aliases in my .tcshrc to keep track of them. If I am in a directory I want to remember I just say
keep
or
keep2
and then later I can get back there by simply typing
cd $k
or
cd $k2
If I want to see the directories I have "saved" I type
ks
I can also use these variable for other operations such as cp/mv (and quite frequently this is what I do to save on typing long path names).
You didn't specify your shell, so this is using the tcsh but could be easily adapted to any other shell as long as you know how to set up equivalent aliases. This allows me to save up to 6 different directories, you can decide how many you set up.
This is my own "homegrown" solution that has served me well for the last 10+ years, there might be others, perhaps "built-in". At this point I use these automatically and so regularly that I don't even think of them as aliases.
alias keep 'set k=`pwd`'
alias keep2 'set k2=`pwd`'
alias keep3 'set k3=`pwd`'
alias keep4 'set k4=`pwd`'
alias keep5 'set k5=`pwd`'
alias keep6 'set k6=`pwd`'
alias ks 'echo $k; echo $k2; echo $k3; echo $k4; echo $k5; echo $k6'
Write your 'long path' once and fetch it next time by pressing "Ctrl + r".
You need to press "Ctrl + r" first. Then start typing your 'long path' it will start showing you the path you have already typed.
To find existence of more old records (of the same text you have typed) keep pressing "Ctrl + r" and it will show you the old records.
Also check out the CDPATH variable, that can hold a list of common directories that you want to jump into - eg :
export CDPATH=/pathA/pathB/pathC
cd pathD
# now you're at /pathA/pathB/pathC/pathD
If you feel the command will be used frequently you could add a tag
command #useful
Then
[ctrl+r] #useful

undelete the deleted command in bash

If you have written a really long command, say cd /very/long/path, and then you do ctrl+c or ctrl+u (if the cursor is at the end), and then you realise that you want the command back, is there any way to get the full line back without re-typing. Is there any trick to change .bashrc so that bash_history keep track of keys pressed on the shell and not just after the enter is hit.
I have answered a question at In bash, how does one clear the current input? and realised if we have some option like this it would be very helpful.
To undo, use either
Ctrl+X, Ctrl+U; or
Ctrl+_ (underscore).
See bind -P for a full list of keybindings in bash.

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